Skip to content

Commit d190530

Browse files
author
Andy
authored
Merge pull request #15303 from Microsoft/importTracker_npe
Fix null error in importTracker: VariableDeclaration might not have a VariableStatement ancestor
2 parents 0e08e6e + 4ff180d commit d190530

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

src/compiler/utilities.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1870,7 +1870,7 @@ namespace ts {
18701870
}
18711871
}
18721872

1873-
export function getAncestor(node: Node | undefined, kind: SyntaxKind): Node {
1873+
export function getAncestor(node: Node | undefined, kind: SyntaxKind): Node | undefined {
18741874
while (node) {
18751875
if (node.kind === kind) {
18761876
return node;

src/services/importTracker.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -387,6 +387,7 @@ namespace ts.FindAllReferences {
387387
symbol: Symbol;
388388
exportInfo: ExportInfo;
389389
}
390+
390391
/**
391392
* Given a local reference, we might notice that it's an import/export and recursively search for references of that.
392393
* If at an import, look locally for the symbol it imports.
@@ -398,7 +399,7 @@ namespace ts.FindAllReferences {
398399
return comingFromExport ? getExport() : getExport() || getImport();
399400

400401
function getExport(): ExportedSymbol | ImportedSymbol | undefined {
401-
const { parent } = node;
402+
const parent = node.parent!;
402403
if (symbol.flags & SymbolFlags.Export) {
403404
if (parent.kind === SyntaxKind.PropertyAccessExpression) {
404405
// When accessing an export of a JS module, there's no alias. The symbol will still be flagged as an export even though we're at the use.
@@ -414,8 +415,8 @@ namespace ts.FindAllReferences {
414415
}
415416
}
416417
else {
417-
const exportNode = parent.kind === SyntaxKind.VariableDeclaration ? getAncestor(parent, SyntaxKind.VariableStatement) : parent;
418-
if (hasModifier(exportNode, ModifierFlags.Export)) {
418+
const exportNode = getExportNode(parent);
419+
if (exportNode && hasModifier(exportNode, ModifierFlags.Export)) {
419420
if (exportNode.kind === SyntaxKind.ImportEqualsDeclaration && (exportNode as ImportEqualsDeclaration).moduleReference === node) {
420421
// 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.
421422
if (comingFromExport) {
@@ -492,6 +493,16 @@ namespace ts.FindAllReferences {
492493
}
493494
}
494495

496+
// If a reference is a variable declaration, the exported node would be the variable statement.
497+
function getExportNode(parent: Node): Node | undefined {
498+
if (parent.kind === SyntaxKind.VariableDeclaration) {
499+
const p = parent as ts.VariableDeclaration;
500+
return p.parent.kind === ts.SyntaxKind.CatchClause ? undefined : p.parent.parent.kind === SyntaxKind.VariableStatement ? p.parent.parent : undefined;
501+
} else {
502+
return parent;
503+
}
504+
}
505+
495506
function isNodeImport(node: Node): { isNamedImport: boolean } | undefined {
496507
const { parent } = node;
497508
switch (parent.kind) {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
////try { }
4+
////catch ([|{| "isWriteAccess": true, "isDefinition": true |}err|]) {
5+
//// [|err|];
6+
////}
7+
8+
verify.singleReferenceGroup("var err: any");

0 commit comments

Comments
 (0)