Fix null error in importTracker: VariableDeclaration might not have a VariableStatement ancestor

This commit is contained in:
Andy Hanson 2017-04-21 07:53:09 -07:00
parent 8534b430be
commit 4ff180d814
3 changed files with 23 additions and 4 deletions

View File

@ -1870,7 +1870,7 @@ namespace ts {
}
}
export function getAncestor(node: Node | undefined, kind: SyntaxKind): Node {
export function getAncestor(node: Node | undefined, kind: SyntaxKind): Node | undefined {
while (node) {
if (node.kind === kind) {
return node;

View File

@ -387,6 +387,7 @@ namespace ts.FindAllReferences {
symbol: Symbol;
exportInfo: ExportInfo;
}
/**
* Given a local reference, we might notice that it's an import/export and recursively search for references of that.
* If at an import, look locally for the symbol it imports.
@ -398,7 +399,7 @@ namespace ts.FindAllReferences {
return comingFromExport ? getExport() : getExport() || getImport();
function getExport(): ExportedSymbol | ImportedSymbol | undefined {
const { parent } = node;
const parent = node.parent!;
if (symbol.flags & SymbolFlags.Export) {
if (parent.kind === SyntaxKind.PropertyAccessExpression) {
// 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 {
}
}
else {
const exportNode = parent.kind === SyntaxKind.VariableDeclaration ? getAncestor(parent, SyntaxKind.VariableStatement) : parent;
if (hasModifier(exportNode, ModifierFlags.Export)) {
const exportNode = getExportNode(parent);
if (exportNode && hasModifier(exportNode, ModifierFlags.Export)) {
if (exportNode.kind === SyntaxKind.ImportEqualsDeclaration && (exportNode as ImportEqualsDeclaration).moduleReference === node) {
// 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.
if (comingFromExport) {
@ -492,6 +493,16 @@ namespace ts.FindAllReferences {
}
}
// If a reference is a variable declaration, the exported node would be the variable statement.
function getExportNode(parent: Node): Node | undefined {
if (parent.kind === SyntaxKind.VariableDeclaration) {
const p = parent as ts.VariableDeclaration;
return p.parent.kind === ts.SyntaxKind.CatchClause ? undefined : p.parent.parent.kind === SyntaxKind.VariableStatement ? p.parent.parent : undefined;
} else {
return parent;
}
}
function isNodeImport(node: Node): { isNamedImport: boolean } | undefined {
const { parent } = node;
switch (parent.kind) {

View File

@ -0,0 +1,8 @@
/// <reference path="fourslash.ts" />
////try { }
////catch ([|{| "isWriteAccess": true, "isDefinition": true |}err|]) {
//// [|err|];
////}
verify.singleReferenceGroup("var err: any");