Fix sending correct symbol when using commonjs require and destructuring (#43511)

* Fix sending correct symbol when using commonjs require and destructuring

* Check BindingElement
This commit is contained in:
Armando Aguirre 2021-04-13 15:06:34 -07:00 committed by GitHub
parent 6002cff776
commit 5a6a499d0e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 110 additions and 1 deletions

View File

@ -1477,7 +1477,7 @@ namespace ts.FindAllReferences {
if (!hasMatchingMeaning(referenceLocation, state)) return;
const referenceSymbol = state.checker.getSymbolAtLocation(referenceLocation);
let referenceSymbol = state.checker.getSymbolAtLocation(referenceLocation);
if (!referenceSymbol) {
return;
}
@ -1514,6 +1514,11 @@ namespace ts.FindAllReferences {
Debug.assertNever(state.specialSearchKind);
}
// 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;
getImportOrExportReferences(referenceLocation, referenceSymbol, search, state);
}

View File

@ -0,0 +1,89 @@
// === /tests/cases/fourslash/bar.js ===
// const { /*FIND ALL REFS*/[|foo|]: bar } = require('./foo');
// === /tests/cases/fourslash/foo.js ===
// module.exports = {
// [|foo|]: '1'
// };
[
{
"definition": {
"containerKind": "",
"containerName": "",
"fileName": "/tests/cases/fourslash/foo.js",
"kind": "property",
"name": "(property) foo: string",
"textSpan": {
"start": 23,
"length": 3
},
"displayParts": [
{
"text": "(",
"kind": "punctuation"
},
{
"text": "property",
"kind": "text"
},
{
"text": ")",
"kind": "punctuation"
},
{
"text": " ",
"kind": "space"
},
{
"text": "foo",
"kind": "propertyName"
},
{
"text": ":",
"kind": "punctuation"
},
{
"text": " ",
"kind": "space"
},
{
"text": "string",
"kind": "keyword"
}
],
"contextSpan": {
"start": 23,
"length": 8
}
},
"references": [
{
"textSpan": {
"start": 23,
"length": 3
},
"fileName": "/tests/cases/fourslash/foo.js",
"contextSpan": {
"start": 23,
"length": 8
},
"isWriteAccess": true,
"isDefinition": true
},
{
"textSpan": {
"start": 8,
"length": 3
},
"fileName": "/tests/cases/fourslash/bar.js",
"contextSpan": {
"start": 0,
"length": 38
},
"isWriteAccess": false,
"isDefinition": false
}
]
}
]

View File

@ -0,0 +1,15 @@
/// <reference path="fourslash.ts" />
// @allowJs: true
// @noEmit: true
// @checkJs: true
// @Filename: foo.js
//// module.exports = {
//// foo: '1'
//// };
// @Filename: bar.js
//// const { /*1*/foo: bar } = require('./foo');
verify.baselineFindAllReferences("1");