Skip to content

Commit 25eeb9d

Browse files
authored
Fixed for loops with expressions (#370)
fixes #367
1 parent 22098f1 commit 25eeb9d

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/LuaTransformer.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1144,9 +1144,13 @@ export class LuaTransformer {
11441144
const result: tstl.Statement[] = [];
11451145

11461146
if (statement.initializer) {
1147-
for (const variableDeclaration of (statement.initializer as ts.VariableDeclarationList).declarations) {
1148-
// local initializer = value
1149-
result.push(...this.transformVariableDeclaration(variableDeclaration));
1147+
if (ts.isVariableDeclarationList(statement.initializer)) {
1148+
for (const variableDeclaration of statement.initializer.declarations) {
1149+
// local initializer = value
1150+
result.push(...this.transformVariableDeclaration(variableDeclaration));
1151+
}
1152+
} else {
1153+
result.push(this.transformExpressionStatement(statement.initializer));
11501154
}
11511155
}
11521156

test/unit/loops.spec.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,22 @@ export class LuaLoopTests
103103
Expect(result).toBe(JSON.stringify(expected));
104104
}
105105

106+
@TestCase([0, 1, 2, 3], [1, 2, 3, 4])
107+
@Test("for with expression")
108+
public forWithExpression(inp: number[], expected: number[]): void
109+
{
110+
const result = util.transpileAndExecute(
111+
`let arrTest = ${JSON.stringify(inp)};
112+
let i: number;
113+
for (i = 0 * 1; i < arrTest.length; ++i) {
114+
arrTest[i] = arrTest[i] + 1;
115+
}
116+
return JSONStringify(arrTest);`
117+
);
118+
// Assert
119+
Expect(result).toBe(JSON.stringify(expected));
120+
}
121+
106122
@TestCase([0, 1, 2, 3, 4], [0, 0, 2, 0, 4])
107123
@Test("for with continue")
108124
public forWithContinue(inp: number[], expected: number[]): void

0 commit comments

Comments
 (0)