Skip to content

Commit 7d8f6ee

Browse files
authored
Add cache to auto-import package.json filter (#52422)
1 parent 720ec45 commit 7d8f6ee

File tree

2 files changed

+45
-9
lines changed

2 files changed

+45
-9
lines changed

src/compiler/moduleSpecifiers.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,9 @@ function computeModuleSpecifiers(
371371
let redirectPathsSpecifiers: string[] | undefined;
372372
let relativeSpecifiers: string[] | undefined;
373373
for (const modulePath of modulePaths) {
374-
const specifier = tryGetModuleNameAsNodeModule(modulePath, info, importingSourceFile, host, compilerOptions, userPreferences, /*packageNameOnly*/ undefined, options.overrideImportMode);
374+
const specifier = modulePath.isInNodeModules
375+
? tryGetModuleNameAsNodeModule(modulePath, info, importingSourceFile, host, compilerOptions, userPreferences, /*packageNameOnly*/ undefined, options.overrideImportMode)
376+
: undefined;
375377
nodeModulesSpecifiers = append(nodeModulesSpecifiers, specifier);
376378
if (specifier && modulePath.isRedirect) {
377379
// If we got a specifier for a redirect, it was a bare package specifier (e.g. "@foo/bar",

src/services/utilities.ts

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3677,7 +3677,13 @@ export function createPackageJsonImportFilter(fromFile: SourceFile, preferences:
36773677
).filter(p => p.parseable);
36783678

36793679
let usesNodeCoreModules: boolean | undefined;
3680-
return { allowsImportingAmbientModule, allowsImportingSourceFile, allowsImportingSpecifier };
3680+
let ambientModuleCache: Map<Symbol, boolean> | undefined;
3681+
let sourceFileCache: Map<SourceFile, boolean> | undefined;
3682+
return {
3683+
allowsImportingAmbientModule,
3684+
allowsImportingSourceFile,
3685+
allowsImportingSpecifier,
3686+
};
36813687

36823688
function moduleSpecifierIsCoveredByPackageJson(specifier: string) {
36833689
const packageName = getNodeModuleRootSpecifier(specifier);
@@ -3694,32 +3700,60 @@ export function createPackageJsonImportFilter(fromFile: SourceFile, preferences:
36943700
return true;
36953701
}
36963702

3697-
const declaringSourceFile = moduleSymbol.valueDeclaration.getSourceFile();
3698-
const declaringNodeModuleName = getNodeModulesPackageNameFromFileName(declaringSourceFile.fileName, moduleSpecifierResolutionHost);
3699-
if (typeof declaringNodeModuleName === "undefined") {
3700-
return true;
3703+
if (!ambientModuleCache) {
3704+
ambientModuleCache = new Map();
3705+
}
3706+
else {
3707+
const cached = ambientModuleCache.get(moduleSymbol);
3708+
if (cached !== undefined) {
3709+
return cached;
3710+
}
37013711
}
37023712

37033713
const declaredModuleSpecifier = stripQuotes(moduleSymbol.getName());
37043714
if (isAllowedCoreNodeModulesImport(declaredModuleSpecifier)) {
3715+
ambientModuleCache.set(moduleSymbol, true);
37053716
return true;
37063717
}
37073718

3708-
return moduleSpecifierIsCoveredByPackageJson(declaringNodeModuleName)
3709-
|| moduleSpecifierIsCoveredByPackageJson(declaredModuleSpecifier);
3719+
const declaringSourceFile = moduleSymbol.valueDeclaration.getSourceFile();
3720+
const declaringNodeModuleName = getNodeModulesPackageNameFromFileName(declaringSourceFile.fileName, moduleSpecifierResolutionHost);
3721+
if (typeof declaringNodeModuleName === "undefined") {
3722+
ambientModuleCache.set(moduleSymbol, true);
3723+
return true;
3724+
}
3725+
3726+
const result =
3727+
moduleSpecifierIsCoveredByPackageJson(declaringNodeModuleName) ||
3728+
moduleSpecifierIsCoveredByPackageJson(declaredModuleSpecifier);
3729+
ambientModuleCache.set(moduleSymbol, result);
3730+
return result;
37103731
}
37113732

37123733
function allowsImportingSourceFile(sourceFile: SourceFile, moduleSpecifierResolutionHost: ModuleSpecifierResolutionHost): boolean {
37133734
if (!packageJsons.length) {
37143735
return true;
37153736
}
37163737

3738+
if (!sourceFileCache) {
3739+
sourceFileCache = new Map();
3740+
}
3741+
else {
3742+
const cached = sourceFileCache.get(sourceFile);
3743+
if (cached !== undefined) {
3744+
return cached;
3745+
}
3746+
}
3747+
37173748
const moduleSpecifier = getNodeModulesPackageNameFromFileName(sourceFile.fileName, moduleSpecifierResolutionHost);
37183749
if (!moduleSpecifier) {
3750+
sourceFileCache.set(sourceFile, true);
37193751
return true;
37203752
}
37213753

3722-
return moduleSpecifierIsCoveredByPackageJson(moduleSpecifier);
3754+
const result = moduleSpecifierIsCoveredByPackageJson(moduleSpecifier);
3755+
sourceFileCache.set(sourceFile, result);
3756+
return result;
37233757
}
37243758

37253759
function allowsImportingSpecifier(moduleSpecifier: string) {

0 commit comments

Comments
 (0)