Skip to content
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

Make isDefinition aware of declaring symbol #45920

Merged
merged 8 commits into from
Sep 22, 2021

Conversation

sandersn
Copy link
Member

@sandersn sandersn commented Sep 16, 2021

After this PR, isDefinition is only true when the declaring symbol is found, and the declaration for the reference's node is one of the symbol's declarations.

This improves the References Codelens, fixing the cases in #42889.
Edit: Previously, I changed isDefinition for commonjs assignment declarations. The current PR now has a simple workaround for this case. I put the old discussion at the bottom.

Remaining work

I still need to update a lot of baselines. However, the change itself is ready to review.

Fixes #42889

Old

One change is with CommonJS aliases:

// @filename: a.js
function f() { }
module.exports.f = f

// @filename: b.js
const { f } = require('./a')
f/**/

Previously module.exports.f = f has isDefinition: true and didn't contribute to f's reference count. Now it has isDefinition: false, and it does.
This is arguable, since it's not a particularly interesting reference, even though it's technically correct. But fixing will involve re-creating the ad-hoc commonjs alias resolution code from the checker. I don't think it's worth it for an edge case like this.

Initial code, haven't fixed any tests yet.
This commit includes a regression for commonjs aliases:

```js
// @filename: a.js
function f() { }
module.exports.f = f

// @filename: b.js
const { f } = require('./a')
f/**/
```

Now says that `f` in b.js has 1 reference --
the alias `module.exports.f = f`. This is not correct (or not exactly
correct), but correctly fixing will involve re-creating the ad-hoc
commonjs alias resolution code from the checker. I don't think it's
worth it for an edge case like this.
@typescript-bot typescript-bot added Author: Team For Milestone Bug PRs that fix a bug with a specific milestone labels Sep 16, 2021
@sandersn
Copy link
Member Author

@mjbvz You're probably interested in this too.

@amcasey I tagged you on the review because this changes isDefinition for VS too. However, I think it's a good change -- basically a fix to an incomplete implementation. The only question is whether the old behaviour is critical to VS in some way. (I doubt it.)

Copy link
Contributor

@mjbvz mjbvz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sandersn
Copy link
Member Author

Looking over the baselines, I can see that the changes are pretty much universally improvements.

@sandersn sandersn merged commit f0fe1b8 into main Sep 22, 2021
@sandersn sandersn deleted the make-isDefinition-symbol-aware branch September 22, 2021 20:43
@rchl
Copy link
Contributor

rchl commented Nov 18, 2021

I've noticed regressions in my tests that tests references returned from the typescript API.

For documents:

  1. foo.ts
export function doStuff(): boolean {
    return true;
}
  1. bar.ts
import { doStuff } from './foo';
doStuff();

getting references for doStuff(); in bar.ts, one of the returned references is:

    {
      "file": "/../bar.ts",
      "start": {
        "line": 1,
        "offset": 10
      },
      "end": {
        "line": 1,
        "offset": 17
      },
      "contextStart": {
        "line": 1,
        "offset": 1
      },
      "contextEnd": {
        "line": 1,
        "offset": 33
      },
      "lineText": "import { doStuff } from './foo';",
      "isWriteAccess": true,
      "isDefinition": true
    },

where isDefinition is true while before 4.5.2 it was false.

Is that an intended change?

It seems to be problematic for the feature that I have that allows findings all callers of given symbol. With that change the import statement is included as a caller which doesn't seem right.

@rchl
Copy link
Contributor

rchl commented Dec 10, 2021

No comments about whether this is intended changed or not?

@amcasey
Copy link
Member

amcasey commented Dec 10, 2021

ping @sandersn

@sandersn
Copy link
Member Author

Yes, that change is intended. An import is not one of the declarations of doStuff in your example. Find-all-callers should be a subset of find-all-references -- [doStuff] is another example of a non-call reference that would have been included even before this change.

@rchl
Copy link
Contributor

rchl commented Dec 11, 2021

OK, thanks for clarifying.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Author: Team For Milestone Bug PRs that fix a bug with a specific milestone
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add way to differentiate references for definitions at requesting location
5 participants