X Tutup
Skip to content

Fix prototype call check for nullable types#1230

Merged
Perryvw merged 2 commits intomasterfrom
nullable-prototype-calls
Mar 17, 2022
Merged

Fix prototype call check for nullable types#1230
Perryvw merged 2 commits intomasterfrom
nullable-prototype-calls

Conversation

@Perryvw
Copy link
Member

@Perryvw Perryvw commented Mar 13, 2022

Turns out that when we check for built-in prototype methods (like string.prototype.substring for example), we were not taking optional chaining into account. Therefore strOrUndefined?.substring() would lead to invalid Lua. This PR fixes the check for all prototype calls we detect, so they also work in case of optional chaining.

Fixes #1218

@Perryvw Perryvw requested review from lolleko and tomblind March 13, 2022 19:10
// Issue #1218: https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1218
test.each(['"foo"', "undefined"])("prototype call on nullable string (%p)", value => {
util.testFunction`
function toUpper(str?: string) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

str?: string is one way of triggering the bug in #1218 - Is it also worth having tests for the other potential triggers, str: string | undefined and str: string | null? I only see tests for str?: string.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They should all resolve to the same type (kinda), but you are right, adding some more tests wouldn't hurt. I added some more.

@Perryvw Perryvw merged commit 3b41774 into master Mar 17, 2022
@Perryvw Perryvw deleted the nullable-prototype-calls branch March 17, 2022 19:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Null-safe ?.toUpperCase() on Interface property is converted to :toUpperCase() (a nil value)

3 participants

X Tutup