Add boolean flag to not walk the tree if there is no dynamic import

This commit is contained in:
Kanchalai Tanglertsampan
2017-04-04 15:43:45 -07:00
parent faaa38d93e
commit 55430c46e6
4 changed files with 15 additions and 4 deletions

View File

@@ -3700,6 +3700,7 @@ namespace ts {
// For example:
// var foo3 = require("subfolder
// import * as foo1 from "module-from-node -> we want this import to be a statement rather than import call expression
sourceFile.possiblyContainDynamicImport = true;
expression = parseTokenNode<PrimaryExpression>();
}
else {

View File

@@ -1223,7 +1223,9 @@ namespace ts {
for (const node of file.statements) {
collectModuleReferences(node, /*inAmbientModule*/ false);
collectImportOrRequireCalls(node);
if (file.possiblyContainDynamicImport || isJavaScriptFile) {
collectDynamicImportOrRequireCalls(node);
}
}
file.imports = imports || emptyArray;
@@ -1285,8 +1287,8 @@ namespace ts {
}
}
function collectImportOrRequireCalls(node: Node): void {
if (isJavaScriptFile && isRequireCall(node, /*checkArgumentIsStringLiteral*/true)) {
function collectDynamicImportOrRequireCalls(node: Node): void {
if (isRequireCall(node, /*checkArgumentIsStringLiteral*/true)) {
(imports || (imports = [])).push(<StringLiteral>(<CallExpression>node).arguments[0]);
}
// we have to check the argument list has length of 1. We will still have to process these even though we have parsing error.
@@ -1294,7 +1296,7 @@ namespace ts {
(imports || (imports = [])).push(<StringLiteral>(<CallExpression>node).arguments[0]);
}
else {
forEachChild(node, collectImportOrRequireCalls);
forEachChild(node, collectDynamicImportOrRequireCalls);
}
}
}

View File

@@ -2309,6 +2309,13 @@ namespace ts {
/* @internal */ patternAmbientModules?: PatternAmbientModule[];
/* @internal */ ambientModuleNames: string[];
/* @internal */ checkJsDirective: CheckJsDirective | undefined;
// This flag will be set to true when the parse encounter dynamic import so that post-parsing process of module resolution
// will not walk the tree if the flag is not set. However, this flag is just a approximation because once it is set, the flag never get reset.
// (hence it is named "possiblyContainDynamicImport").
// During editing, if dynamic import is remove, incremental parsing will *NOT* update this flag. This will then causes walking of the tree during module resolution.
// However, the removal operation should not occur often and in the case of the removal, it is likely that users will add back the import anyway.
// The advantage of this approach is its simplicity. For the case of batch compilation, we garuntee that users won't have to pay the price of walking the tree if dynamic import isn't used.
/* @internal */ possiblyContainDynamicImport: boolean;
}
export interface Bundle extends Node {

View File

@@ -507,6 +507,7 @@ namespace ts {
private namedDeclarations: Map<Declaration[]>;
public ambientModuleNames: string[];
public checkJsDirective: CheckJsDirective | undefined;
public possiblyContainDynamicImport: boolean;
constructor(kind: SyntaxKind, pos: number, end: number) {
super(kind, pos, end);