Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/LuaTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2073,14 +2073,14 @@ export class LuaTransformer {
// Transpile body
const body = tstl.createBlock(this.transformLoopBody(statement));

if (tsHelper.isArrayType(this.checker.getTypeAtLocation(statement.expression), this.checker)) {
// Arrays
return this.transformForOfArrayStatement(statement, body);

} else if (tsHelper.isLuaIteratorType(statement.expression, this.checker)) {
if (tsHelper.isLuaIteratorType(statement.expression, this.checker)) {
// LuaIterators
return this.transformForOfLuaIteratorStatement(statement, body);

} else if (tsHelper.isArrayType(this.checker.getTypeAtLocation(statement.expression), this.checker)) {
// Arrays
return this.transformForOfArrayStatement(statement, body);

} else {
// TS Iterables
return this.transformForOfIteratorStatement(statement, body);
Expand Down
21 changes: 21 additions & 0 deletions test/unit/loops.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,27 @@ export class LuaLoopTests
Expect(result).toBe("abc");
}

@Test("forof array lua iterator")
public forofArrayLuaIterator(): void {
const code = `const arr = ["a", "b", "c"];
/** @luaIterator */
interface Iter extends Array<string> {}
function luaIter(): Iter {
let i = 0;
return (() => arr[i++]) as any;
}
let result = "";
for (let e of luaIter()) { result += e; }
return result;`;
const compilerOptions = {
luaLibImport: LuaLibImportKind.Require,
luaTarget: LuaTarget.Lua53,
target: ts.ScriptTarget.ES2015,
};
const result = util.transpileAndExecute(code, compilerOptions);
Expect(result).toBe("abc");
}

@Test("forof lua iterator with existing variable")
public forofLuaIteratorExistingVar(): void {
const code = `const arr = ["a", "b", "c"];
Expand Down