Skip to content

Commit e4b718b

Browse files
committed
Wrapped tuple return calls assigned to single variable
1 parent a9d4355 commit e4b718b

3 files changed

Lines changed: 35 additions & 11 deletions

File tree

src/TSHelper.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@ export class TSHelper {
8080
&& this.hasCustomDecorator(type, checker, "!Phantom");
8181
}
8282

83+
public static isTupleReturnCall(node: ts.Node, checker: ts.TypeChecker): boolean {
84+
if (ts.isCallExpression(node)) {
85+
const type = checker.getTypeAtLocation(node.expression);
86+
return this.isTupleReturnFunction(type, checker);
87+
} else {
88+
return false;
89+
}
90+
}
91+
8392
public static isTupleReturnFunction(type: ts.Type, checker: ts.TypeChecker): boolean {
8493
return type.symbol
8594
&& ((type.symbol.flags & ts.SymbolFlags.Function) !== 0

src/Transpiler.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -798,10 +798,19 @@ export class LuaTranspiler {
798798
result = `${lhs}<=${rhs}`;
799799
break;
800800
case ts.SyntaxKind.EqualsToken:
801+
let assignmentValue = rhs;
802+
// If the right-hand side of the equation is a tuple return method
803+
// and the left-hand side is not a destructing statement, wrap the
804+
// rhs in { }.
805+
if (ts.isIdentifier(node.left) && tsHelper.isTupleReturnCall(node.right, this.checker)) {
806+
assignmentValue = `{ ${rhs} }`;
807+
}
808+
801809
if (tsHelper.hasSetAccessor(node.left, this.checker)) {
802-
return this.transpileSetAccessor(node.left as ts.PropertyAccessExpression, rhs);
810+
return this.transpileSetAccessor(node.left as ts.PropertyAccessExpression, assignmentValue);
803811
}
804-
result = `${lhs}=${rhs}`;
812+
813+
result = `${lhs}=${assignmentValue}`;
805814
break;
806815
case ts.SyntaxKind.EqualsEqualsToken:
807816
case ts.SyntaxKind.EqualsEqualsEqualsToken:
@@ -1176,7 +1185,15 @@ export class LuaTranspiler {
11761185
const identifier = node.name;
11771186
if (node.initializer) {
11781187
const value = this.transpileExpression(node.initializer);
1179-
return `local ${identifier.escapedText} = ${value}\n`;
1188+
1189+
// If the right-hand side of the equation is a tuple return method
1190+
// and the left-hand side is not a destructing statement, wrap the
1191+
// rhs in { }.
1192+
if (tsHelper.isTupleReturnCall(node.initializer, this.checker)) {
1193+
return `local ${identifier.escapedText} = { ${value} }\n`;
1194+
} else {
1195+
return `local ${identifier.escapedText} = ${value}\n`;
1196+
}
11801197
} else {
11811198
return `local ${identifier.escapedText} = nil\n`;
11821199
}
@@ -1194,10 +1211,7 @@ export class LuaTranspiler {
11941211
).escapedText).join(",");
11951212

11961213
// Don't unpack TupleReturn decorated functions
1197-
if (ts.isCallExpression(node.initializer)
1198-
&& tsHelper.isTupleReturnFunction(this.checker.getTypeAtLocation(node.initializer.expression),
1199-
this.checker)
1200-
) {
1214+
if (tsHelper.isTupleReturnCall(node.initializer, this.checker)) {
12011215
return `local ${vars}=${value}\n`;
12021216
} else {
12031217
return `local ${vars}=table.unpack(${value})\n`;

test/unit/assignments.spec.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Expect, Test, TestCase, FocusTest } from "alsatian";
1+
import { Expect, Test, TestCase } from "alsatian";
22
import { TranspileError } from "../../src/Transpiler";
33

44
import * as util from "../src/util";
@@ -87,11 +87,12 @@ export class AssignmentTests {
8787
@Test("TupleReturn Single assignment")
8888
public tupleReturnSingleAssignment() {
8989
const code = `/** !TupleReturn */\n`
90-
+ `declare function abc() { return [1,2,3]; }\n`
91-
+ `let a = abc();`;
90+
+ `declare function abc(): [number, string]; }\n`
91+
+ `let a = abc();`
92+
+ `a = abc();`;
9293

9394
const lua = util.transpileString(code);
94-
Expect(lua).toBe("local a = abc()");
95+
Expect(lua).toBe("local a = { abc() }\n\na={ abc() }");
9596
}
9697

9798
@Test("TupleReturn interface assignment")

0 commit comments

Comments
 (0)