getReferencesAtLocation: fix handling of destructoring imports

Fixes #45423.
This commit is contained in:
Eli Barzilay
2021-08-18 14:39:54 -04:00
parent fc07d9dcc6
commit 7a19c22063
3 changed files with 108 additions and 3 deletions
+8 -3
View File
@@ -1523,9 +1523,14 @@ namespace ts.FindAllReferences {
}
// Use the parent symbol if the location is commonjs require syntax on javascript files only.
referenceSymbol = isInJSFile(referenceLocation) && referenceLocation.parent.kind === SyntaxKind.BindingElement && isRequireVariableDeclaration(referenceLocation.parent)
? referenceLocation.parent.symbol
: referenceSymbol;
if (isInJSFile(referenceLocation)
&& referenceLocation.parent.kind === SyntaxKind.BindingElement
&& isRequireVariableDeclaration(referenceLocation.parent)) {
referenceSymbol = referenceLocation.parent.symbol;
// The parent will not have a symbol if it's an ObjectBindingPattern (when destructuring is used). In
// this case, just skip it, since the bound identifiers are not an alias of the import.
if (!referenceSymbol) return;
}
getImportOrExportReferences(referenceLocation, referenceSymbol, search, state);
}
@@ -0,0 +1,87 @@
// === /Y.js ===
// const { /*FIND ALL REFS*/[|x|]: { y } } = require("./X");
// === /X.js ===
// module.exports = { [|x|]: 1 };
[
{
"definition": {
"containerKind": "",
"containerName": "",
"fileName": "/X.js",
"kind": "property",
"name": "(property) x: number",
"textSpan": {
"start": 19,
"length": 1
},
"displayParts": [
{
"text": "(",
"kind": "punctuation"
},
{
"text": "property",
"kind": "text"
},
{
"text": ")",
"kind": "punctuation"
},
{
"text": " ",
"kind": "space"
},
{
"text": "x",
"kind": "propertyName"
},
{
"text": ":",
"kind": "punctuation"
},
{
"text": " ",
"kind": "space"
},
{
"text": "number",
"kind": "keyword"
}
],
"contextSpan": {
"start": 19,
"length": 4
}
},
"references": [
{
"textSpan": {
"start": 19,
"length": 1
},
"fileName": "/X.js",
"contextSpan": {
"start": 19,
"length": 4
},
"isWriteAccess": true,
"isDefinition": true
},
{
"textSpan": {
"start": 8,
"length": 1
},
"fileName": "/Y.js",
"contextSpan": {
"start": 0,
"length": 36
},
"isWriteAccess": false,
"isDefinition": false
}
]
}
]
@@ -0,0 +1,13 @@
/// <reference path="fourslash.ts" />
// @allowJs: true
// @noEmit: true
// @checkJs: true
// @Filename: /X.js
////module.exports = { x: 1 };
// @Filename: /Y.js
////const { /*1*/x: { y } } = require("./X");
verify.baselineFindAllReferences("1");