Skip to content

Commit 761cd14

Browse files
authored
declaring nested functions as local and cleaned up a few small things (#335)
1 parent 4dc3753 commit 761cd14

4 files changed

Lines changed: 9 additions & 6 deletions

File tree

src/LuaPrinter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ export class LuaPrinter {
308308
paramterArr.push(this.printDotsLiteral(expression.dots));
309309
}
310310

311-
let result = this.indent(`function (${paramterArr.join(", ")})\n`);
311+
let result = `function (${paramterArr.join(", ")})\n`;
312312
this.pushIndent();
313313
result += this.printBlock(expression.body);
314314
this.popIndent();

src/LuaTransformer.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -694,7 +694,7 @@ export class LuaTransformer {
694694
// Push spread operator here
695695
if (spreadIdentifier) {
696696
const spreadTable = this.wrapInTable(tstl.createDotsLiteral());
697-
headerStatements.push(this.createLocalOrGlobalDeclaration(spreadIdentifier, spreadTable));
697+
headerStatements.push(tstl.createVariableDeclarationStatement(spreadIdentifier, spreadTable));
698698
}
699699

700700
const bodyStatements = this.transformStatements(body.statements);
@@ -2316,7 +2316,7 @@ export class LuaTransformer {
23162316
//(function() local ____TS_self = context; return ____TS_self[argument](parameters); end)()
23172317
const argument = this.transformExpression(node.expression.argumentExpression);
23182318
const selfIdentifier = tstl.createIdentifier("____TS_self");
2319-
const selfAssignment = this.createLocalOrGlobalDeclaration(selfIdentifier, context);
2319+
const selfAssignment = tstl.createVariableDeclarationStatement(selfIdentifier, context);
23202320
const index = tstl.createTableIndexExpression(selfIdentifier, argument);
23212321
const callExpression = tstl.createCallExpression(index, parameters);
23222322
return this.createImmediatelyInvokedFunctionExpression([selfAssignment], callExpression, node);
@@ -2895,7 +2895,10 @@ export class LuaTransformer {
28952895
tsOriginal?: ts.Node
28962896
): tstl.Statement
28972897
{
2898-
if (this.isModule || this.currentNamespace) {
2898+
if (this.isModule
2899+
|| this.currentNamespace
2900+
|| (tsOriginal && tsHelper.findFirstNodeAbove(tsOriginal, ts.isFunctionLike))
2901+
) {
28992902
return tstl.createVariableDeclarationStatement(lhs, rhs, parent, tsOriginal);
29002903
} else {
29012904
return tstl.createAssignmentStatement(lhs, rhs, parent, tsOriginal);
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
varargsFunction = function (a, ...)
2-
b = ({...});
2+
local b = ({...});
33
end;

test/translation/lua/methodRestArguments.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ end;
1010
MyClass.constructor = function (self)
1111
end;
1212
MyClass.varargsFunction = function (self, a, ...)
13-
b = ({...});
13+
local b = ({...});
1414
end;

0 commit comments

Comments
 (0)