Skip to content

Commit 4a09dff

Browse files
committed
Fixed major bug with vararg/spread operator
1 parent abbdd03 commit 4a09dff

3 files changed

Lines changed: 26 additions & 3 deletions

File tree

src/Transpiler.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1151,14 +1151,33 @@ export class LuaTranspiler {
11511151

11521152
// Build parameter string
11531153
const paramNames: string[] = [];
1154-
parameters.forEach((param) => {
1155-
paramNames.push((param.name as ts.Identifier).escapedText as string);
1156-
});
1154+
1155+
let spreadIdentifier = "";
1156+
1157+
// Only push parameter name to paramName array if it isn't a spread parameter
1158+
for (const param of parameters) {
1159+
const paramName = (param.name as ts.Identifier).escapedText as string;
1160+
1161+
// This parameter is a spread parameter (...param)
1162+
if (!param.dotDotDotToken) {
1163+
paramNames.push(paramName);
1164+
} else {
1165+
spreadIdentifier = paramName;
1166+
// Push the spread operator into the paramNames array
1167+
paramNames.push("...");
1168+
}
1169+
}
11571170

11581171
// Build function header
11591172
result += this.indent + this.accessPrefix(node) + `function ${methodName}(${paramNames.join(",")})\n`;
11601173

11611174
this.pushIndent();
1175+
1176+
// Push spread operator here
1177+
if (spreadIdentifier !== "") {
1178+
result += ` local ${spreadIdentifier} = { ... }\n`;
1179+
}
1180+
11621181
result += this.transpileBlock(body);
11631182
this.popIndent();
11641183

test/translation/lua/varargs.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function varargsFunction(a,...)
2+
local b = { ... }
3+
end

test/translation/ts/varargs.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
function varargsFunction(a: string, ...b: string[]): void {}

0 commit comments

Comments
 (0)