From 7a19c22063660da0b2f03fca97178679814de1e7 Mon Sep 17 00:00:00 2001 From: Eli Barzilay Date: Wed, 11 Aug 2021 23:55:17 -0400 Subject: [PATCH] `getReferencesAtLocation`: fix handling of destructoring imports Fixes #45423. --- src/services/findAllReferences.ts | 11 ++- ...ncesJsRequireDestructuring1.baseline.jsonc | 87 +++++++++++++++++++ ...indAllReferencesJsRequireDestructuring1.ts | 13 +++ 3 files changed, 108 insertions(+), 3 deletions(-) create mode 100644 tests/baselines/reference/findAllReferencesJsRequireDestructuring1.baseline.jsonc create mode 100644 tests/cases/fourslash/findAllReferencesJsRequireDestructuring1.ts diff --git a/src/services/findAllReferences.ts b/src/services/findAllReferences.ts index cac241927cf..0d22c83b244 100644 --- a/src/services/findAllReferences.ts +++ b/src/services/findAllReferences.ts @@ -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); } diff --git a/tests/baselines/reference/findAllReferencesJsRequireDestructuring1.baseline.jsonc b/tests/baselines/reference/findAllReferencesJsRequireDestructuring1.baseline.jsonc new file mode 100644 index 00000000000..84abe44e9a6 --- /dev/null +++ b/tests/baselines/reference/findAllReferencesJsRequireDestructuring1.baseline.jsonc @@ -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 + } + ] + } +] \ No newline at end of file diff --git a/tests/cases/fourslash/findAllReferencesJsRequireDestructuring1.ts b/tests/cases/fourslash/findAllReferencesJsRequireDestructuring1.ts new file mode 100644 index 00000000000..a1d6b2f6347 --- /dev/null +++ b/tests/cases/fourslash/findAllReferencesJsRequireDestructuring1.ts @@ -0,0 +1,13 @@ +/// + +// @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");