Skip to content

Commit d75cdab

Browse files
tomblindPerryvw
authored andcommitted
Array union fix (#243)
* Updated check for array types to include unions and intersections that include an array type. * fix for detecting arrays/tuples in aliases * added tests
1 parent c6159e1 commit d75cdab

File tree

3 files changed

+77
-2
lines changed

3 files changed

+77
-2
lines changed

src/TSHelper.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,16 @@ export class TSHelper {
6666
|| (type.flags & ts.TypeFlags.StringLiteral) !== 0;
6767
}
6868

69+
public static isArrayTypeNode(typeNode: ts.TypeNode): boolean {
70+
return typeNode.kind === ts.SyntaxKind.ArrayType
71+
|| typeNode.kind === ts.SyntaxKind.TupleType
72+
|| ((typeNode.kind === ts.SyntaxKind.UnionType || typeNode.kind === ts.SyntaxKind.IntersectionType)
73+
&& (typeNode as ts.UnionOrIntersectionTypeNode).types.some(this.isArrayTypeNode));
74+
}
75+
6976
public static isArrayType(type: ts.Type, checker: ts.TypeChecker): boolean {
70-
const typeNode = checker.typeToTypeNode(type);
71-
return typeNode && (typeNode.kind === ts.SyntaxKind.ArrayType || typeNode.kind === ts.SyntaxKind.TupleType);
77+
const typeNode = checker.typeToTypeNode(type, undefined, ts.NodeBuilderFlags.InTypeAlias);
78+
return typeNode && this.isArrayTypeNode(typeNode);
7279
}
7380

7481
public static isTupleReturnCall(node: ts.Node, checker: ts.TypeChecker): boolean {

test/unit/array.spec.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import { Expect, Test, TestCase } from "alsatian";
2+
import * as util from "../src/util";
3+
4+
export class ArrayTests {
5+
@Test("Array access")
6+
public arrayAccess(): void {
7+
const lua = util.transpileString(
8+
`const arr: number[] = [3,5,1];
9+
return arr[1];`
10+
);
11+
const result = util.executeLua(lua);
12+
Expect(result).toBe(5);
13+
}
14+
15+
@Test("Array union access")
16+
public arrayUnionAccess(): void {
17+
const lua = util.transpileString(
18+
`function makeArray(): number[] | string[] { return [3,5,1]; }
19+
const arr = makeArray();
20+
return arr[1];`
21+
);
22+
const result = util.executeLua(lua);
23+
Expect(result).toBe(5);
24+
}
25+
26+
@Test("Array intersection access")
27+
public arrayIntersectionAccess(): void {
28+
const lua = util.transpileString(
29+
`type I = number[] & {foo: string};
30+
function makeArray(): I {
31+
let t = [3,5,1];
32+
(t as I).foo = "bar";
33+
return (t as I);
34+
}
35+
const arr = makeArray();
36+
return arr[1];`
37+
);
38+
const result = util.executeLua(lua);
39+
Expect(result).toBe(5);
40+
}
41+
}

test/unit/tuples.spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,33 @@ export class TupleTests {
5151
Expect(result).toBe(5);
5252
}
5353

54+
@Test("Tuple union access")
55+
public tupleUnionAccess(): void {
56+
const lua = util.transpileString(
57+
`function makeTuple(): [number, number, number] | [string, string, string] { return [3,5,1]; }
58+
const tuple = makeTuple();
59+
return tuple[1];`
60+
);
61+
const result = util.executeLua(lua);
62+
Expect(result).toBe(5);
63+
}
64+
65+
@Test("Tuple intersection access")
66+
public tupleIntersectionAccess(): void {
67+
const lua = util.transpileString(
68+
`type I = [number, number, number] & {foo: string};
69+
function makeTuple(): I {
70+
let t = [3,5,1];
71+
(t as I).foo = "bar";
72+
return (t as I);
73+
}
74+
const tuple = makeTuple();
75+
return tuple[1];`
76+
);
77+
const result = util.executeLua(lua);
78+
Expect(result).toBe(5);
79+
}
80+
5481
@Test("Tuple Destruct")
5582
public tupleDestruct(): void {
5683
// Transpile

0 commit comments

Comments
 (0)