Skip to content

Simplify optional chaining expressions where possible#1381

Merged
Perryvw merged 7 commits intoTypeScriptToLua:masterfrom
GlassBricks:simplify-optional
Jan 21, 2023
Merged

Simplify optional chaining expressions where possible#1381
Perryvw merged 7 commits intoTypeScriptToLua:masterfrom
GlassBricks:simplify-optional

Conversation

@GlassBricks
Copy link
Copy Markdown
Contributor

This PR simplifies/reduces the lua output of optional chains, in the most common cases.

  • Uses an and statement (a and a.b) instead of an if statement where possible.
  • Reuses identifiers on the left expression when possible

isDelete?: ts.DeleteExpression
): ExpressionWithThisValue {
const luaTemp = context.createTempNameForNode(node);
const luaTempName = context.createTempName("opt");
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.

Why change this from what it was?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

"node" was too generic and so confusing; there are a lot of variables, TS and Lua, in the body.

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.

It wasn't actually "node" literal though, it was already deducing a very specific name based on what the node is, see the changes in the snapshots where it changed ____table_has_result_0 to ___opt_0

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sorry, earlier I thought the comment was addressing something else.

The reason for this is that long-ish optional chains, which are somewhat common, generate long names such as "____a_b_c_foo_result_c_1". "____opt" is shorter and still signifies what the original TS was.

This shouldn't depend on strictNullChecks
This tests that the output statements are correct.
@GlassBricks GlassBricks requested a review from Perryvw January 20, 2023 03:13
@Perryvw Perryvw merged commit 3d0e98d into TypeScriptToLua:master Jan 21, 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

Development

Successfully merging this pull request may close these issues.

2 participants