Simplify handling of node:-prefixed modules in auto-imports (#59702)

This commit is contained in:
Andrew Branch
2024-08-21 16:43:21 -07:00
committed by GitHub
parent f6ec916313
commit a5eec2485f
16 changed files with 943 additions and 191 deletions
+11 -2
View File
@@ -75,6 +75,7 @@ import {
ensureTrailingDirectorySeparator,
equateStringsCaseInsensitive,
equateStringsCaseSensitive,
exclusivelyPrefixedNodeCoreModules,
explainIfFileIsRedirectAndImpliedFormat,
ExportAssignment,
ExportDeclaration,
@@ -323,6 +324,7 @@ import {
TypeChecker,
typeDirectiveIsEqualTo,
TypeReferenceDirectiveResolutionCache,
unprefixedNodeCoreModules,
VariableDeclaration,
VariableStatement,
Version,
@@ -1758,7 +1760,7 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
let sourceFileToPackageName = new Map<Path, string>();
// Key is a file name. Value is the (non-empty, or undefined) list of files that redirect to it.
let redirectTargetsMap = createMultiMap<Path, string>();
let usesUriStyleNodeCoreModules = false;
let usesUriStyleNodeCoreModules: boolean | undefined;
/**
* map with
@@ -3499,7 +3501,14 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
setParentRecursive(node, /*incremental*/ false); // we need parent data on imports before the program is fully bound, so we ensure it's set here
imports = append(imports, moduleNameExpr);
if (!usesUriStyleNodeCoreModules && currentNodeModulesDepth === 0 && !file.isDeclarationFile) {
usesUriStyleNodeCoreModules = startsWith(moduleNameExpr.text, "node:");
if (startsWith(moduleNameExpr.text, "node:") && !exclusivelyPrefixedNodeCoreModules.has(moduleNameExpr.text)) {
// Presence of `node:` prefix takes precedence over unprefixed node core modules
usesUriStyleNodeCoreModules = true;
}
else if (usesUriStyleNodeCoreModules === undefined && unprefixedNodeCoreModules.has(moduleNameExpr.text)) {
// Avoid `unprefixedNodeCoreModules.has` for every import
usesUriStyleNodeCoreModules = false;
}
}
}
}