Skip to content

fix: diagnose correct class name when resolving class member fails #2799

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 17, 2023
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
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");