@@ -3663,7 +3663,13 @@ export function createPackageJsonImportFilter(fromFile: SourceFile, preferences:
3663
3663
) . filter ( p => p . parseable ) ;
3664
3664
3665
3665
let usesNodeCoreModules : boolean | undefined ;
3666
- return { allowsImportingAmbientModule, allowsImportingSourceFile, allowsImportingSpecifier } ;
3666
+ let ambientModuleCache : Map < Symbol , boolean > | undefined ;
3667
+ let sourceFileCache : Map < SourceFile , boolean > | undefined ;
3668
+ return {
3669
+ allowsImportingAmbientModule,
3670
+ allowsImportingSourceFile,
3671
+ allowsImportingSpecifier,
3672
+ } ;
3667
3673
3668
3674
function moduleSpecifierIsCoveredByPackageJson ( specifier : string ) {
3669
3675
const packageName = getNodeModuleRootSpecifier ( specifier ) ;
@@ -3680,32 +3686,60 @@ export function createPackageJsonImportFilter(fromFile: SourceFile, preferences:
3680
3686
return true ;
3681
3687
}
3682
3688
3683
- const declaringSourceFile = moduleSymbol . valueDeclaration . getSourceFile ( ) ;
3684
- const declaringNodeModuleName = getNodeModulesPackageNameFromFileName ( declaringSourceFile . fileName , moduleSpecifierResolutionHost ) ;
3685
- if ( typeof declaringNodeModuleName === "undefined" ) {
3686
- return true ;
3689
+ if ( ! ambientModuleCache ) {
3690
+ ambientModuleCache = new Map ( ) ;
3691
+ }
3692
+ else {
3693
+ const cached = ambientModuleCache . get ( moduleSymbol ) ;
3694
+ if ( cached !== undefined ) {
3695
+ return cached ;
3696
+ }
3687
3697
}
3688
3698
3689
3699
const declaredModuleSpecifier = stripQuotes ( moduleSymbol . getName ( ) ) ;
3690
3700
if ( isAllowedCoreNodeModulesImport ( declaredModuleSpecifier ) ) {
3701
+ ambientModuleCache . set ( moduleSymbol , true ) ;
3691
3702
return true ;
3692
3703
}
3693
3704
3694
- return moduleSpecifierIsCoveredByPackageJson ( declaringNodeModuleName )
3695
- || moduleSpecifierIsCoveredByPackageJson ( declaredModuleSpecifier ) ;
3705
+ const declaringSourceFile = moduleSymbol . valueDeclaration . getSourceFile ( ) ;
3706
+ const declaringNodeModuleName = getNodeModulesPackageNameFromFileName ( declaringSourceFile . fileName , moduleSpecifierResolutionHost ) ;
3707
+ if ( typeof declaringNodeModuleName === "undefined" ) {
3708
+ ambientModuleCache . set ( moduleSymbol , true ) ;
3709
+ return true ;
3710
+ }
3711
+
3712
+ const result =
3713
+ moduleSpecifierIsCoveredByPackageJson ( declaringNodeModuleName ) ||
3714
+ moduleSpecifierIsCoveredByPackageJson ( declaredModuleSpecifier ) ;
3715
+ ambientModuleCache . set ( moduleSymbol , result ) ;
3716
+ return result ;
3696
3717
}
3697
3718
3698
3719
function allowsImportingSourceFile ( sourceFile : SourceFile , moduleSpecifierResolutionHost : ModuleSpecifierResolutionHost ) : boolean {
3699
3720
if ( ! packageJsons . length ) {
3700
3721
return true ;
3701
3722
}
3702
3723
3724
+ if ( ! sourceFileCache ) {
3725
+ sourceFileCache = new Map ( ) ;
3726
+ }
3727
+ else {
3728
+ const cached = sourceFileCache . get ( sourceFile ) ;
3729
+ if ( cached !== undefined ) {
3730
+ return cached ;
3731
+ }
3732
+ }
3733
+
3703
3734
const moduleSpecifier = getNodeModulesPackageNameFromFileName ( sourceFile . fileName , moduleSpecifierResolutionHost ) ;
3704
3735
if ( ! moduleSpecifier ) {
3736
+ sourceFileCache . set ( sourceFile , true ) ;
3705
3737
return true ;
3706
3738
}
3707
3739
3708
- return moduleSpecifierIsCoveredByPackageJson ( moduleSpecifier ) ;
3740
+ const result = moduleSpecifierIsCoveredByPackageJson ( moduleSpecifier ) ;
3741
+ sourceFileCache . set ( sourceFile , result ) ;
3742
+ return result ;
3709
3743
}
3710
3744
3711
3745
function allowsImportingSpecifier ( moduleSpecifier : string ) {
0 commit comments