Skip to content

Commit bc073bb

Browse files
Janne252Perryvw
authored andcommitted
Fix variable destructuring compatibility issue with lua 5.1 (#158)
* Fix variable destructuring compatibility issue for lua 5.1 * Remove deprecated assignmentDestructuring test files in favor of version-specific tests * fix Lua 5.3 VM compatibility issue with running Lua 5.1 code * Removed duplicate tests, renamed tests to match the test case * Fix style & coding conventions to match the project * Remove unnecessary backport (generated lualib no longer contains varible destruturing)
1 parent 0d4c162 commit bc073bb

File tree

6 files changed

+49
-7
lines changed

6 files changed

+49
-7
lines changed

src/Transpiler.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,6 +1286,14 @@ export abstract class LuaTranspiler {
12861286
return result;
12871287
}
12881288

1289+
// Implemented in 5.1 and overridden in 5.2 (and onwards)
1290+
public transpileVariableDestructuring(value: string): string {
1291+
throw new TranspileError(
1292+
`transpileVariableDestructuring must be implemented!`,
1293+
null
1294+
);
1295+
}
1296+
12891297
public transpileVariableDeclaration(node: ts.VariableDeclaration): string {
12901298
if (ts.isIdentifier(node.name)) {
12911299
// Find variable identifier
@@ -1298,20 +1306,20 @@ export abstract class LuaTranspiler {
12981306
}
12991307
} else if (ts.isArrayBindingPattern(node.name)) {
13001308
// Destructuring type
1301-
const value = this.transpileExpression(node.initializer);
13021309

13031310
// Disallow ellipsis destruction
13041311
if (node.name.elements.some(elem => !ts.isBindingElement(elem) || elem.dotDotDotToken !== undefined)) {
13051312
throw new TranspileError(`Ellipsis destruction is not allowed.`, node);
13061313
}
13071314

13081315
const vars = node.name.elements.map(e => this.transpileArrayBindingElement(e)).join(",");
1316+
const value = this.transpileExpression(node.initializer);
13091317

13101318
// Don't unpack TupleReturn decorated functions
13111319
if (tsHelper.isTupleReturnCall(node.initializer, this.checker)) {
13121320
return `local ${vars}=${value}\n`;
13131321
} else {
1314-
return `local ${vars}=table.unpack(${value})\n`;
1322+
return `local ${vars}=${this.transpileVariableDestructuring(value)}\n`;
13151323
}
13161324
} else {
13171325
throw new TranspileError(

src/targets/Transpiler.51.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,8 @@ import { TSHelper as tsHelper } from "../TSHelper";
44
import * as ts from "typescript";
55

66
export class LuaTranspiler51 extends LuaTranspiler {
7-
7+
/** @override */
8+
public transpileVariableDestructuring(value: string): string {
9+
return `unpack(${value})`;
10+
}
811
}

src/targets/Transpiler.52.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,9 @@ export class LuaTranspiler52 extends LuaTranspiler51 {
5353
return `bit32.arshift(${lhs},${rhs})`;
5454
}
5555
}
56+
57+
/** @override */
58+
public transpileVariableDestructuring(value: string): string {
59+
return `table.unpack(${value})`;
60+
}
5661
}

test/translation/lua/assignmentDestructing.lua

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/translation/ts/assignmentDestructing.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { Expect, Test, TestCase } from "alsatian";
2+
import { LuaTarget } from "../../src/Transpiler";
3+
import * as util from "../src/util";
4+
5+
export class AssignmentDestructuringTests {
6+
7+
private readonly assignmentDestruturingTs = `
8+
declare function myFunc(): [number, string];
9+
let [a, b] = myFunc();`;
10+
11+
@Test("Assignment destructuring [5.1]")
12+
public assignmentDestructuring51() {
13+
// Transpile
14+
const lua = util.transpileString(
15+
this.assignmentDestruturingTs, {luaTarget: LuaTarget.Lua51, luaLibImport: "none"}
16+
);
17+
// Assert
18+
Expect(lua).toBe(`local a,b=unpack(myFunc())`);
19+
}
20+
21+
@Test("Assignment destructuring [5.2]")
22+
public tupleDestructing52() {
23+
// Transpile
24+
const lua = util.transpileString(
25+
this.assignmentDestruturingTs, {luaTarget: LuaTarget.Lua52, luaLibImport: "none"}
26+
);
27+
// Assert
28+
Expect(lua).toBe(`local a,b=table.unpack(myFunc())`);
29+
}
30+
}

0 commit comments

Comments
 (0)