Skip to content

Commit c0b73bd

Browse files
TheLartiansPerryvw
authored andcommitted
Use declared types (#302)
* update forTypeOrAnySupertype() to use declared type * added test case for accessor using this * enhance test to use generics
1 parent fc41aa3 commit c0b73bd

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

src/TSHelper.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,18 @@ export class TSHelper {
8181
}
8282

8383
// iterate over a type and its bases until the callback returns true.
84-
public static forTypeOrAnySupertype(type: ts.Type, callback: (type: ts.Type) => boolean): boolean {
84+
public static forTypeOrAnySupertype(type: ts.Type, checker: ts.TypeChecker, callback: (type: ts.Type) => boolean):
85+
boolean {
8586
if (callback(type)) {
8687
return true;
8788
}
89+
if (!type.isClassOrInterface() && type.symbol) {
90+
type = checker.getDeclaredTypeOfSymbol(type.symbol);
91+
}
8892
const baseTypes = type.getBaseTypes();
8993
if (baseTypes) {
9094
for (const baseType of baseTypes) {
91-
if (this.forTypeOrAnySupertype(baseType, callback)) {
95+
if (this.forTypeOrAnySupertype(baseType, checker, callback)) {
9296
return true;
9397
}
9498
}
@@ -115,7 +119,7 @@ export class TSHelper {
115119
}
116120

117121
public static isArrayType(type: ts.Type, checker: ts.TypeChecker): boolean {
118-
return this.forTypeOrAnySupertype(type, t => this.isExplicitArrayType(t, checker));
122+
return this.forTypeOrAnySupertype(type, checker, t => this.isExplicitArrayType(t, checker));
119123
}
120124

121125
public static isTupleReturnCall(node: ts.Node, checker: ts.TypeChecker): boolean {
@@ -203,7 +207,7 @@ export class TSHelper {
203207
if (ts.isPropertyAccessExpression(node)) {
204208
const name = node.name.escapedText;
205209
const type = checker.getTypeAtLocation(node.expression);
206-
return this.forTypeOrAnySupertype(type, t => this.hasExplicitGetAccessor(t, name));
210+
return this.forTypeOrAnySupertype(type, checker, t => this.hasExplicitGetAccessor(t, name));
207211
}
208212
return false;
209213
}
@@ -219,7 +223,7 @@ export class TSHelper {
219223
if (ts.isPropertyAccessExpression(node)) {
220224
const name = node.name.escapedText;
221225
const type = checker.getTypeAtLocation(node.expression);
222-
return this.forTypeOrAnySupertype(type, t => this.hasExplicitSetAccessor(t, name));
226+
return this.forTypeOrAnySupertype(type, checker, t => this.hasExplicitSetAccessor(t, name));
223227
}
224228
return false;
225229
}

test/unit/array.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ export class ArrayTests {
4343
public derivedArrayAccess(): void {
4444
const lua = `local arr = {firstElement=function(self) return self[1]; end};`
4545
+ util.transpileString(
46-
`interface CustomArray extends Array<number>{ firstElement():number; };
47-
declare const arr: CustomArray;
46+
`interface CustomArray<T> extends Array<T>{ firstElement():number; };
47+
declare const arr: CustomArray<number>;
4848
arr[0] = 3;
4949
return arr.firstElement();`
5050
);

test/unit/expressions.spec.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,7 @@ export class ExpressionTests {
257257
@TestCase("inst.baseField", 7)
258258
@TestCase("inst.field", 6)
259259
@TestCase("inst.superField", 5)
260+
@TestCase("inst.superBaseField", 4)
260261
@Test("Inherited accessors")
261262
public inheritedAccessors(expression: string, expected: any): void {
262263
const source = `class MyBaseClass {`
@@ -273,6 +274,7 @@ export class ExpressionTests {
273274
+ ` public _superField: number;`
274275
+ ` public get superField(): number { return this._superField + 2; }`
275276
+ ` public set superField(v: number) { this._superField = v; }`
277+
+ ` public get superBaseField() { return this.baseField - 3; }`
276278
+ `}`
277279
+ `var inst = new MySuperClass();`
278280
+ `inst.baseField = 1;`

0 commit comments

Comments
 (0)