Skip to content

Commit a11be62

Browse files
TheLartiansPerryvw
authored andcommitted
allow non-default properties with derived array types (#308)
* allow non-default properties with derived array types * added length test case * remove FocusTest
1 parent a61f841 commit a11be62

File tree

3 files changed

+24
-6
lines changed

3 files changed

+24
-6
lines changed

src/TSHelper.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ const defaultArrayCallMethodNames = new Set<string>([
2020
"join",
2121
]);
2222

23+
const defaultArrayPropertyNames = new Set<string>([
24+
"length",
25+
]);
26+
2327
export class TSHelper {
2428

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

315+
public static isDefaultArrayPropertyName(methodName: string): boolean {
316+
return defaultArrayPropertyNames.has(methodName);
317+
}
318+
311319
}

src/Transpiler.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1387,7 +1387,11 @@ export abstract class LuaTranspiler {
13871387
case ts.TypeFlags.StringLiteral:
13881388
return this.transpileStringProperty(node);
13891389
case ts.TypeFlags.Object:
1390-
if (tsHelper.isArrayType(type, this.checker)) {
1390+
if (tsHelper.isExplicitArrayType(type, this.checker)) {
1391+
return this.transpileArrayProperty(node);
1392+
}
1393+
if (tsHelper.isArrayType(type, this.checker)
1394+
&& tsHelper.isDefaultArrayPropertyName(this.transpileIdentifier(node.name))) {
13911395
return this.transpileArrayProperty(node);
13921396
}
13931397
}

test/unit/array.spec.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,22 @@ export class ArrayTests {
3939
Expect(result).toBe(5);
4040
}
4141

42+
@TestCase("firstElement()", 3)
43+
@TestCase("name", "array")
44+
@TestCase("length", 1)
4245
@Test("Derived array access")
43-
public derivedArrayAccess(): void {
44-
const lua = `local arr = {firstElement=function(self) return self[1]; end};`
46+
public derivedArrayAccess(member: string, expected: any): void {
47+
const lua = `local arr = {name="array", firstElement=function(self) return self[1]; end};`
4548
+ util.transpileString(
46-
`interface CustomArray<T> extends Array<T>{ firstElement():number; };
49+
`interface CustomArray<T> extends Array<T>{
50+
name:string,
51+
firstElement():number;
52+
};
4753
declare const arr: CustomArray<number>;
4854
arr[0] = 3;
49-
return arr.firstElement();`
55+
return arr.${member};`
5056
);
5157
const result = util.executeLua(lua);
52-
Expect(result).toBe(3);
58+
Expect(result).toBe(expected);
5359
}
5460
}

0 commit comments

Comments
 (0)