Skip to content

Commit e612831

Browse files
elibarzilayBobobUnicorn
authored andcommitted
getReferencesAtLocation: fix handling of destructoring imports
Fixes microsoft#45423.
1 parent a4e2e69 commit e612831

File tree

3 files changed

+108
-3
lines changed

3 files changed

+108
-3
lines changed

src/services/findAllReferences.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1523,9 +1523,14 @@ namespace ts.FindAllReferences {
15231523
}
15241524

15251525
// Use the parent symbol if the location is commonjs require syntax on javascript files only.
1526-
referenceSymbol = isInJSFile(referenceLocation) && referenceLocation.parent.kind === SyntaxKind.BindingElement && isRequireVariableDeclaration(referenceLocation.parent)
1527-
? referenceLocation.parent.symbol
1528-
: referenceSymbol;
1526+
if (isInJSFile(referenceLocation)
1527+
&& referenceLocation.parent.kind === SyntaxKind.BindingElement
1528+
&& isRequireVariableDeclaration(referenceLocation.parent)) {
1529+
referenceSymbol = referenceLocation.parent.symbol;
1530+
// The parent will not have a symbol if it's an ObjectBindingPattern (when destructuring is used). In
1531+
// this case, just skip it, since the bound identifiers are not an alias of the import.
1532+
if (!referenceSymbol) return;
1533+
}
15291534

15301535
getImportOrExportReferences(referenceLocation, referenceSymbol, search, state);
15311536
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
// === /Y.js ===
2+
// const { /*FIND ALL REFS*/[|x|]: { y } } = require("./X");
3+
4+
// === /X.js ===
5+
// module.exports = { [|x|]: 1 };
6+
7+
[
8+
{
9+
"definition": {
10+
"containerKind": "",
11+
"containerName": "",
12+
"fileName": "/X.js",
13+
"kind": "property",
14+
"name": "(property) x: number",
15+
"textSpan": {
16+
"start": 19,
17+
"length": 1
18+
},
19+
"displayParts": [
20+
{
21+
"text": "(",
22+
"kind": "punctuation"
23+
},
24+
{
25+
"text": "property",
26+
"kind": "text"
27+
},
28+
{
29+
"text": ")",
30+
"kind": "punctuation"
31+
},
32+
{
33+
"text": " ",
34+
"kind": "space"
35+
},
36+
{
37+
"text": "x",
38+
"kind": "propertyName"
39+
},
40+
{
41+
"text": ":",
42+
"kind": "punctuation"
43+
},
44+
{
45+
"text": " ",
46+
"kind": "space"
47+
},
48+
{
49+
"text": "number",
50+
"kind": "keyword"
51+
}
52+
],
53+
"contextSpan": {
54+
"start": 19,
55+
"length": 4
56+
}
57+
},
58+
"references": [
59+
{
60+
"textSpan": {
61+
"start": 19,
62+
"length": 1
63+
},
64+
"fileName": "/X.js",
65+
"contextSpan": {
66+
"start": 19,
67+
"length": 4
68+
},
69+
"isWriteAccess": true,
70+
"isDefinition": true
71+
},
72+
{
73+
"textSpan": {
74+
"start": 8,
75+
"length": 1
76+
},
77+
"fileName": "/Y.js",
78+
"contextSpan": {
79+
"start": 0,
80+
"length": 36
81+
},
82+
"isWriteAccess": false,
83+
"isDefinition": false
84+
}
85+
]
86+
}
87+
]
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @allowJs: true
4+
// @noEmit: true
5+
// @checkJs: true
6+
7+
// @Filename: /X.js
8+
////module.exports = { x: 1 };
9+
10+
// @Filename: /Y.js
11+
////const { /*1*/x: { y } } = require("./X");
12+
13+
verify.baselineFindAllReferences("1");

0 commit comments

Comments
 (0)