Skip to content

Commit 76cc39e

Browse files
author
Andy
authoredMay 10, 2017
Merge pull request microsoft#15703 from Microsoft/require-calls
importTracker: Require calls are stored in `sourceFile.imports`, no need to search for them
2 parents 3768dae + 318ccf2 commit 76cc39e

File tree

1 file changed

+11
-17
lines changed

1 file changed

+11
-17
lines changed
 

‎src/services/importTracker.ts

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,7 @@ namespace ts.FindAllReferences {
330330

331331
/** Calls `action` for each import, re-export, or require() in a file. */
332332
function forEachImport(sourceFile: SourceFile, action: (importStatement: ImporterOrCallExpression, imported: StringLiteral) => void): void {
333-
if (sourceFile.externalModuleIndicator) {
333+
if (sourceFile.externalModuleIndicator || sourceFile.imports !== undefined) {
334334
for (const moduleSpecifier of sourceFile.imports) {
335335
action(importerFromModuleSpecifier(moduleSpecifier), moduleSpecifier);
336336
}
@@ -358,27 +358,21 @@ namespace ts.FindAllReferences {
358358
}
359359
}
360360
});
361-
362-
if (sourceFile.flags & NodeFlags.JavaScriptFile) {
363-
// Find all 'require()' calls.
364-
sourceFile.forEachChild(function recur(node: Node): void {
365-
if (isRequireCall(node, /*checkArgumentIsStringLiteral*/ true)) {
366-
action(node, node.arguments[0] as StringLiteral);
367-
} else {
368-
node.forEachChild(recur);
369-
}
370-
});
371-
}
372361
}
373362
}
374363

375-
function importerFromModuleSpecifier(moduleSpecifier: StringLiteral): Importer {
364+
function importerFromModuleSpecifier(moduleSpecifier: StringLiteral): ImporterOrCallExpression {
376365
const decl = moduleSpecifier.parent;
377-
if (decl.kind === SyntaxKind.ImportDeclaration || decl.kind === SyntaxKind.ExportDeclaration) {
378-
return decl as ImportDeclaration | ExportDeclaration;
366+
switch (decl.kind) {
367+
case SyntaxKind.CallExpression:
368+
case SyntaxKind.ImportDeclaration:
369+
case SyntaxKind.ExportDeclaration:
370+
return decl as ImportDeclaration | ExportDeclaration | CallExpression;
371+
case SyntaxKind.ExternalModuleReference:
372+
return (decl as ExternalModuleReference).parent;
373+
default:
374+
Debug.assert(false);
379375
}
380-
Debug.assert(decl.kind === SyntaxKind.ExternalModuleReference);
381-
return (decl as ExternalModuleReference).parent;
382376
}
383377

384378
export interface ImportedSymbol {

0 commit comments

Comments
 (0)
Please sign in to comment.