Skip to content

Make isDefinition aware of declaring symbol - #45920

Merged
Nathan Shively-Sanders (sandersn) merged 8 commits into
mainfrom
make-isDefinition-symbol-aware
Sep 22, 2021
Merged

Make isDefinition aware of declaring symbol#45920
Nathan Shively-Sanders (sandersn) merged 8 commits into
mainfrom
make-isDefinition-symbol-aware

Conversation

@sandersn

@sandersn Nathan Shively-Sanders (sandersn) commented Sep 16, 2021

Copy link
Copy Markdown
Member

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.
@sandersn

Copy link
Copy Markdown
Member Author

Matt Bierner (@mjbvz) You're probably interested in this too.

Andrew Casey (@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.)

@mjbvz Matt Bierner (mjbvz) left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@sandersn

Copy link
Copy Markdown
Member Author

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

@sandersn
Nathan Shively-Sanders (sandersn) deleted the make-isDefinition-symbol-aware branch September 22, 2021 20:43
@rchl

Rafał Chłodnicki (rchl) commented Nov 18, 2021

Copy link
Copy Markdown
Contributor

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": "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/../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
Copy Markdown
Contributor

No comments about whether this is intended changed or not?

@amcasey

Copy link
Copy Markdown
Member

@sandersn

Copy link
Copy Markdown
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
Copy Markdown
Contributor

OK, thanks for clarifying.

@microsoft Microsoft (microsoft) locked as resolved and limited conversation to collaborators Oct 21, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

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