microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
100.59k stars 12.44k forks source link

Find all references on overridden subclass method includes references to the parent class #37878

Open mjbvz opened 4 years ago

mjbvz commented 4 years ago

https://github.com/microsoft/vscode/issues/93654

TypeScript Version: 3.9.0-dev.20200409

Search Terms:

Code For the TS

class Foo {
  bar() { }
}

class SubFoo extends Foo {
  bar() { }
}

new SubFoo().bar();
  1. Run find all references on bar in class SubFoo

Expected behavior: A reference to new SubFoo().bar(); is returned

Actual behavior: Two references are returned, including one to the parent class's bar

mjbvz commented 4 years ago

This seems tricky. Obviously you can write something like:

const a: Foo = new SubFoo();
a.bar();

Which would call SubFoo.bar at runtime, yet should probably count as a reference to Foo.bar?

Moving this issue upstream for more feedback

DanielRosenwasser commented 4 years ago

I'm pretty strongly in the camp that that this behavior is correct, but I guess I'm willing to hear more if there's a compelling justification.

pushkine commented 4 years ago

I'm pretty strongly in the camp that that this behavior is correct, but I guess I'm willing to hear more if there's a compelling justification.

I think that the original report example is pretty compelling Code_2020-03-28_14-41-45 Code_2020-03-28_14-49-59

mjbvz commented 4 years ago

The references code lens is implemented with find all references so it's the same problem:

const x: Array = new X()
x.filter(); // Should this be counted as a reference to `X.filter`? 
starball5 commented 1 year ago

Related on Stack Overflow: How can I see all references of a JS/TS derived class' method override without references to the base class method or other overrides in VS Code?.

ranshemt commented 1 year ago

Since this issue is now a discussion, I would like to say that I agree with @mjbvz.

Here's an example to justify:

In a typescript NestJS project, there are many services. Each service is a class. Let's say you have a class BaseService that implements some common logic, which you can override if needed. I have ServiceX which extends BaseService and overrides one of the methods there.

I wish to find all the references of to the overridden method in ServiceX. With current behavior, I will simply get a list of all the places that reference the function of all the derived classes - which might be a huge list of references. While all I want is just a very small subset.