-
-
Notifications
You must be signed in to change notification settings - Fork 185
Don't output parameter initializer statement if default value is nil #1424
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -107,6 +107,48 @@ test("Function default binding parameter maintains order", () => { | |
| `.expectToMatchJsResult(); | ||
| }); | ||
|
|
||
| test.each(["undefined", "null"])("Function default parameter with value %p", defaultValue => { | ||
| util.testFunction` | ||
| function foo(x = ${defaultValue}) { | ||
| return x; | ||
| } | ||
| return foo(); | ||
| ` | ||
| .expectToMatchJsResult() | ||
| .tap(builder => { | ||
| const lua = builder.getMainLuaCodeChunk(); | ||
| expect(lua).not.toMatch("if x == nil then"); | ||
| }) | ||
| .expectLuaToMatchSnapshot(); | ||
| }); | ||
|
|
||
| test("Function default parameter with preceding statements", () => { | ||
| util.testFunction` | ||
| let i = 1 | ||
| function foo(x = i++) { | ||
| return x; | ||
| } | ||
| return [i, foo(), i]; | ||
| `.expectToMatchJsResult(); | ||
| }); | ||
|
|
||
| test("Function default parameter with nil value and preceding statements", () => { | ||
| util.testFunction` | ||
| const a = new LuaTable() | ||
| a.set("foo", "bar") | ||
| function foo(x: any = a.set("foo", "baz")) { | ||
| return x ?? "nil"; | ||
| } | ||
| return [a.get("foo"), foo(), a.get("foo")]; | ||
| ` | ||
| .withLanguageExtensions() | ||
| .tap(builder => { | ||
| const lua = builder.getMainLuaCodeChunk(); | ||
| expect(lua).not.toMatch(" x = nil"); | ||
| }) | ||
| .expectToEqual(["bar", "nil", "baz"]); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would prefer if you could just use
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you give a more detailed example what that could look like?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh I see what you are testing now, that seems maybe a bit too specific but okay |
||
| }); | ||
|
|
||
| test("Class method call", () => { | ||
| util.testFunction` | ||
| class TestClass { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couldn't we do that at the top of the function? Check ts.isUndefinedLiteral || ts.isNullLiteral? That way we can avoid a whole bunch of transformations.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doing this makes this the most general; it applies to anything that results in a nil literal (casts/parenthesis/etc, language extensions, plugins) and shouldn't have any performance impact.