@@ -13,7 +13,7 @@ namespace ts {
1313 resolveModuleNames ( moduleNames : string [ ] , containingFile : string , logChanges : boolean ) : ResolvedModuleFull [ ] ;
1414 resolveTypeReferenceDirectives ( typeDirectiveNames : string [ ] , containingFile : string ) : ResolvedTypeReferenceDirective [ ] ;
1515
16- invalidateResolutionOfDeletedFile ( filePath : Path ) : void ;
16+ invalidateResolutionOfFile ( filePath : Path ) : void ;
1717 invalidateResolutionOfChangedFailedLookupLocation ( failedLookupLocation : string ) : void ;
1818
1919 createHasInvalidatedResolution ( ) : HasInvalidatedResolution ;
@@ -22,7 +22,7 @@ namespace ts {
2222 }
2323
2424 interface NameResolutionWithFailedLookupLocations {
25- readonly failedLookupLocations : string [ ] ;
25+ readonly failedLookupLocations : ReadonlyArray < string > ;
2626 isInvalidated ?: boolean ;
2727 }
2828
@@ -40,9 +40,12 @@ namespace ts {
4040 getGlobalCache ?: ( ) => string | undefined ) : ResolutionCache {
4141
4242 let host : ModuleResolutionHost ;
43- let filesWithChangedSetOfUnresolvedImports : Path [ ] ;
44- let filesWithInvalidatedResolutions : Map < true > ;
43+ let filesWithChangedSetOfUnresolvedImports : Path [ ] | undefined ;
44+ let filesWithInvalidatedResolutions : Map < true > | undefined ;
4545
46+ // The resolvedModuleNames and resolvedTypeReferenceDirectives are the cache of resolutions per file.
47+ // The key in the map is source file's path.
48+ // The values are Map of resolutions with key being name lookedup.
4649 const resolvedModuleNames = createMap < Map < ResolvedModuleWithFailedLookupLocations > > ( ) ;
4750 const resolvedTypeReferenceDirectives = createMap < Map < ResolvedTypeReferenceDirectiveWithFailedLookupLocations > > ( ) ;
4851
@@ -54,7 +57,7 @@ namespace ts {
5457 finishRecordingFilesWithChangedResolutions,
5558 resolveModuleNames,
5659 resolveTypeReferenceDirectives,
57- invalidateResolutionOfDeletedFile ,
60+ invalidateResolutionOfFile ,
5861 invalidateResolutionOfChangedFailedLookupLocation,
5962 createHasInvalidatedResolution,
6063 clear
@@ -65,6 +68,7 @@ namespace ts {
6568 }
6669
6770 function clear ( ) {
71+ // Close all the watches for failed lookup locations, irrespective of refcounts for them since this is to clear the cache
6872 clearMap ( failedLookupLocationsWatches , ( failedLookupLocationPath , failedLookupLocationWatcher ) => {
6973 log ( `Watcher: FailedLookupLocations: Status: ForceClose: LocationPath: ${ failedLookupLocationPath } , refCount: ${ failedLookupLocationWatcher . refCount } ` ) ;
7074 failedLookupLocationWatcher . fileWatcher . close ( ) ;
@@ -103,7 +107,7 @@ namespace ts {
103107 // if it will fail and we've already found something during the first pass - we don't want to pollute its results
104108 const { resolvedModule, failedLookupLocations } = loadModuleFromGlobalCache ( moduleName , projectName , compilerOptions , host , globalCache ) ;
105109 if ( resolvedModule ) {
106- return { resolvedModule, failedLookupLocations : primaryResult . failedLookupLocations . concat ( failedLookupLocations ) } ;
110+ return { resolvedModule, failedLookupLocations : addRange ( primaryResult . failedLookupLocations as Array < string > , failedLookupLocations ) } ;
107111 }
108112 }
109113
@@ -229,20 +233,20 @@ namespace ts {
229233 }
230234
231235 type FailedLookupLocationAction = ( failedLookupLocation : string , failedLookupLocationPath : Path , containingFile : string , name : string ) => void ;
232- function withFailedLookupLocations ( failedLookupLocations : string [ ] , containingFile : string , name : string , fn : FailedLookupLocationAction ) {
236+ function withFailedLookupLocations ( failedLookupLocations : ReadonlyArray < string > , containingFile : string , name : string , fn : FailedLookupLocationAction ) {
233237 forEach ( failedLookupLocations , failedLookupLocation => {
234238 fn ( failedLookupLocation , toPath ( failedLookupLocation ) , containingFile , name ) ;
235239 } ) ;
236240 }
237241
238- function updateFailedLookupLocationWatches ( containingFile : string , name : string , existingFailedLookupLocations : string [ ] , failedLookupLocations : string [ ] ) {
242+ function updateFailedLookupLocationWatches ( containingFile : string , name : string , existingFailedLookupLocations : ReadonlyArray < string > | undefined , failedLookupLocations : ReadonlyArray < string > ) {
239243 if ( failedLookupLocations ) {
240244 if ( existingFailedLookupLocations ) {
241- const existingWatches = arrayToMap ( existingFailedLookupLocations , failedLookupLocation => toPath ( failedLookupLocation ) ) ;
245+ const existingWatches = arrayToMap ( existingFailedLookupLocations , toPath ) ;
242246 for ( const failedLookupLocation of failedLookupLocations ) {
243247 const failedLookupLocationPath = toPath ( failedLookupLocation ) ;
244248 if ( existingWatches && existingWatches . has ( failedLookupLocationPath ) ) {
245- // still has same failed lookup location, keep the was
249+ // still has same failed lookup location, keep the watch
246250 existingWatches . delete ( failedLookupLocationPath ) ;
247251 }
248252 else {
@@ -272,15 +276,15 @@ namespace ts {
272276 cache : Map < Map < T > > ,
273277 getResult : ( s : T ) => R ,
274278 getResultFileName : ( result : R ) => string | undefined ) {
275- cache . forEach ( ( value , path : Path ) => {
279+ cache . forEach ( ( value , path ) => {
276280 if ( path === deletedFilePath ) {
277281 cache . delete ( path ) ;
278282 value . forEach ( ( resolution , name ) => {
279283 withFailedLookupLocations ( resolution . failedLookupLocations , path , name , closeFailedLookupLocationWatcher ) ;
280284 } ) ;
281285 }
282286 else if ( value ) {
283- value . forEach ( ( resolution , __name ) => {
287+ value . forEach ( resolution => {
284288 if ( resolution && ! resolution . isInvalidated ) {
285289 const result = getResult ( resolution ) ;
286290 if ( result ) {
@@ -298,10 +302,10 @@ namespace ts {
298302 function invalidateResolutionCacheOfChangedFailedLookupLocation < T extends NameResolutionWithFailedLookupLocations > (
299303 failedLookupLocation : string ,
300304 cache : Map < Map < T > > ) {
301- cache . forEach ( ( value , containingFile : Path ) => {
305+ cache . forEach ( ( value , containingFile ) => {
302306 if ( value ) {
303- value . forEach ( ( resolution , __name ) => {
304- if ( resolution && ! resolution . isInvalidated && contains ( resolution . failedLookupLocations , failedLookupLocation ) ) {
307+ value . forEach ( resolution => {
308+ if ( resolution && ! resolution . isInvalidated && some ( resolution . failedLookupLocations , location => toPath ( location ) === failedLookupLocation ) ) {
305309 // Mark the file as needing re-evaluation of module resolution instead of using it blindly.
306310 resolution . isInvalidated = true ;
307311 ( filesWithInvalidatedResolutions || ( filesWithInvalidatedResolutions = createMap < true > ( ) ) ) . set ( containingFile , true ) ;
@@ -311,7 +315,7 @@ namespace ts {
311315 } ) ;
312316 }
313317
314- function invalidateResolutionOfDeletedFile ( filePath : Path ) {
318+ function invalidateResolutionOfFile ( filePath : Path ) {
315319 invalidateResolutionCacheOfDeletedFile ( filePath , resolvedModuleNames , m => m . resolvedModule , r => r . resolvedFileName ) ;
316320 invalidateResolutionCacheOfDeletedFile ( filePath , resolvedTypeReferenceDirectives , m => m . resolvedTypeReferenceDirective , r => r . resolvedFileName ) ;
317321 }
0 commit comments