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
8 changes: 8 additions & 0 deletions src/TSHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ const defaultArrayCallMethodNames = new Set<string>([
"join",
]);

const defaultArrayPropertyNames = new Set<string>([
"length",
]);

export class TSHelper {

// Reverse lookup of enum key by value
Expand Down Expand Up @@ -308,4 +312,8 @@ export class TSHelper {
return defaultArrayCallMethodNames.has(methodName);
}

public static isDefaultArrayPropertyName(methodName: string): boolean {
return defaultArrayPropertyNames.has(methodName);
}

}
6 changes: 5 additions & 1 deletion src/Transpiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1387,7 +1387,11 @@ export abstract class LuaTranspiler {
case ts.TypeFlags.StringLiteral:
return this.transpileStringProperty(node);
case ts.TypeFlags.Object:
if (tsHelper.isArrayType(type, this.checker)) {
if (tsHelper.isExplicitArrayType(type, this.checker)) {
return this.transpileArrayProperty(node);
}
if (tsHelper.isArrayType(type, this.checker)
&& tsHelper.isDefaultArrayPropertyName(this.transpileIdentifier(node.name))) {
return this.transpileArrayProperty(node);
}
}
Expand Down
16 changes: 11 additions & 5 deletions test/unit/array.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,22 @@ export class ArrayTests {
Expect(result).toBe(5);
}

@TestCase("firstElement()", 3)
@TestCase("name", "array")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a testcase for length here (since that's what you added)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point

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