X Tutup
Skip to content

Commit 741db69

Browse files
fix: lexical scoping
create scope for switch and remove additional scope for each clause.
1 parent 272b5ac commit 741db69

File tree

2 files changed

+7
-14
lines changed

2 files changed

+7
-14
lines changed

src/transformation/visitors/switch.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,7 @@ export const transformSwitchStatement: FunctionVisitor<ts.SwitchStatement> = (st
4141
caseBody[i] = statement.caseBlock.clauses
4242
.slice(i, end >= 0 ? end + i + 1 : undefined)
4343
.reduce<lua.Statement[]>(
44-
(statements, clause) => [
45-
...statements,
46-
lua.createDoStatement(context.transformStatements(clause.statements)),
47-
],
44+
(statements, clause) => [...statements, ...context.transformStatements(clause.statements)],
4845
[]
4946
);
5047
}
@@ -88,5 +85,5 @@ export const transformSwitchStatement: FunctionVisitor<ts.SwitchStatement> = (st
8885
const expression = context.transformExpression(statement.expression);
8986
statements.unshift(lua.createVariableDeclarationStatement(switchVariable, expression));
9087

91-
return statements;
88+
return lua.createDoStatement(statements);
9289
};

test/unit/__snapshots__/switch.spec.ts.snap

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,17 @@ exports[`switch uses elseif 1`] = `
44
"local ____exports = {}
55
function ____exports.__main(self)
66
local result = -1
7-
local ____switch3 = 2
8-
if ____switch3 == 0 then
9-
do
7+
do
8+
local ____switch3 = 2
9+
if ____switch3 == 0 then
1010
do
1111
result = 200
1212
end
13-
end
14-
elseif ____switch3 == 1 then
15-
do
13+
elseif ____switch3 == 1 then
1614
do
1715
result = 100
1816
end
19-
end
20-
elseif ____switch3 == 2 then
21-
do
17+
elseif ____switch3 == 2 then
2218
do
2319
result = 1
2420
end

0 commit comments

Comments
 (0)
X Tutup