@@ -204,6 +204,7 @@ export function verifyResolutionCache(
204
204
actualProgram : ts . Program ,
205
205
resolutionHostCacheHost : ts . ResolutionCacheHost ,
206
206
projectName : string ,
207
+ userResolvedModuleNames ?: true ,
207
208
) {
208
209
const currentDirectory = resolutionHostCacheHost . getCurrentDirectory ! ( ) ;
209
210
const expected = ts . createResolutionCache ( resolutionHostCacheHost , actual . rootDirForResolution ) ;
@@ -214,6 +215,12 @@ export function verifyResolutionCache(
214
215
const expectedToResolution = new Map < ExpectedResolution , ts . ResolutionWithFailedLookupLocations > ( ) ;
215
216
const resolutionToExpected = new Map < ts . ResolutionWithFailedLookupLocations , ExpectedResolution > ( ) ;
216
217
const resolutionToRefs = new Map < ts . ResolutionWithFailedLookupLocations , ResolutionInfo [ ] > ( ) ;
218
+ const inferredTypesPath = resolutionHostCacheHost . toPath (
219
+ ts . getAutomaticTypeDirectiveContainingFile (
220
+ actualProgram . getCompilerOptions ( ) ,
221
+ currentDirectory ,
222
+ ) ,
223
+ ) ;
217
224
actual . resolvedModuleNames . forEach ( ( resolutions , path ) =>
218
225
collectResolutionToRefFromCache (
219
226
"Modules" ,
@@ -222,6 +229,7 @@ export function verifyResolutionCache(
222
229
getResolvedModuleFileName ,
223
230
/*deferWatchingNonRelativeResolution*/ true ,
224
231
expected . resolvedModuleNames ,
232
+ ( name , mode ) => actualProgram . getResolvedModule ( actualProgram . getSourceFileByPath ( path ) ! , name , mode ) ,
225
233
)
226
234
) ;
227
235
actual . resolvedTypeReferenceDirectives . forEach ( ( resolutions , path ) =>
@@ -232,6 +240,10 @@ export function verifyResolutionCache(
232
240
getResolvedTypeRefFileName ,
233
241
/*deferWatchingNonRelativeResolution*/ false ,
234
242
expected . resolvedTypeReferenceDirectives ,
243
+ ( name , mode ) =>
244
+ path !== inferredTypesPath ?
245
+ actualProgram . getResolvedTypeReferenceDirective ( actualProgram . getSourceFileByPath ( path ) ! , name , mode ) :
246
+ actualProgram . getAutomaticTypeDirectiveResolutions ( ) . get ( name , mode ) ,
235
247
)
236
248
) ;
237
249
actual . resolvedLibraries . forEach ( ( resolved , libFileName ) => {
@@ -248,6 +260,39 @@ export function verifyResolutionCache(
248
260
) ;
249
261
expected . resolvedLibraries . set ( libFileName , expectedResolution ) ;
250
262
} ) ;
263
+ // Check for resolutions in program but not in cache to empty resolutions
264
+ if ( ! userResolvedModuleNames ) {
265
+ actualProgram . forEachResolvedModule ( ( resolution , name , mode , filePath ) =>
266
+ verifyResolutionIsInCache (
267
+ "Modules" ,
268
+ actual . resolvedModuleNames . get ( filePath ) ,
269
+ resolution ,
270
+ name ,
271
+ mode ,
272
+ filePath ,
273
+ )
274
+ ) ;
275
+ }
276
+ actualProgram . forEachResolvedTypeReferenceDirective ( ( resolution , name , mode , filePath ) =>
277
+ verifyResolutionIsInCache (
278
+ "TypeRefs" ,
279
+ actual . resolvedTypeReferenceDirectives . get ( filePath ) ,
280
+ resolution ,
281
+ name ,
282
+ mode ,
283
+ filePath ,
284
+ )
285
+ ) ;
286
+ actualProgram . getAutomaticTypeDirectiveResolutions ( ) . forEach ( ( resolution , name , mode ) =>
287
+ verifyResolutionIsInCache (
288
+ "AutoTypeRefs" ,
289
+ actual . resolvedTypeReferenceDirectives . get ( inferredTypesPath ) ,
290
+ resolution ,
291
+ name ,
292
+ mode ,
293
+ inferredTypesPath ,
294
+ )
295
+ ) ;
251
296
252
297
expected . finishCachingPerDirectoryResolution ( actualProgram , /*oldProgram*/ undefined ) ;
253
298
@@ -260,6 +305,10 @@ export function verifyResolutionCache(
260
305
`Expected from:: ${ JSON . stringify ( info , undefined , " " ) } ` +
261
306
`Actual from: ${ resolution . files ?. size } ` ,
262
307
) ;
308
+ ts . Debug . assert (
309
+ ! resolution . isInvalidated ,
310
+ `${ projectName } :: Resolution should not be invalidated` ,
311
+ ) ;
263
312
verifySet ( resolutionToExpected . get ( resolution ) ! . files , resolution . files , `${ projectName } :: Resolution files` ) ;
264
313
} ) ;
265
314
verifyMapOfResolutionSet ( expected . resolvedFileToResolution , actual . resolvedFileToResolution , `resolvedFileToResolution` ) ;
@@ -295,24 +344,54 @@ export function verifyResolutionCache(
295
344
ts . Debug . assert ( expected . countResolutionsResolvedWithGlobalCache ( ) === 0 , `${ projectName } :: ResolutionsResolvedWithGlobalCache should be cleared` ) ;
296
345
ts . Debug . assert ( expected . countResolutionsResolvedWithoutGlobalCache ( ) === 0 , `${ projectName } :: ResolutionsResolvedWithoutGlobalCache should be cleared` ) ;
297
346
347
+ function verifyResolutionIsInCache < T extends ts . ResolutionWithFailedLookupLocations > (
348
+ cacheType : string ,
349
+ cache : ts . ModeAwareCache < T > | undefined ,
350
+ resolution : T ,
351
+ name : string ,
352
+ mode : ts . ResolutionMode ,
353
+ fileName : string ,
354
+ ) {
355
+ if ( resolution as unknown !== ts . emptyResolution ) {
356
+ // Resolutions should match
357
+ ts . Debug . assert (
358
+ cache ?. get ( name , mode ) === resolution ,
359
+ `${ projectName } :: ${ cacheType } :: ${ name } :: ${ mode } Expected resolution in program to be in cache ${ fileName } ` ,
360
+ ) ;
361
+ }
362
+ else {
363
+ // EmptyResolution is place holder and shouldnt be in the cache
364
+ ts . Debug . assert (
365
+ ! cache ?. has ( name , mode ) ,
366
+ `${ projectName } :: ${ cacheType } :: ${ name } :: ${ mode } Ambient moduleResolution, should not be watched ${ fileName } ` ,
367
+ ) ;
368
+ }
369
+ }
370
+
298
371
function collectResolutionToRefFromCache < T extends ts . ResolutionWithFailedLookupLocations > (
299
372
cacheType : string ,
300
373
fileName : ts . Path ,
301
374
cache : ts . ModeAwareCache < T > | undefined ,
302
375
getResolvedFileName : ( resolution : T ) => string | undefined ,
303
376
deferWatchingNonRelativeResolution : boolean ,
304
- storeExpcted : Map < ts . Path , ts . ModeAwareCache < ts . ResolutionWithFailedLookupLocations > > ,
377
+ storeExpected : Map < ts . Path , ts . ModeAwareCache < ts . ResolutionWithFailedLookupLocations > > ,
378
+ getProgramResolutions : ( name : string , mode : ts . ResolutionMode ) => T | undefined ,
305
379
) {
306
380
ts . Debug . assert (
307
- actualProgram . getSourceFileByPath ( fileName ) || ts . endsWith ( fileName , ts . inferredTypesContainingFile ) ,
381
+ actualProgram . getSourceFileByPath ( fileName ) || inferredTypesPath === fileName ,
308
382
`${ projectName } :: ${ cacheType } ${ fileName } Expect cache for file in program or auto type ref` ,
309
383
) ;
310
384
let expectedCache : ts . ModeAwareCache < ts . ResolutionWithFailedLookupLocations > | undefined ;
311
385
cache ?. forEach ( ( resolved , name , mode ) => {
312
386
const resolvedFileName = getResolvedFileName ( resolved ) ;
313
387
const expected = collectResolution ( cacheType , fileName , resolved , resolvedFileName , name , mode , deferWatchingNonRelativeResolution ) ;
314
- if ( ! expectedCache ) storeExpcted . set ( fileName , expectedCache = ts . createModeAwareCache ( ) ) ;
388
+ if ( ! expectedCache ) storeExpected . set ( fileName , expectedCache = ts . createModeAwareCache ( ) ) ;
315
389
expectedCache . set ( name , mode , expected ) ;
390
+ // Resolution in cache should be same as that is in program
391
+ ts . Debug . assert (
392
+ resolved === getProgramResolutions ( name , mode ) ,
393
+ `${ projectName } :: ${ cacheType } ${ fileName } ${ name } ${ mode } Expected resolution in cache to be matched to that in the program` ,
394
+ ) ;
316
395
} ) ;
317
396
}
318
397
0 commit comments