@@ -3677,7 +3677,13 @@ export function createPackageJsonImportFilter(fromFile: SourceFile, preferences:
3677
3677
) . filter ( p => p . parseable ) ;
3678
3678
3679
3679
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
+ } ;
3681
3687
3682
3688
function moduleSpecifierIsCoveredByPackageJson ( specifier : string ) {
3683
3689
const packageName = getNodeModuleRootSpecifier ( specifier ) ;
@@ -3694,32 +3700,60 @@ export function createPackageJsonImportFilter(fromFile: SourceFile, preferences:
3694
3700
return true ;
3695
3701
}
3696
3702
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
+ }
3701
3711
}
3702
3712
3703
3713
const declaredModuleSpecifier = stripQuotes ( moduleSymbol . getName ( ) ) ;
3704
3714
if ( isAllowedCoreNodeModulesImport ( declaredModuleSpecifier ) ) {
3715
+ ambientModuleCache . set ( moduleSymbol , true ) ;
3705
3716
return true ;
3706
3717
}
3707
3718
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 ;
3710
3731
}
3711
3732
3712
3733
function allowsImportingSourceFile ( sourceFile : SourceFile , moduleSpecifierResolutionHost : ModuleSpecifierResolutionHost ) : boolean {
3713
3734
if ( ! packageJsons . length ) {
3714
3735
return true ;
3715
3736
}
3716
3737
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
+
3717
3748
const moduleSpecifier = getNodeModulesPackageNameFromFileName ( sourceFile . fileName , moduleSpecifierResolutionHost ) ;
3718
3749
if ( ! moduleSpecifier ) {
3750
+ sourceFileCache . set ( sourceFile , true ) ;
3719
3751
return true ;
3720
3752
}
3721
3753
3722
- return moduleSpecifierIsCoveredByPackageJson ( moduleSpecifier ) ;
3754
+ const result = moduleSpecifierIsCoveredByPackageJson ( moduleSpecifier ) ;
3755
+ sourceFileCache . set ( sourceFile , result ) ;
3756
+ return result ;
3723
3757
}
3724
3758
3725
3759
function allowsImportingSpecifier ( moduleSpecifier : string ) {
0 commit comments