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

This commit is contained in:
Andrew Branch authored and GitHub committed 2024-08-21 16:43:21 -07:00
1 parent f6ec916313
commit a5eec2485f
16 files changed
+943 -191

No files matched your search

+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;
}
}
}
}
+5 -2
View File
@@ -4866,11 +4866,14 @@ export interface Program extends ScriptReferenceHost {
*/
redirectTargetsMap: MultiMap<Path, string>;
/**
* Whether any (non-external, non-declaration) source files use `node:`-prefixed module specifiers.
* Whether any (non-external, non-declaration) source files use `node:`-prefixed module specifiers
* (except for those that are not available without the prefix).
* `false` indicates that an unprefixed builtin module was seen; `undefined` indicates that no
* builtin modules (or only modules exclusively available with the prefix) were seen.
*
* @internal
*/
readonly usesUriStyleNodeCoreModules: boolean;
readonly usesUriStyleNodeCoreModules: boolean | undefined;
/**
* Map from libFileName to actual resolved location of the lib
* @internal
+80
View File
@@ -11796,3 +11796,83 @@ export function isSideEffectImport(node: Node): boolean {
const ancestor = findAncestor(node, isImportDeclaration);
return !!ancestor && !ancestor.importClause;
}
// require('module').builtinModules.filter(x => !x.startsWith('_'))
const unprefixedNodeCoreModulesList = [
"assert",
"assert/strict",
"async_hooks",
"buffer",
"child_process",
"cluster",
"console",
"constants",
"crypto",
"dgram",
"diagnostics_channel",
"dns",
"dns/promises",
"domain",
"events",
"fs",
"fs/promises",
"http",
"http2",
"https",
"inspector",
"inspector/promises",
"module",
"net",
"os",
"path",
"path/posix",
"path/win32",
"perf_hooks",
"process",
"punycode",
"querystring",
"readline",
"readline/promises",
"repl",
"stream",
"stream/consumers",
"stream/promises",
"stream/web",
"string_decoder",
"sys",
"test/mock_loader",
"timers",
"timers/promises",
"tls",
"trace_events",
"tty",
"url",
"util",
"util/types",
"v8",
"vm",
"wasi",
"worker_threads",
"zlib",
];
/** @internal */
export const unprefixedNodeCoreModules = new Set(unprefixedNodeCoreModulesList);
// await fetch('https://nodejs.org/docs/latest/api/all.json').then(r => r.text()).then(t =>
// new Set(t.match(/(?<=')node:.+?(?=')/g))
// .difference(new Set(require('module').builtinModules.map(x => `node:${x}`))))
/** @internal */
export const exclusivelyPrefixedNodeCoreModules = new Set([
"node:sea",
"node:sqlite",
"node:test",
"node:test/reporters",
]);
/** @internal */
export const nodeCoreModules = new Set([
...unprefixedNodeCoreModulesList,
...unprefixedNodeCoreModulesList.map(name => `node:${name}`),
...exclusivelyPrefixedNodeCoreModules,
]);