From 318ccf24b501eef627104adbe119983ca5bd486c Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Tue, 9 May 2017 09:08:58 -0700 Subject: [PATCH] importTracker: Require calls are stored in `sourceFile.imports`, no need to search for them --- src/services/importTracker.ts | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/src/services/importTracker.ts b/src/services/importTracker.ts index c94d5cc3143..f85f5961aaa 100644 --- a/src/services/importTracker.ts +++ b/src/services/importTracker.ts @@ -330,7 +330,7 @@ namespace ts.FindAllReferences { /** Calls `action` for each import, re-export, or require() in a file. */ function forEachImport(sourceFile: SourceFile, action: (importStatement: ImporterOrCallExpression, imported: StringLiteral) => void): void { - if (sourceFile.externalModuleIndicator) { + if (sourceFile.externalModuleIndicator || sourceFile.imports !== undefined) { for (const moduleSpecifier of sourceFile.imports) { action(importerFromModuleSpecifier(moduleSpecifier), moduleSpecifier); } @@ -358,27 +358,21 @@ namespace ts.FindAllReferences { } } }); - - if (sourceFile.flags & NodeFlags.JavaScriptFile) { - // Find all 'require()' calls. - sourceFile.forEachChild(function recur(node: Node): void { - if (isRequireCall(node, /*checkArgumentIsStringLiteral*/ true)) { - action(node, node.arguments[0] as StringLiteral); - } else { - node.forEachChild(recur); - } - }); - } } } - function importerFromModuleSpecifier(moduleSpecifier: StringLiteral): Importer { + function importerFromModuleSpecifier(moduleSpecifier: StringLiteral): ImporterOrCallExpression { const decl = moduleSpecifier.parent; - if (decl.kind === SyntaxKind.ImportDeclaration || decl.kind === SyntaxKind.ExportDeclaration) { - return decl as ImportDeclaration | ExportDeclaration; + switch (decl.kind) { + case SyntaxKind.CallExpression: + case SyntaxKind.ImportDeclaration: + case SyntaxKind.ExportDeclaration: + return decl as ImportDeclaration | ExportDeclaration | CallExpression; + case SyntaxKind.ExternalModuleReference: + return (decl as ExternalModuleReference).parent; + default: + Debug.assert(false); } - Debug.assert(decl.kind === SyntaxKind.ExternalModuleReference); - return (decl as ExternalModuleReference).parent; } export interface ImportedSymbol {