Skip to content

Commit 6cf30fb

Browse files
author
Andy
authored
Fix bug in importTracker: getExportNode must verify that we are on the LHS of a VariableDeclaration (#17205)
1 parent 555776e commit 6cf30fb

File tree

2 files changed

+21
-3
lines changed

2 files changed

+21
-3
lines changed

src/services/importTracker.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -453,7 +453,7 @@ namespace ts.FindAllReferences {
453453
}
454454
}
455455
else {
456-
const exportNode = getExportNode(parent);
456+
const exportNode = getExportNode(parent, node);
457457
if (exportNode && hasModifier(exportNode, ModifierFlags.Export)) {
458458
if (isImportEqualsDeclaration(exportNode) && exportNode.moduleReference === node) {
459459
// We're at `Y` in `export import X = Y`. This is not the exported symbol, the left-hand-side is. So treat this as an import statement.
@@ -558,10 +558,11 @@ namespace ts.FindAllReferences {
558558

559559
// If a reference is a class expression, the exported node would be its parent.
560560
// If a reference is a variable declaration, the exported node would be the variable statement.
561-
function getExportNode(parent: Node): Node | undefined {
561+
function getExportNode(parent: Node, node: Node): Node | undefined {
562562
if (parent.kind === SyntaxKind.VariableDeclaration) {
563563
const p = parent as ts.VariableDeclaration;
564-
return p.parent.kind === ts.SyntaxKind.CatchClause ? undefined : p.parent.parent.kind === SyntaxKind.VariableStatement ? p.parent.parent : undefined;
564+
return p.name !== node ? undefined :
565+
p.parent.kind === ts.SyntaxKind.CatchClause ? undefined : p.parent.parent.kind === SyntaxKind.VariableStatement ? p.parent.parent : undefined;
565566
}
566567
else {
567568
return parent;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
// @Filename: /a.ts
4+
////class [|{| "isWriteAccess": true, "isDefinition": true |}C|] {}
5+
////export const [|{| "isWriteAccess": true, "isDefinition": true |}D|] = [|C|];
6+
7+
// @Filename: /b.ts
8+
////import { [|{| "isWriteAccess": true, "isDefinition": true |}D|] } from "./a";
9+
10+
const [C0, D0, C1, D1] = test.ranges();
11+
12+
verify.singleReferenceGroup("class C", [C0, C1]);
13+
14+
const d0Group = { definition: "const D: typeof C", ranges: [D0] };
15+
const d1Group = { definition: "import D", ranges: [D1] };
16+
verify.referenceGroups(D0, [d0Group, d1Group]);
17+
verify.referenceGroups(D1, [d1Group, d0Group]);

0 commit comments

Comments
 (0)