Skip to content

Commit

Permalink
fix: diagnose correct class name when resolving class member fails (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
HerrCai0907 authored Nov 17, 2023
1 parent 53aab48 commit 5ee17be
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 24 deletions.
48 changes: 24 additions & 24 deletions src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1440,8 +1440,10 @@ export class Resolver extends DiagnosticEmitter {
case ElementKind.InterfacePrototype:
case ElementKind.Class:
case ElementKind.Interface: {
let classLikeTarget = target;
let findBase = false;
do {
let member = target.getMember(propertyName);
let member = classLikeTarget.getMember(propertyName);
if (member) {
if (member.kind == ElementKind.PropertyPrototype) {
let propertyInstance = this.resolveProperty(<PropertyPrototype>member, reportMode);
Expand All @@ -1458,34 +1460,32 @@ export class Resolver extends DiagnosticEmitter {
this.currentElementExpression = null;
return member; // instance FIELD, static GLOBAL, FUNCTION_PROTOTYPE, PROPERTY...
}
// traverse inherited static members on the base prototype if target is a class prototype
if (
target.kind == ElementKind.ClassPrototype ||
target.kind == ElementKind.InterfacePrototype
) {
let classPrototype = <ClassPrototype>target;
let basePrototype = classPrototype.basePrototype;
if (basePrototype) {
target = basePrototype;
} else {
findBase = false;
switch (classLikeTarget.kind) {
case ElementKind.ClassPrototype:
case ElementKind.InterfacePrototype: {
// traverse inherited static members on the base prototype if target is a class prototype
let classPrototype = <ClassPrototype>classLikeTarget;
let basePrototype = classPrototype.basePrototype;
if (basePrototype) {
findBase = true;
classLikeTarget = basePrototype;
}
break;
}
// traverse inherited instance members on the base class if target is a class instance
} else if (
target.kind == ElementKind.Class ||
target.kind == ElementKind.Interface
) {
let classInstance = <Class>target;
let baseInstance = classInstance.base;
if (baseInstance) {
target = baseInstance;
} else {
case ElementKind.Class:
case ElementKind.Interface: {
// traverse inherited instance members on the base class if target is a class instance
let classInstance = <Class>classLikeTarget;
let baseInstance = classInstance.base;
if (baseInstance) {
findBase = true;
classLikeTarget = baseInstance;
}
break;
}
} else {
break;
}
} while (true);
} while (findBase);
break;
}
default: { // enums or other namespace-like elements
Expand Down
9 changes: 9 additions & 0 deletions tests/compiler/class-errors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"asc_flags": [
],
"stderr": [
"TS2304: Cannot find name 'T'.",
"TS2339: Property 'v' does not exist on type 'class-errors/A'.",
"EOF"
]
}
7 changes: 7 additions & 0 deletions tests/compiler/class-errors.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class A {
v: T;
}

new A().v;

ERROR("EOF");

0 comments on commit 5ee17be

Please sign in to comment.