From 75cce0893122258e0ff489e2916869ec0e14cd28 Mon Sep 17 00:00:00 2001 From: Tom Date: Mon, 15 Oct 2018 08:24:44 -0600 Subject: [PATCH 1/3] Updated check for array types to include unions and intersections that include an array type. --- src/TSHelper.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/TSHelper.ts b/src/TSHelper.ts index 9bc655566..13b4eb332 100644 --- a/src/TSHelper.ts +++ b/src/TSHelper.ts @@ -66,9 +66,16 @@ export class TSHelper { || (type.flags & ts.TypeFlags.StringLiteral) !== 0; } + public static isArrayTypeNode(typeNode: ts.TypeNode): boolean { + return typeNode.kind === ts.SyntaxKind.ArrayType + || typeNode.kind === ts.SyntaxKind.TupleType + || ((typeNode.kind === ts.SyntaxKind.UnionType || typeNode.kind === ts.SyntaxKind.IntersectionType) + && (typeNode as ts.UnionOrIntersectionTypeNode).types.some(this.isArrayTypeNode)); + } + public static isArrayType(type: ts.Type, checker: ts.TypeChecker): boolean { const typeNode = checker.typeToTypeNode(type); - return typeNode && (typeNode.kind === ts.SyntaxKind.ArrayType || typeNode.kind === ts.SyntaxKind.TupleType); + return typeNode && this.isArrayTypeNode(typeNode); } public static isTupleReturnCall(node: ts.Node, checker: ts.TypeChecker): boolean { From 528d557ee9e903c704b405f76b96d5e365959118 Mon Sep 17 00:00:00 2001 From: Tom Date: Mon, 15 Oct 2018 09:06:16 -0600 Subject: [PATCH 2/3] fix for detecting arrays/tuples in aliases --- src/TSHelper.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TSHelper.ts b/src/TSHelper.ts index 13b4eb332..26f95345e 100644 --- a/src/TSHelper.ts +++ b/src/TSHelper.ts @@ -74,7 +74,7 @@ export class TSHelper { } public static isArrayType(type: ts.Type, checker: ts.TypeChecker): boolean { - const typeNode = checker.typeToTypeNode(type); + const typeNode = checker.typeToTypeNode(type, undefined, ts.NodeBuilderFlags.InTypeAlias); return typeNode && this.isArrayTypeNode(typeNode); } From beb8cce311cc716af727bd645bb44695a472cc86 Mon Sep 17 00:00:00 2001 From: Tom Date: Mon, 15 Oct 2018 09:22:38 -0600 Subject: [PATCH 3/3] added tests --- test/unit/array.spec.ts | 41 ++++++++++++++++++++++++++++++++++++++++ test/unit/tuples.spec.ts | 27 ++++++++++++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 test/unit/array.spec.ts diff --git a/test/unit/array.spec.ts b/test/unit/array.spec.ts new file mode 100644 index 000000000..bca94494a --- /dev/null +++ b/test/unit/array.spec.ts @@ -0,0 +1,41 @@ +import { Expect, Test, TestCase } from "alsatian"; +import * as util from "../src/util"; + +export class ArrayTests { + @Test("Array access") + public arrayAccess(): void { + const lua = util.transpileString( + `const arr: number[] = [3,5,1]; + return arr[1];` + ); + const result = util.executeLua(lua); + Expect(result).toBe(5); + } + + @Test("Array union access") + public arrayUnionAccess(): void { + const lua = util.transpileString( + `function makeArray(): number[] | string[] { return [3,5,1]; } + const arr = makeArray(); + return arr[1];` + ); + const result = util.executeLua(lua); + Expect(result).toBe(5); + } + + @Test("Array intersection access") + public arrayIntersectionAccess(): void { + const lua = util.transpileString( + `type I = number[] & {foo: string}; + function makeArray(): I { + let t = [3,5,1]; + (t as I).foo = "bar"; + return (t as I); + } + const arr = makeArray(); + return arr[1];` + ); + const result = util.executeLua(lua); + Expect(result).toBe(5); + } +} diff --git a/test/unit/tuples.spec.ts b/test/unit/tuples.spec.ts index 8f0e383df..067776bd5 100644 --- a/test/unit/tuples.spec.ts +++ b/test/unit/tuples.spec.ts @@ -51,6 +51,33 @@ export class TupleTests { Expect(result).toBe(5); } + @Test("Tuple union access") + public tupleUnionAccess(): void { + const lua = util.transpileString( + `function makeTuple(): [number, number, number] | [string, string, string] { return [3,5,1]; } + const tuple = makeTuple(); + return tuple[1];` + ); + const result = util.executeLua(lua); + Expect(result).toBe(5); + } + + @Test("Tuple intersection access") + public tupleIntersectionAccess(): void { + const lua = util.transpileString( + `type I = [number, number, number] & {foo: string}; + function makeTuple(): I { + let t = [3,5,1]; + (t as I).foo = "bar"; + return (t as I); + } + const tuple = makeTuple(); + return tuple[1];` + ); + const result = util.executeLua(lua); + Expect(result).toBe(5); + } + @Test("Tuple Destruct") public tupleDestruct(): void { // Transpile