Skip to content

Commit 8515a03

Browse files
authored
Changed position of spread operator declaration in function body (#485)
1 parent 02b9642 commit 8515a03

2 files changed

Lines changed: 18 additions & 6 deletions

File tree

src/LuaTransformer.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,6 +1169,12 @@ export class LuaTransformer {
11691169

11701170
headerStatements.push(...defaultValueDeclarations);
11711171

1172+
// Push spread operator here
1173+
if (spreadIdentifier) {
1174+
const spreadTable = this.wrapInTable(tstl.createDotsLiteral());
1175+
headerStatements.push(tstl.createVariableDeclarationStatement(spreadIdentifier, spreadTable));
1176+
}
1177+
11721178
// Add object binding patterns
11731179
let identifierIndex = 0;
11741180
const bindingPatternDeclarations: tstl.Statement[] = [];
@@ -1181,12 +1187,6 @@ export class LuaTransformer {
11811187

11821188
headerStatements.push(...bindingPatternDeclarations);
11831189

1184-
// Push spread operator here
1185-
if (spreadIdentifier) {
1186-
const spreadTable = this.wrapInTable(tstl.createDotsLiteral());
1187-
headerStatements.push(tstl.createVariableDeclarationStatement(spreadIdentifier, spreadTable));
1188-
}
1189-
11901190
const bodyStatements = this.performHoisting(this.transformStatements(body.statements));
11911191

11921192
const scope = this.popScope();

test/unit/functions.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,4 +511,16 @@ export class FunctionTests {
511511
}`;
512512
Expect(util.transpileAndExecute("return foo;", undefined, undefined, code)).toBe("foo");
513513
}
514+
515+
@Test("Function rest binding pattern")
516+
public functionRestBindingPattern(): void {
517+
const result = util.transpileAndExecute(
518+
`function bar(foo: string, ...[bar, baz]: [string, string]) {
519+
return bar + baz + foo;
520+
}
521+
return bar("abc", "def", "xyz");
522+
`);
523+
524+
Expect(result).toBe("defxyzabc");
525+
}
514526
}

0 commit comments

Comments
 (0)