Skip to content

Commit badd012

Browse files
authored
@luaIterator on array types (#479)
* enabling use of @luaIterator on Array types * removing focustest
1 parent d870f00 commit badd012

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

src/LuaTransformer.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2073,14 +2073,14 @@ export class LuaTransformer {
20732073
// Transpile body
20742074
const body = tstl.createBlock(this.transformLoopBody(statement));
20752075

2076-
if (tsHelper.isArrayType(this.checker.getTypeAtLocation(statement.expression), this.checker)) {
2077-
// Arrays
2078-
return this.transformForOfArrayStatement(statement, body);
2079-
2080-
} else if (tsHelper.isLuaIteratorType(statement.expression, this.checker)) {
2076+
if (tsHelper.isLuaIteratorType(statement.expression, this.checker)) {
20812077
// LuaIterators
20822078
return this.transformForOfLuaIteratorStatement(statement, body);
20832079

2080+
} else if (tsHelper.isArrayType(this.checker.getTypeAtLocation(statement.expression), this.checker)) {
2081+
// Arrays
2082+
return this.transformForOfArrayStatement(statement, body);
2083+
20842084
} else {
20852085
// TS Iterables
20862086
return this.transformForOfIteratorStatement(statement, body);

test/unit/loops.spec.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,27 @@ export class LuaLoopTests
542542
Expect(result).toBe("abc");
543543
}
544544

545+
@Test("forof array lua iterator")
546+
public forofArrayLuaIterator(): void {
547+
const code = `const arr = ["a", "b", "c"];
548+
/** @luaIterator */
549+
interface Iter extends Array<string> {}
550+
function luaIter(): Iter {
551+
let i = 0;
552+
return (() => arr[i++]) as any;
553+
}
554+
let result = "";
555+
for (let e of luaIter()) { result += e; }
556+
return result;`;
557+
const compilerOptions = {
558+
luaLibImport: LuaLibImportKind.Require,
559+
luaTarget: LuaTarget.Lua53,
560+
target: ts.ScriptTarget.ES2015,
561+
};
562+
const result = util.transpileAndExecute(code, compilerOptions);
563+
Expect(result).toBe("abc");
564+
}
565+
545566
@Test("forof lua iterator with existing variable")
546567
public forofLuaIteratorExistingVar(): void {
547568
const code = `const arr = ["a", "b", "c"];

0 commit comments

Comments
 (0)