@@ -1175,7 +1175,7 @@ export function getReferencedFileLocation(program: Program, ref: ReferencedFile)
1175
1175
switch ( kind ) {
1176
1176
case FileIncludeKind . Import :
1177
1177
const importLiteral = getModuleNameStringLiteralAt ( file , index ) ;
1178
- packageId = program . resolvedModules ?. get ( file . path ) ?. get ( importLiteral . text , getModeForResolutionAtIndex ( file , index ) ) ?. resolvedModule ?. packageId ;
1178
+ packageId = program . getResolvedModule ( file , importLiteral . text , getModeForResolutionAtIndex ( file , index ) ) ?. resolvedModule ?. packageId ;
1179
1179
if ( importLiteral . pos === - 1 ) return { file, packageId, text : importLiteral . text } ;
1180
1180
pos = skipTrivia ( file . text , importLiteral . pos ) ;
1181
1181
end = importLiteral . end ;
@@ -1185,7 +1185,7 @@ export function getReferencedFileLocation(program: Program, ref: ReferencedFile)
1185
1185
break ;
1186
1186
case FileIncludeKind . TypeReferenceDirective :
1187
1187
( { pos, end, resolutionMode } = file . typeReferenceDirectives [ index ] ) ;
1188
- packageId = program . resolvedTypeReferenceDirectiveNames ?. get ( file . path ) ?. get ( toFileNameLowerCase ( file . typeReferenceDirectives [ index ] . fileName ) , resolutionMode || file . impliedNodeFormat ) ?. resolvedTypeReferenceDirective ?. packageId ;
1188
+ packageId = program . getResolvedTypeReferenceDirective ( file , toFileNameLowerCase ( file . typeReferenceDirectives [ index ] . fileName ) , resolutionMode || file . impliedNodeFormat ) ?. resolvedTypeReferenceDirective ?. packageId ;
1189
1189
break ;
1190
1190
case FileIncludeKind . LibReferenceDirective :
1191
1191
( { pos, end } = file . libReferenceDirectives [ index ] ) ;
@@ -1886,6 +1886,10 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
1886
1886
resolvedModules,
1887
1887
resolvedTypeReferenceDirectiveNames,
1888
1888
resolvedLibReferences,
1889
+ getResolvedModule,
1890
+ getResolvedTypeReferenceDirective,
1891
+ forEachResolvedModule,
1892
+ forEachResolvedTypeReferenceDirective,
1889
1893
getCurrentPackagesMap : ( ) => packageMap ,
1890
1894
typesPackageExists,
1891
1895
packageBundlesTypes,
@@ -1935,18 +1939,47 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
1935
1939
1936
1940
return program ;
1937
1941
1942
+ function getResolvedModule ( file : SourceFile , moduleName : string , mode : ResolutionMode ) {
1943
+ return resolvedModules ?. get ( file . path ) ?. get ( moduleName , mode ) ;
1944
+ }
1945
+
1946
+ function getResolvedTypeReferenceDirective ( file : SourceFile , typeDirectiveName : string , mode : ResolutionMode ) {
1947
+ return resolvedTypeReferenceDirectiveNames ?. get ( file . path ) ?. get ( typeDirectiveName , mode ) ;
1948
+ }
1949
+
1950
+ function forEachResolvedModule (
1951
+ callback : ( resolution : ResolvedModuleWithFailedLookupLocations , moduleName : string , mode : ResolutionMode , filePath : Path ) => void ,
1952
+ file ?: SourceFile ,
1953
+ ) {
1954
+ forEachResolution ( resolvedModules , callback , file ) ;
1955
+ }
1956
+
1957
+ function forEachResolvedTypeReferenceDirective (
1958
+ callback : ( resolution : ResolvedTypeReferenceDirectiveWithFailedLookupLocations , moduleName : string , mode : ResolutionMode , filePath : Path ) => void ,
1959
+ file ?: SourceFile ,
1960
+ ) : void {
1961
+ forEachResolution ( resolvedTypeReferenceDirectiveNames , callback , file ) ;
1962
+ }
1963
+
1964
+ function forEachResolution < T > (
1965
+ resolutionCache : Map < Path , ModeAwareCache < T > > | undefined ,
1966
+ callback : ( resolution : T , moduleName : string , mode : ResolutionMode , filePath : Path ) => void ,
1967
+ file : SourceFile | undefined ,
1968
+ ) {
1969
+ if ( file ) resolutionCache ?. get ( file . path ) ?. forEach ( ( resolution , name , mode ) => callback ( resolution , name , mode , file . path ) ) ;
1970
+ else resolutionCache ?. forEach ( ( resolutions , filePath ) => resolutions . forEach ( ( resolution , name , mode ) => callback ( resolution , name , mode , filePath ) ) ) ;
1971
+ }
1972
+
1938
1973
function getPackagesMap ( ) {
1939
1974
if ( packageMap ) return packageMap ;
1940
1975
packageMap = new Map ( ) ;
1941
1976
// A package name maps to true when we detect it has .d.ts files.
1942
1977
// This is useful as an approximation of whether a package bundles its own types.
1943
1978
// Note: we only look at files already found by module resolution,
1944
1979
// so there may be files we did not consider.
1945
- files . forEach ( sf =>
1946
- resolvedModules ?. get ( sf . path ) ?. forEach ( ( { resolvedModule } ) => {
1947
- if ( resolvedModule ?. packageId ) packageMap ! . set ( resolvedModule . packageId . name , resolvedModule . extension === Extension . Dts || ! ! packageMap ! . get ( resolvedModule . packageId . name ) ) ;
1948
- } )
1949
- ) ;
1980
+ forEachResolvedModule ( ( { resolvedModule } ) => {
1981
+ if ( resolvedModule ?. packageId ) packageMap ! . set ( resolvedModule . packageId . name , resolvedModule . extension === Extension . Dts || ! ! packageMap ! . get ( resolvedModule . packageId . name ) ) ;
1982
+ } ) ;
1950
1983
return packageMap ;
1951
1984
}
1952
1985
@@ -2119,7 +2152,7 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
2119
2152
// If the source file is unchanged and doesnt have invalidated resolution, reuse the module resolutions
2120
2153
if ( file === oldSourceFile && ! hasInvalidatedResolutions ( file . path ) ) {
2121
2154
const mode = getModeForUsageLocation ( file , moduleName ) ;
2122
- const oldResolution = oldProgram ?. resolvedModules ?. get ( file . path ) ?. get ( moduleName . text , mode ) ;
2155
+ const oldResolution = oldProgram ?. getResolvedModule ( file , moduleName . text , mode ) ;
2123
2156
if ( oldResolution ?. resolvedModule ) {
2124
2157
if ( isTraceEnabled ( options , host ) ) {
2125
2158
trace (
@@ -2187,7 +2220,7 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
2187
2220
// If we change our policy of rechecking failed lookups on each program create,
2188
2221
// we should adjust the value returned here.
2189
2222
function moduleNameResolvesToAmbientModuleInNonModifiedFile ( moduleName : StringLiteralLike ) : boolean {
2190
- const resolutionToFile = oldProgram ?. resolvedModules ?. get ( file . path ) ?. get ( moduleName . text , getModeForUsageLocation ( file , moduleName ) ) ?. resolvedModule ;
2223
+ const resolutionToFile = oldProgram ?. getResolvedModule ( file , moduleName . text , getModeForUsageLocation ( file , moduleName ) ) ?. resolvedModule ;
2191
2224
const resolvedFile = resolutionToFile && oldProgram ! . getSourceFile ( resolutionToFile . resolvedFileName ) ;
2192
2225
if ( resolutionToFile && resolvedFile ) {
2193
2226
// In the old program, we resolved to an ambient module that was in the same
@@ -2234,7 +2267,9 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
2234
2267
if ( canReuseResolutions ) {
2235
2268
const typeDirectiveName = getTypeReferenceResolutionName ( entry ) ;
2236
2269
const mode = getModeForFileReference ( entry , containingSourceFile ?. impliedNodeFormat ) ;
2237
- const oldResolution = ( ! isString ( containingFile ) ? oldProgram ?. resolvedTypeReferenceDirectiveNames ?. get ( containingFile . path ) : oldProgram ?. getAutomaticTypeDirectiveResolutions ( ) ) ?. get ( typeDirectiveName , mode ) ;
2270
+ const oldResolution = ! isString ( containingFile ) ?
2271
+ oldProgram ?. getResolvedTypeReferenceDirective ( containingFile , typeDirectiveName , mode ) :
2272
+ oldProgram ?. getAutomaticTypeDirectiveResolutions ( ) ?. get ( typeDirectiveName , mode ) ;
2238
2273
if ( oldResolution ?. resolvedTypeReferenceDirective ) {
2239
2274
if ( isTraceEnabled ( options , host ) ) {
2240
2275
trace (
@@ -2476,22 +2511,29 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
2476
2511
const moduleNames = getModuleNames ( newSourceFile ) ;
2477
2512
const resolutions = resolveModuleNamesReusingOldState ( moduleNames , newSourceFile ) ;
2478
2513
( resolvedModulesProcessing ??= new Map ( ) ) . set ( newSourceFile . path , resolutions ) ;
2479
- const oldResolutions = oldProgram . resolvedModules ?. get ( newSourceFile . path ) ;
2480
2514
// ensure that module resolution results are still correct
2481
- const resolutionsChanged = hasChangesInResolutions ( moduleNames , newSourceFile , resolutions , oldResolutions , moduleResolutionIsEqualTo , moduleResolutionNameAndModeGetter ) ;
2515
+ const resolutionsChanged = hasChangesInResolutions (
2516
+ moduleNames ,
2517
+ newSourceFile ,
2518
+ resolutions ,
2519
+ ( name , mode ) => oldProgram ! . getResolvedModule ( newSourceFile , name , mode ) ,
2520
+ moduleResolutionIsEqualTo ,
2521
+ moduleResolutionNameAndModeGetter ,
2522
+ ) ;
2482
2523
if ( resolutionsChanged ) structureIsReused = StructureIsReused . SafeModules ;
2483
2524
const typesReferenceDirectives = newSourceFile . typeReferenceDirectives ;
2484
2525
const typeReferenceResolutions = resolveTypeReferenceDirectiveNamesReusingOldState ( typesReferenceDirectives , newSourceFile ) ;
2485
2526
( resolvedTypeReferenceDirectiveNamesProcessing ??= new Map ( ) ) . set ( newSourceFile . path , typeReferenceResolutions ) ;
2486
2527
// ensure that types resolutions are still correct
2487
- const oldTypeResolutions = oldProgram . resolvedTypeReferenceDirectiveNames ?. get ( newSourceFile . path ) ;
2488
- const typeReferenceResolutionsChanged = hasChangesInResolutions ( typesReferenceDirectives , newSourceFile , typeReferenceResolutions , oldTypeResolutions , typeDirectiveIsEqualTo , typeReferenceResolutionNameAndModeGetter ) ;
2489
- if ( typeReferenceResolutionsChanged ) {
2490
- structureIsReused = StructureIsReused . SafeModules ;
2491
- }
2492
- else if ( oldTypeResolutions ) {
2493
- ( resolvedTypeReferenceDirectiveNamesProcessing ??= new Map ( ) ) . set ( newSourceFile . path , oldTypeResolutions ) ;
2494
- }
2528
+ const typeReferenceResolutionsChanged = hasChangesInResolutions (
2529
+ typesReferenceDirectives ,
2530
+ newSourceFile ,
2531
+ typeReferenceResolutions ,
2532
+ ( name , mode ) => oldProgram ?. getResolvedTypeReferenceDirective ( newSourceFile , name , mode ) ,
2533
+ typeDirectiveIsEqualTo ,
2534
+ typeReferenceResolutionNameAndModeGetter ,
2535
+ ) ;
2536
+ if ( typeReferenceResolutionsChanged ) structureIsReused = StructureIsReused . SafeModules ;
2495
2537
}
2496
2538
2497
2539
if ( structureIsReused !== StructureIsReused . Completely ) {
@@ -4931,7 +4973,7 @@ export function createProgram(rootNamesOrOptions: readonly string[] | CreateProg
4931
4973
symlinks = createSymlinkCache ( currentDirectory , getCanonicalFileName ) ;
4932
4974
}
4933
4975
if ( files && ! symlinks . hasProcessedResolutions ( ) ) {
4934
- symlinks . setSymlinksFromResolutions ( resolvedModules , resolvedTypeReferenceDirectiveNames , automaticTypeDirectiveResolutions ) ;
4976
+ symlinks . setSymlinksFromResolutions ( forEachResolvedModule , forEachResolvedTypeReferenceDirective , automaticTypeDirectiveResolutions ) ;
4935
4977
}
4936
4978
return symlinks ;
4937
4979
}
0 commit comments