Skip to content

Commit c981124

Browse files
authored
Fix synthetic transformer nodes throwing on getChildren when transforming a switch statement (#1465)
1 parent 793e7f0 commit c981124

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

src/transformation/visitors/switch.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,16 @@ const containsBreakOrReturn = (nodes: Iterable<ts.Node>): boolean => {
99
for (const s of nodes) {
1010
if (ts.isBreakStatement(s) || ts.isReturnStatement(s)) {
1111
return true;
12-
} else if (ts.isBlock(s) && containsBreakOrReturn(s.getChildren())) {
13-
return true;
14-
} else if (s.kind === ts.SyntaxKind.SyntaxList && containsBreakOrReturn(s.getChildren())) {
12+
} else if (ts.isBlock(s) && containsBreakOrReturn(s.statements)) {
1513
return true;
14+
} else if (s.kind === ts.SyntaxKind.SyntaxList) {
15+
// We cannot use getChildren() because that breaks when using synthetic nodes from transformers
16+
// So get children the long way
17+
const children: ts.Node[] = [];
18+
ts.forEachChild(s, c => children.push(c));
19+
if (containsBreakOrReturn(children)) {
20+
return true;
21+
}
1622
}
1723
}
1824

test/transpile/transformers/transformers.spec.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,22 @@ describe("factory types", () => {
3535
.expectToEqual(true);
3636
});
3737
});
38+
39+
// https://github.com/TypeScriptToLua/TypeScriptToLua/issues/1464
40+
test("transformer with switch does not break (#1464)", () => {
41+
util.testFunction`
42+
const foo: number = 3;
43+
switch (foo) {
44+
case 2: {
45+
return 10;
46+
}
47+
case 3: {
48+
return false;
49+
}
50+
}
51+
`
52+
.setOptions({
53+
plugins: [{ transform: path.join(__dirname, "fixtures.ts"), import: "program", value: true }],
54+
})
55+
.expectToEqual(true);
56+
});

0 commit comments

Comments
 (0)