Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion src/TSHelper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
import * as ts from "typescript";
import { Decorator, DecoratorKind } from "./Decorator";

const defaultArrayCallMethodNames = new Set<string>([
"concat",
"push",
"reverse",
"shift",
"unshift",
"sort",
"pop",
"forEach",
"indexOf",
"map",
"filter",
"some",
"every",
"slice",
"splice",
"join",
]);

export class TSHelper {

// Reverse lookup of enum key by value
Expand Down Expand Up @@ -74,11 +93,21 @@ export class TSHelper {
&& (typeNode as ts.UnionOrIntersectionTypeNode).types.some(this.isArrayTypeNode));
}

public static isArrayType(type: ts.Type, checker: ts.TypeChecker): boolean {
public static isExplicitArrayType(type: ts.Type, checker: ts.TypeChecker): boolean {
const typeNode = checker.typeToTypeNode(type, undefined, ts.NodeBuilderFlags.InTypeAlias);
return typeNode && this.isArrayTypeNode(typeNode);
}

public static isArrayType(type: ts.Type, checker: ts.TypeChecker): boolean {
const baseTypes = type.getBaseTypes();
if (baseTypes) {
for (const baseType of baseTypes) {
if (this.isExplicitArrayType(baseType, checker)) { return true; }
}
}
return this.isExplicitArrayType(type, checker);
}

public static isTupleReturnCall(node: ts.Node, checker: ts.TypeChecker): boolean {
if (ts.isCallExpression(node)) {
const type = checker.getTypeAtLocation(node.expression);
Expand Down Expand Up @@ -254,4 +283,9 @@ export class TSHelper {
}
return [false, null, null];
}

public static isDefaultArrayCallMethodName(methodName: string): boolean {
return defaultArrayCallMethodNames.has(methodName);
}

}
9 changes: 8 additions & 1 deletion src/Transpiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1203,7 +1203,14 @@ export abstract class LuaTranspiler {

}

if (tsHelper.isArrayType(ownerType, this.checker)) {
// if ownerType is a array, use only supported functions
if (tsHelper.isExplicitArrayType(ownerType, this.checker)) {
return this.transpileArrayCallExpression(node);
}

// if ownerType inherits from an array, use array calls where appropriate
if (tsHelper.isArrayType(ownerType, this.checker)
&& tsHelper.isDefaultArrayCallMethodName(this.transpileIdentifier(node.expression.name))) {
return this.transpileArrayCallExpression(node);
}

Expand Down
13 changes: 13 additions & 0 deletions test/unit/array.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,17 @@ export class ArrayTests {
const result = util.executeLua(lua);
Expect(result).toBe(5);
}

@Test("Derived array access")
public derivedArrayAccess(): void {
const lua = `local arr = {firstElement=function(self) return self[1]; end};`
+ util.transpileString(
`interface CustomArray extends Array<number>{ firstElement():number; };
declare const arr: CustomArray;
arr[0] = 3;
return arr.firstElement();`
);
const result = util.executeLua(lua);
Expect(result).toBe(3);
}
}