Skip to content

Commit 8e858b0

Browse files
committed
Merge branch 'nested-switch-fix'
2 parents c9005c6 + 21335c2 commit 8e858b0

File tree

2 files changed

+48
-6
lines changed

2 files changed

+48
-6
lines changed

src/Transpiler.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export class LuaTranspiler {
5151
checker: ts.TypeChecker;
5252
options: ts.CompilerOptions;
5353
genVarCounter: number;
54-
transpilingSwitch: boolean;
54+
transpilingSwitch: number;
5555
namespace: string[];
5656
importCount: number;
5757
isModule: boolean;
@@ -62,7 +62,7 @@ export class LuaTranspiler {
6262
this.checker = checker;
6363
this.options = options;
6464
this.genVarCounter = 0;
65-
this.transpilingSwitch = false;
65+
this.transpilingSwitch = 0;
6666
this.namespace = [];
6767
this.importCount = 0;
6868
this.sourceFile = sourceFile;
@@ -277,7 +277,7 @@ export class LuaTranspiler {
277277
}
278278

279279
transpileBreak(): string {
280-
if (this.transpilingSwitch) {
280+
if (this.transpilingSwitch > 0) {
281281
return '';
282282
} else {
283283
return this.indent + "break\n";
@@ -410,11 +410,11 @@ export class LuaTranspiler {
410410
}
411411
this.pushIndent();
412412

413-
this.transpilingSwitch = true;
413+
this.transpilingSwitch++;
414414
clause.statements.forEach(statement => {
415415
result += this.transpileNode(statement);
416416
});
417-
this.transpilingSwitch = false;
417+
this.transpilingSwitch--;
418418

419419
let i = index + 1;
420420
if (i < clauses.length && !tsEx.containsStatement(clause.statements, ts.SyntaxKind.BreakStatement)) {

test/integration/lua/conditionals.spec.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Expect, Test, TestCase } from "alsatian";
1+
import { Expect, Test, TestCase, FocusTest } from "alsatian";
22
import * as util from "../../src/util"
33

44
export class LuaConditionalsTests {
@@ -208,5 +208,47 @@ export class LuaConditionalsTests {
208208
Expect(result).toBe(expected);
209209
}
210210

211+
@TestCase(0, 0)
212+
@TestCase(1, 1)
213+
@TestCase(2, 2)
214+
@TestCase(3, -2)
215+
@Test("nestedSwitch")
216+
public nestedSwitch(inp: number, expected: number) {
217+
// Transpile
218+
let lua = util.transpileString(
219+
`let result = -1;
211220
221+
switch (${inp}) {
222+
case 0:
223+
result = 0;
224+
break;
225+
case 1:
226+
switch(${inp}) {
227+
case 0:
228+
result = 0;
229+
break;
230+
case 1:
231+
result = 1;
232+
break;
233+
default:
234+
result = -3;
235+
break;
236+
}
237+
break;
238+
case 2:
239+
result = 2;
240+
break;
241+
default:
242+
result = -2;
243+
break;
244+
}
245+
return result;`
246+
);
247+
248+
// Execute
249+
let result = util.executeLua(lua);
250+
251+
// Assert
252+
Expect(result).toBe(expected);
253+
}
212254
}

0 commit comments

Comments
 (0)