Skip to content

Commit 7909433

Browse files
tomblindPerryvw
authored andcommitted
Validation crash fix (#326)
* fix for crash when validating assignments of arrays/tuples * added re-assignment code to tuple return test * added new tuple assignment test and fixed others
1 parent 92de529 commit 7909433

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

src/Transpiler.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1529,13 +1529,23 @@ export abstract class LuaTranspiler {
15291529
}
15301530
}
15311531

1532-
const fromTypeReference = fromType as ts.TypeReference;
1533-
const toTypeReference = toType as ts.TypeReference;
1534-
if (fromTypeReference.typeArguments && toTypeReference.typeArguments) {
1535-
// Recurse into tuples/arrays
1536-
toTypeReference.typeArguments.forEach((t, i) => {
1537-
this.validateFunctionAssignment(node, fromTypeReference.typeArguments[i], t, toName);
1538-
});
1532+
const fromTypeNode = this.checker.typeToTypeNode(fromType);
1533+
const toTypeNode = this.checker.typeToTypeNode(toType);
1534+
1535+
if ((ts.isArrayTypeNode(toTypeNode) || ts.isTupleTypeNode(toTypeNode))
1536+
&& (ts.isArrayTypeNode(fromTypeNode) || ts.isTupleTypeNode(fromTypeNode))) {
1537+
// Recurse into arrays/tuples
1538+
const fromTypeReference = fromType as ts.TypeReference;
1539+
const toTypeReference = toType as ts.TypeReference;
1540+
const count = Math.min(fromTypeReference.typeArguments.length, toTypeReference.typeArguments.length);
1541+
for (let i = 0; i < count; ++i) {
1542+
this.validateFunctionAssignment(
1543+
node,
1544+
fromTypeReference.typeArguments[i],
1545+
toTypeReference.typeArguments[i],
1546+
toName
1547+
);
1548+
}
15391549
}
15401550

15411551
if ((toType.flags & ts.TypeFlags.Object) !== 0

test/unit/assignments.spec.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,19 @@ export class AssignmentTests {
9191
.toThrowError(Error, "Ellipsis destruction is not allowed.");
9292
}
9393

94+
@Test("Tuple Assignment")
95+
public tupleAssignment(): void {
96+
const code = `function abc() { return [1, 2]; };
97+
let t: [number, number] = abc();
98+
return t[0] + t[1];`;
99+
const result = util.transpileAndExecute(code);
100+
Expect(result).toBe(3);
101+
}
102+
94103
@Test("TupleReturn assignment")
95104
public tupleReturnFunction(): void {
96105
const code = `/** @tupleReturn */\n`
97-
+ `declare function abc() { return [1,2,3]; }\n`
106+
+ `declare function abc(): number[]\n`
98107
+ `let [a,b] = abc();`;
99108

100109
const lua = util.transpileString(code);
@@ -104,7 +113,7 @@ export class AssignmentTests {
104113
@Test("TupleReturn Single assignment")
105114
public tupleReturnSingleAssignment(): void {
106115
const code = `/** @tupleReturn */\n`
107-
+ `declare function abc(): [number, string]; }\n`
116+
+ `declare function abc(): [number, string];\n`
108117
+ `let a = abc();`
109118
+ `a = abc();`;
110119

0 commit comments

Comments
 (0)