Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/transformation/visitors/switch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ export const transformSwitchStatement: FunctionVisitor<ts.SwitchStatement> = (st
let statements: lua.Statement[] = [];

const caseClauses = statement.caseBlock.clauses.filter(ts.isCaseClause);
for (const [index, clause] of caseClauses.entries()) {

// Starting from the back, concatenating ifs into one big if/elseif statement
const concatenatedIf = caseClauses.reduceRight((previousCondition, clause, index) => {
// If the clause condition holds, go to the correct label
const condition = lua.createBinaryExpression(
switchVariable,
Expand All @@ -28,8 +30,11 @@ export const transformSwitchStatement: FunctionVisitor<ts.SwitchStatement> = (st
);

const goto = lua.createGotoStatement(`${switchName}_case_${index}`);
const conditionalGoto = lua.createIfStatement(condition, lua.createBlock([goto]));
statements.push(conditionalGoto);
return lua.createIfStatement(condition, lua.createBlock([goto]), previousCondition);
}, undefined as lua.IfStatement | undefined);

if (concatenatedIf) {
statements.push(concatenatedIf);
}

const hasDefaultCase = statement.caseBlock.clauses.some(ts.isDefaultClause);
Expand Down
41 changes: 41 additions & 0 deletions test/unit/__snapshots__/conditionals.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`array 1`] = `
"local ____exports = {}
function ____exports.__main(self)
local result = -1
local ____switch3 = 2
if ____switch3 == 0 then
goto ____switch3_case_0
elseif ____switch3 == 1 then
goto ____switch3_case_1
elseif ____switch3 == 2 then
goto ____switch3_case_2
end
goto ____switch3_end
::____switch3_case_0::
do
do
result = 200
goto ____switch3_end
end
end
::____switch3_case_1::
do
do
result = 100
goto ____switch3_end
end
end
::____switch3_case_2::
do
do
result = 1
goto ____switch3_end
end
end
::____switch3_end::
return result
end
return ____exports"
`;
31 changes: 30 additions & 1 deletion test/unit/conditionals.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ test("variable in nested scope does not interfere with case scope", () => {
`.expectToMatchJsResult();
});

test.only("switch using variable re-declared in cases", () => {
test("switch using variable re-declared in cases", () => {
util.testFunction`
let foo: number = 0;
switch (foo) {
Expand Down Expand Up @@ -311,6 +311,35 @@ test.each([0, 1, 2, 3])("switchWithBracketsBreakInInternalLoop (%p)", inp => {
`.expectToMatchJsResult();
});

test("switch uses elseif", () => {
test("array", () => {
util.testFunction`
let result: number = -1;

switch (2 as number) {
case 0: {
result = 200;
break;
}

case 1: {
result = 100;
break;
}

case 2: {
result = 1;
break;
}
}

return result;
`
.expectLuaToMatchSnapshot()
.expectToMatchJsResult();
});
});

test("switch not allowed in 5.1", () => {
util.testFunction`
switch ("abc") {}
Expand Down