Skip to content

Commit 3c03b59

Browse files
committed
Added test "forin[Array]" & Fixed fail from previous PR
Previous PR changed for in ipairs/pairs to \"pairs\" instead of pairs
1 parent 125fc32 commit 3c03b59

3 files changed

Lines changed: 24 additions & 7 deletions

File tree

dist/Transpiler.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,11 +246,11 @@ var LuaTranspiler = /** @class */ (function () {
246246
var identifier = variable.name;
247247
// Transpile expression
248248
var expression = this.transpileExpression(node.expression);
249-
// Use ipairs for array types, pairs otherwise
250-
var isArray = TSHelper_1.TSHelper.isArrayType(this.checker.getTypeAtLocation(node.expression));
251-
var pairs = isArray ? "ipairs" : "pairs";
249+
if (TSHelper_1.TSHelper.isArrayType(this.checker.getTypeAtLocation(node.expression))) {
250+
throw new TranspileError("Iterating over arrays with 'for in' is not allowed.", node);
251+
}
252252
// Make header
253-
var result = this.indent + ("for " + identifier.escapedText + ", _ in " + pairs + "(" + expression + ") do\n");
253+
var result = this.indent + ("for " + identifier.escapedText + ", _ in pairs(" + expression + ") do\n");
254254
// For body
255255
this.pushIndent();
256256
result += this.transpileStatement(node.statement);
@@ -561,6 +561,8 @@ var LuaTranspiler = /** @class */ (function () {
561561
return "TS_slice(" + caller + ", " + params + ")";
562562
case "splice":
563563
return "TS_splice(" + caller + ", " + params + ")";
564+
case "join":
565+
return "table.concat(" + caller + ", " + params + ")";
564566
default:
565567
throw new TranspileError("Unsupported array function: " + expression.name.escapedText, node);
566568
}

src/Transpiler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ export class LuaTranspiler {
274274
}
275275

276276
// Make header
277-
let result = this.indent + `for ${identifier.escapedText}, _ in "pairs"(${expression}) do\n`;
277+
let result = this.indent + `for ${identifier.escapedText}, _ in pairs(${expression}) do\n`;
278278

279279
// For body
280280
this.pushIndent();

test/integration/lua/loops.spec.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ export class LuaLoopTests {
2626
}
2727

2828
@TestCase({ ['test1']: 0, ['test2']: 1, ['test3']: 2 }, { ['test1']: 1, ['test2']: 2, ['test3']: 3 })
29-
@Test("forin")
30-
public forin<T>(inp: any, expected: any) {
29+
@Test("forin[Object]")
30+
public forinObject<T>(inp: any, expected: any) {
3131
// Transpile
3232
let lua = util.transpileString(
3333
`let objTest = ${JSON.stringify(inp)};
@@ -45,6 +45,21 @@ export class LuaLoopTests {
4545
Expect(deepEqual(JSON.parse(result), expected)).toBe(true);
4646
}
4747

48+
@TestCase([1,2,3])
49+
@Test("forin[Array]")
50+
public forinArray<T>(inp: T[]) {
51+
// Transpile & Assert
52+
Expect(() => {
53+
let lua = util.transpileString(
54+
`let arrTest = [${inp.toString()}];
55+
for (let key in arrTest) {
56+
arrTest[key]++;
57+
}`
58+
, util.dummyTypes.Array
59+
);
60+
}).toThrowError(Error, "Iterating over arrays with 'for in' is not allowed.");
61+
}
62+
4863
@TestCase([0,1,2], [1,2,3])
4964
@Test("forof")
5065
public forof<T>(inp: any, expected: any) {

0 commit comments

Comments
 (0)