Skip to content

Don't output parameter initializer statement if default value is nil#1424

Merged
Perryvw merged 4 commits intoTypeScriptToLua:masterfrom
GlassBricks:nil-default-parameter
Mar 30, 2023
Merged

Don't output parameter initializer statement if default value is nil#1424
Perryvw merged 4 commits intoTypeScriptToLua:masterfrom
GlassBricks:nil-default-parameter

Conversation

@GlassBricks
Copy link
Copy Markdown
Contributor

Closes #1422
Also fixes #1423

context.transformExpression(value)
);
const statements = [...precedingStatements];
if (!lua.isNilLiteral(parameterValue)) {
Copy link
Copy Markdown
Member

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.

Copy link
Copy Markdown
Contributor Author

@GlassBricks GlassBricks Mar 28, 2023

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.

const lua = builder.getMainLuaCodeChunk();
expect(lua).not.toMatch(" x = nil");
})
.expectToEqual(["bar", "nil", "baz"]);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Would prefer if you could just use expectToMatchJsResult here, using default TS (no language extensions) with an object and helper function for example.

Copy link
Copy Markdown
Contributor Author

@GlassBricks GlassBricks Mar 28, 2023

Choose a reason for hiding this comment

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

Could you give a more detailed example what that could look like?
Besides using language extensions, I can't think of a good method that transpiles to nil (not just returns undefined) that works both in JS and lua.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The 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

@GlassBricks GlassBricks requested a review from Perryvw March 28, 2023 23:10
@Perryvw Perryvw merged commit bfba5b7 into TypeScriptToLua:master Mar 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants