X Tutup
Skip to content

Commit 504fd1a

Browse files
fix: scope switch in repeat-until, emit break
1 parent 9172e88 commit 504fd1a

File tree

4 files changed

+14
-5
lines changed

4 files changed

+14
-5
lines changed

src/transformation/visitors/break-continue.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { findScope, ScopeType } from "../utils/scope";
88
export const transformBreakStatement: FunctionVisitor<ts.BreakStatement> = (breakStatement, context) => {
99
const breakableScope = findScope(context, ScopeType.Loop | ScopeType.Switch);
1010
if (breakableScope?.type === ScopeType.Switch) {
11-
return undefined;
11+
return lua.createBreakStatement(breakStatement);
1212
} else {
1313
return lua.createBreakStatement(breakStatement);
1414
}

src/transformation/visitors/switch.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { performHoisting, popScope, pushScope, ScopeType } from "../utils/scope"
66
const containsBreakStatement = (statements: ts.Node[]): boolean => {
77
for (const s of statements) {
88
if (
9+
ts.isIfStatement(s) ||
910
ts.isSwitchStatement(s) ||
1011
ts.isWhileStatement(s) ||
1112
ts.isDoStatement(s) ||
@@ -85,5 +86,5 @@ export const transformSwitchStatement: FunctionVisitor<ts.SwitchStatement> = (st
8586
const expression = context.transformExpression(statement.expression);
8687
statements.unshift(lua.createVariableDeclarationStatement(switchVariable, expression));
8788

88-
return lua.createDoStatement(statements);
89+
return lua.createRepeatStatement(lua.createBlock(statements), lua.createBooleanLiteral(true));
8990
};

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,25 @@ exports[`switch uses elseif 1`] = `
44
"local ____exports = {}
55
function ____exports.__main(self)
66
local result = -1
7-
do
7+
repeat
88
local ____switch3 = 2
99
if ____switch3 == 0 then
1010
do
1111
result = 200
12+
break
1213
end
1314
elseif ____switch3 == 1 then
1415
do
1516
result = 100
17+
break
1618
end
1719
elseif ____switch3 == 2 then
1820
do
1921
result = 1
22+
break
2023
end
2124
end
22-
end
25+
until true
2326
return result
2427
end
2528
return ____exports"

test/unit/switch.spec.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ test.each([0, 1, 2, 3])("switchWithBrackets (%p)", inp => {
207207
`.expectToMatchJsResult();
208208
});
209209

210-
test.each([0, 1, 2, 3])("switchWithBracketsBreakInConditional (%p)", inp => {
210+
test.each([0, 1, 2, 3, 4])("switchWithBracketsBreakInConditional (%p)", inp => {
211211
util.testFunction`
212212
let result: number = -1;
213213
@@ -223,6 +223,11 @@ test.each([0, 1, 2, 3])("switchWithBracketsBreakInConditional (%p)", inp => {
223223
}
224224
case 2: {
225225
result = 2;
226+
227+
if (result != 2) break;
228+
}
229+
case 3: {
230+
result = 3;
226231
break;
227232
}
228233
}

0 commit comments

Comments
 (0)
X Tutup