Skip to content

Commit 55430c4

Browse files
author
Kanchalai Tanglertsampan
committed
Add boolean flag to not walk the tree if there is no dynamic import
1 parent faaa38d commit 55430c4

File tree

4 files changed

+15
-4
lines changed

4 files changed

+15
-4
lines changed

src/compiler/parser.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3700,6 +3700,7 @@ namespace ts {
37003700
// For example:
37013701
// var foo3 = require("subfolder
37023702
// import * as foo1 from "module-from-node -> we want this import to be a statement rather than import call expression
3703+
sourceFile.possiblyContainDynamicImport = true;
37033704
expression = parseTokenNode<PrimaryExpression>();
37043705
}
37053706
else {

src/compiler/program.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1223,7 +1223,9 @@ namespace ts {
12231223

12241224
for (const node of file.statements) {
12251225
collectModuleReferences(node, /*inAmbientModule*/ false);
1226-
collectImportOrRequireCalls(node);
1226+
if (file.possiblyContainDynamicImport || isJavaScriptFile) {
1227+
collectDynamicImportOrRequireCalls(node);
1228+
}
12271229
}
12281230

12291231
file.imports = imports || emptyArray;
@@ -1285,16 +1287,16 @@ namespace ts {
12851287
}
12861288
}
12871289

1288-
function collectImportOrRequireCalls(node: Node): void {
1289-
if (isJavaScriptFile && isRequireCall(node, /*checkArgumentIsStringLiteral*/true)) {
1290+
function collectDynamicImportOrRequireCalls(node: Node): void {
1291+
if (isRequireCall(node, /*checkArgumentIsStringLiteral*/true)) {
12901292
(imports || (imports = [])).push(<StringLiteral>(<CallExpression>node).arguments[0]);
12911293
}
12921294
// we have to check the argument list has length of 1. We will still have to process these even though we have parsing error.
12931295
else if (isImportCall(node) && node.arguments.length === 1 && node.arguments[0].kind === SyntaxKind.StringLiteral) {
12941296
(imports || (imports = [])).push(<StringLiteral>(<CallExpression>node).arguments[0]);
12951297
}
12961298
else {
1297-
forEachChild(node, collectImportOrRequireCalls);
1299+
forEachChild(node, collectDynamicImportOrRequireCalls);
12981300
}
12991301
}
13001302
}

src/compiler/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2309,6 +2309,13 @@ namespace ts {
23092309
/* @internal */ patternAmbientModules?: PatternAmbientModule[];
23102310
/* @internal */ ambientModuleNames: string[];
23112311
/* @internal */ checkJsDirective: CheckJsDirective | undefined;
2312+
// This flag will be set to true when the parse encounter dynamic import so that post-parsing process of module resolution
2313+
// 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.
2314+
// (hence it is named "possiblyContainDynamicImport").
2315+
// 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.
2316+
// 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.
2317+
// 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.
2318+
/* @internal */ possiblyContainDynamicImport: boolean;
23122319
}
23132320

23142321
export interface Bundle extends Node {

src/services/services.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,7 @@ namespace ts {
507507
private namedDeclarations: Map<Declaration[]>;
508508
public ambientModuleNames: string[];
509509
public checkJsDirective: CheckJsDirective | undefined;
510+
public possiblyContainDynamicImport: boolean;
510511

511512
constructor(kind: SyntaxKind, pos: number, end: number) {
512513
super(kind, pos, end);

0 commit comments

Comments
 (0)