From a07d79e5521eda3b67cc798028ae77d1e4f54663 Mon Sep 17 00:00:00 2001 From: Perryvw Date: Sun, 17 Mar 2019 20:24:43 +0100 Subject: [PATCH] Changed position of spread operator declaration in function body --- src/LuaTransformer.ts | 12 ++++++------ test/unit/functions.spec.ts | 12 ++++++++++++ 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/LuaTransformer.ts b/src/LuaTransformer.ts index ab7587e97..763b7ad65 100644 --- a/src/LuaTransformer.ts +++ b/src/LuaTransformer.ts @@ -1169,6 +1169,12 @@ export class LuaTransformer { headerStatements.push(...defaultValueDeclarations); + // Push spread operator here + if (spreadIdentifier) { + const spreadTable = this.wrapInTable(tstl.createDotsLiteral()); + headerStatements.push(tstl.createVariableDeclarationStatement(spreadIdentifier, spreadTable)); + } + // Add object binding patterns let identifierIndex = 0; const bindingPatternDeclarations: tstl.Statement[] = []; @@ -1181,12 +1187,6 @@ export class LuaTransformer { headerStatements.push(...bindingPatternDeclarations); - // Push spread operator here - if (spreadIdentifier) { - const spreadTable = this.wrapInTable(tstl.createDotsLiteral()); - headerStatements.push(tstl.createVariableDeclarationStatement(spreadIdentifier, spreadTable)); - } - const bodyStatements = this.performHoisting(this.transformStatements(body.statements)); const scope = this.popScope(); diff --git a/test/unit/functions.spec.ts b/test/unit/functions.spec.ts index 71b1b96b7..55161623b 100644 --- a/test/unit/functions.spec.ts +++ b/test/unit/functions.spec.ts @@ -511,4 +511,16 @@ export class FunctionTests { }`; Expect(util.transpileAndExecute("return foo;", undefined, undefined, code)).toBe("foo"); } + + @Test("Function rest binding pattern") + public functionRestBindingPattern(): void { + const result = util.transpileAndExecute( + `function bar(foo: string, ...[bar, baz]: [string, string]) { + return bar + baz + foo; + } + return bar("abc", "def", "xyz"); + `); + + Expect(result).toBe("defxyzabc"); + } }