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
3 changes: 3 additions & 0 deletions src/transformation/visitors/language-extensions/iterable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ function transformForOfMultiIterableStatement(
luaIterator: lua.Expression,
invalidMultiUseDiagnostic: (node: ts.Node) => ts.Diagnostic
): lua.Statement {
context.pushPrecedingStatements();
let identifiers: lua.Identifier[] = [];

if (ts.isVariableDeclarationList(statement.initializer)) {
Expand Down Expand Up @@ -51,6 +52,8 @@ function transformForOfMultiIterableStatement(
identifiers.push(lua.createAnonymousIdentifier());
}

block.statements.unshift(...context.popPrecedingStatements());

return lua.createForInStatement(block, identifiers, [luaIterator], statement);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ exports[`LuaIterable with LuaMultiReturn value type invalid LuaIterable<LuaMulti
"local ____exports = {}
function ____exports.__main(self)
local function testIterable()
local strsArray = {{\\"a1\\", \\"a2\\"}, {\\"b1\\", \\"b2\\"}, {\\"c1\\", \\"c2\\"}}
local strsArray = {{\\"a1\\", {a = \\"a\\"}}, {\\"b1\\", {a = \\"b\\"}}, {\\"c1\\", {a = \\"c\\"}}}
local i = 0
return function()
local ____i_0 = i
Expand All @@ -27,7 +27,7 @@ exports[`LuaIterable with LuaMultiReturn value type invalid LuaIterable<LuaMulti
"local ____exports = {}
function ____exports.__main(self)
local function testIterable()
local strsArray = {{\\"a1\\", \\"a2\\"}, {\\"b1\\", \\"b2\\"}, {\\"c1\\", \\"c2\\"}}
local strsArray = {{\\"a1\\", {a = \\"a\\"}}, {\\"b1\\", {a = \\"b\\"}}, {\\"c1\\", {a = \\"c\\"}}}
local i = 0
return function()
local ____i_0 = i
Expand Down
45 changes: 31 additions & 14 deletions test/unit/language-extensions/iterable.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ describe("LuaIterable with array value type", () => {
test("basic destructuring", () => {
util.testFunction`
${testIterable}
const results: Array<string[]> = [];
const results = [];
for (const [x, y] of testIterable()) {
results.push([x, y]);
}
Expand All @@ -252,7 +252,7 @@ describe("LuaIterable with array value type", () => {
test("destructure with external control variable", () => {
util.testFunction`
${testIterable}
const results: Array<string[]> = [];
const results = []
let x: string, y: string;
for ([x, y] of testIterable()) {
results.push([x, y]);
Expand Down Expand Up @@ -326,8 +326,8 @@ describe("LuaIterable with array value type", () => {

describe("LuaIterable with LuaMultiReturn value type", () => {
const testIterable = `
function testIterable(this: void): LuaIterable<LuaMultiReturn<string[]>> {
const strsArray = [["a1", "a2"], ["b1", "b2"], ["c1", "c2"]];
function testIterable(this: void): LuaIterable<LuaMultiReturn<[string, {a: string}]>> {
const strsArray = [["a1", {a: "a"}], ["b1", {a: "b"}], ["c1", {a: "c"}]];
let i = 0;
return (() => {
const strs = strsArray[i++];
Expand All @@ -338,15 +338,15 @@ describe("LuaIterable with LuaMultiReturn value type", () => {
}
`;
const testResults = [
["a1", "a2"],
["b1", "b2"],
["c1", "c2"],
["a1", { a: "a" }],
["b1", { a: "b" }],
["c1", { a: "c" }],
];

test("basic destructuring", () => {
util.testFunction`
${testIterable}
const results: Array<string[]> = [];
const results = [];
for (const [x, y] of testIterable()) {
results.push([x, y]);
}
Expand All @@ -359,8 +359,8 @@ describe("LuaIterable with LuaMultiReturn value type", () => {
test("destructure with external control variable", () => {
util.testFunction`
${testIterable}
const results: Array<string[]> = [];
let x: string, y: string;
const results = [];
let x: string, y: any;
for ([x, y] of testIterable()) {
results.push([x, y]);
}
Expand All @@ -374,7 +374,7 @@ describe("LuaIterable with LuaMultiReturn value type", () => {
util.testFunction`
${testIterable}
function forward() { return testIterable(); }
const results: Array<string[]> = [];
const results = [];
for (const [x, y] of forward()) {
results.push([x, y]);
}
Expand All @@ -388,7 +388,7 @@ describe("LuaIterable with LuaMultiReturn value type", () => {
util.testFunction`
${testIterable}
function forward() { const iter = testIterable(); return iter; }
const results: Array<string[]> = [];
const results = [];
for (const [x, y] of forward()) {
results.push([x, y]);
}
Expand All @@ -402,7 +402,7 @@ describe("LuaIterable with LuaMultiReturn value type", () => {
util.testFunction`
${testIterable}
const forward = () => testIterable();
const results: Array<string[]> = [];
const results = [];
for (const [x, y] of forward()) {
results.push([x, y]);
}
Expand All @@ -415,7 +415,7 @@ describe("LuaIterable with LuaMultiReturn value type", () => {
test("destructure manual use", () => {
util.testFunction`
${testIterable}
const results: Array<string[]> = [];
const results = [];
const iter = testIterable();
while (true) {
const [x, y] = iter();
Expand All @@ -430,6 +430,23 @@ describe("LuaIterable with LuaMultiReturn value type", () => {
.expectToEqual(testResults);
});

test("nested destructuring", () => {
util.testFunction`
${testIterable}
const results = [];
for (const [x, {a}] of testIterable()) {
results.push([x, a]);
}
return results;
`
.withLanguageExtensions()
.expectToEqual([
["a1", "a"],
["b1", "b"],
["c1", "c"],
]);
});

test.each(["for (const s of testIterable()) {}", "let s; for (s of testIterable()) {}"])(
"invalid LuaIterable<LuaMultiReturn> without destructuring (%p)",
statement => {
Expand Down