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
12 changes: 9 additions & 3 deletions src/transformation/visitors/switch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@ const containsBreakOrReturn = (nodes: Iterable<ts.Node>): boolean => {
for (const s of nodes) {
if (ts.isBreakStatement(s) || ts.isReturnStatement(s)) {
return true;
} else if (ts.isBlock(s) && containsBreakOrReturn(s.getChildren())) {
return true;
} else if (s.kind === ts.SyntaxKind.SyntaxList && containsBreakOrReturn(s.getChildren())) {
} else if (ts.isBlock(s) && containsBreakOrReturn(s.statements)) {
return true;
} else if (s.kind === ts.SyntaxKind.SyntaxList) {
// We cannot use getChildren() because that breaks when using synthetic nodes from transformers
// So get children the long way
const children: ts.Node[] = [];
ts.forEachChild(s, c => children.push(c));
if (containsBreakOrReturn(children)) {
return true;
}
}
}

Expand Down
19 changes: 19 additions & 0 deletions test/transpile/transformers/transformers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,22 @@ describe("factory types", () => {
.expectToEqual(true);
});
});

// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1464
test("transformer with switch does not break (#1464)", () => {
util.testFunction`
const foo: number = 3;
switch (foo) {
case 2: {
return 10;
}
case 3: {
return false;
}
}
`
.setOptions({
plugins: [{ transform: path.join(__dirname, "fixtures.ts"), import: "program", value: true }],
})
.expectToEqual(true);
});