Merged
Conversation
also refactored a few things for clarity and added tests
thejustinwalsh
approved these changes
Sep 6, 2021
Contributor
There was a problem hiding this comment.
I think this is covered by other tests, but passes with expected results. LGTM!
let x = 1;
let y = 2;
let result = [];
switch (x) {
case 1: {
let y = 100;
result.push(y);
}
case 2: {
let y = 500;
result.push(y);
}
case 3: {
y = y + 50;
result.push(y);
}
}
return result
Perryvw
reviewed
Sep 10, 2021
src/transformation/utils/scope.ts
Outdated
| scope: Scope, | ||
| statements: lua.Statement[] | ||
| ): lua.Statement[] { | ||
| function hoistVariableDeclarations(context: TransformationContext, scope: Scope, statements: lua.Statement[]) { |
Member
There was a problem hiding this comment.
Think I would prefer if you explicitly declared the return type of these hoist* functions for clarity - I guess it's just HoistingResult?
| // Otherwise, we need to generate a set of if statements to emulate the switch. | ||
| let statements: lua.Statement[] = []; | ||
| const statements: lua.Statement[] = []; | ||
| const prefixStatements: lua.Statement[] = []; |
Member
There was a problem hiding this comment.
Think it might be clearer if you just named this hoistedStatements? I think to avoid naming conflicts with your destructuring assignments is to just not destructure, I don't think that is really making things clearer when it's split over multiple lines like this anyway.
sanikoyes
pushed a commit
to sanikoyes/TypeScriptToLua
that referenced
this pull request
Sep 24, 2021
* fixes and refactors for hoisting to fix switch statements and make logic more clear * applied hoisting fix to new switch implementation also refactored a few things for clarity and added tests * fixed issues with hoisting from default clause * added snapshots for a couple tests and a comment * fixed edge case with hoisting in a solo default clause * addressing review feedback Co-authored-by: Tom <tomblind@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
fixes #1099
To fix hoisting in switch statements, I've added
separateHoistedStatementsas an intermediate step toperformHoisting. This keeps the results separate, whichtransformSwitchStatementuses directly so it can properly order statements after all clauses are transformed.Previously, switch cases had some custom code to handle hoisting all locals to the top. I've refactored this to use the standard hoisting code path and just force-hoist everything that is immediately in a switch's scope.
Additionally, there was some very hard to follow 'magic' which handled function hoisting so that it could be output as
local function foo()in some cases andlocal foo; function foo()in others when needed. This was incredibly hard to follow in the codebase, so I've changed it to always use the later form which works in all cases and keeps the implementation a bit more straightforward.