Skip to content

Commit bc0d3e1

Browse files
Look for top-level imports/exports before diving into the tree.
1 parent 9672116 commit bc0d3e1

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

src/compiler/parser.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6139,14 +6139,11 @@ namespace ts {
61396139
}
61406140

61416141
function setExternalModuleIndicator(sourceFile: SourceFile) {
6142-
// Usually we'd like to avoid a full tree walk, but it's possible
6143-
// that we have a deeper external module indicator (e.g. `import.meta`,
6144-
// and possibly nested import statements in the future).
6145-
// Ideally the first few statements will be an import/export anyway.
6142+
// Try to use the first top-level import/export when available, then
6143+
// fall back to looking for an 'import.meta' somewhere in the tree if necessary.
61466144
sourceFile.externalModuleIndicator =
6147-
!(sourceFile.flags & NodeFlags.PossiblyContainsImportMeta) ?
6148-
forEach(sourceFile.statements, isAnExternalModuleIndicatorNode) :
6149-
walkTreeForExternalModuleIndicators(sourceFile);
6145+
forEach(sourceFile.statements, isAnExternalModuleIndicatorNode) ||
6146+
getImportMetaIfNecessary(sourceFile);
61506147
}
61516148

61526149
function isAnExternalModuleIndicatorNode(node: Node) {
@@ -6155,13 +6152,22 @@ namespace ts {
61556152
|| node.kind === SyntaxKind.ImportDeclaration
61566153
|| node.kind === SyntaxKind.ExportAssignment
61576154
|| node.kind === SyntaxKind.ExportDeclaration
6158-
|| isMetaProperty(node) && node.keywordToken === SyntaxKind.ImportKeyword && node.name.escapedText === "meta"
61596155
? node
61606156
: undefined;
61616157
}
61626158

6159+
function getImportMetaIfNecessary(sourceFile: SourceFile) {
6160+
return sourceFile.flags & NodeFlags.PossiblyContainsImportMeta ?
6161+
walkTreeForExternalModuleIndicators(sourceFile) :
6162+
undefined;
6163+
}
6164+
61636165
function walkTreeForExternalModuleIndicators(node: Node): Node {
6164-
return isAnExternalModuleIndicatorNode(node) ? node : forEachChild(node, walkTreeForExternalModuleIndicators);
6166+
return isImportMeta(node) ? node : forEachChild(node, walkTreeForExternalModuleIndicators);
6167+
}
6168+
6169+
function isImportMeta(node: Node): boolean {
6170+
return isMetaProperty(node) && node.keywordToken === SyntaxKind.ImportKeyword && node.name.escapedText === "meta";
61656171
}
61666172

61676173
const enum ParsingContext {

src/compiler/types.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2564,7 +2564,11 @@ namespace ts {
25642564
languageVersion: ScriptTarget;
25652565
/* @internal */ scriptKind: ScriptKind;
25662566

2567-
// The first node that causes this file to be an external module
2567+
/**
2568+
* The first "most obvious" node that makes a file an external module.
2569+
* This is intended to be the first top-level import/export,
2570+
* but could be arbitrarily nested (e.g. `import.meta`).
2571+
*/
25682572
/* @internal */ externalModuleIndicator: Node;
25692573
// The first node that causes this file to be a CommonJS module
25702574
/* @internal */ commonJsModuleIndicator: Node;

0 commit comments

Comments
 (0)