@@ -5,8 +5,8 @@ namespace ts {
55 startRecordingFilesWithChangedResolutions ( ) : void ;
66 finishRecordingFilesWithChangedResolutions ( ) : Path [ ] | undefined ;
77
8- resolveModuleNames ( moduleNames : string [ ] , containingFile : string , reusedNames : string [ ] | undefined , redirectedReference ?: ResolvedProjectReference ) : ( ResolvedModuleFull | undefined ) [ ] ;
9- getResolvedModuleWithFailedLookupLocationsFromCache ( moduleName : string , containingFile : string ) : CachedResolvedModuleWithFailedLookupLocations | undefined ;
8+ resolveModuleNames ( moduleNames : string [ ] , containingFile : string , reusedNames : string [ ] | undefined , redirectedReference ?: ResolvedProjectReference , containingSourceFile ?: SourceFile ) : ( ResolvedModuleFull | undefined ) [ ] ;
9+ getResolvedModuleWithFailedLookupLocationsFromCache ( moduleName : string , containingFile : string , resolutionMode ?: ModuleKind . CommonJS | ModuleKind . ESNext ) : CachedResolvedModuleWithFailedLookupLocations | undefined ;
1010 resolveTypeReferenceDirectives ( typeDirectiveNames : string [ ] , containingFile : string , redirectedReference ?: ResolvedProjectReference ) : ( ResolvedTypeReferenceDirective | undefined ) [ ] ;
1111
1212 invalidateResolutionsOfFailedLookupLocations ( ) : boolean ;
@@ -166,8 +166,8 @@ namespace ts {
166166 // The resolvedModuleNames and resolvedTypeReferenceDirectives are the cache of resolutions per file.
167167 // The key in the map is source file's path.
168168 // The values are Map of resolutions with key being name lookedup.
169- const resolvedModuleNames = new Map < Path , ESMap < string , CachedResolvedModuleWithFailedLookupLocations > > ( ) ;
170- const perDirectoryResolvedModuleNames : CacheWithRedirects < ESMap < string , CachedResolvedModuleWithFailedLookupLocations > > = createCacheWithRedirects ( ) ;
169+ const resolvedModuleNames = new Map < Path , ModeAwareCache < CachedResolvedModuleWithFailedLookupLocations > > ( ) ;
170+ const perDirectoryResolvedModuleNames : CacheWithRedirects < ModeAwareCache < CachedResolvedModuleWithFailedLookupLocations > > = createCacheWithRedirects ( ) ;
171171 const nonRelativeModuleNameCache : CacheWithRedirects < PerModuleNameCache > = createCacheWithRedirects ( ) ;
172172 const moduleResolutionCache = createModuleResolutionCache (
173173 getCurrentDirectory ( ) ,
@@ -177,8 +177,8 @@ namespace ts {
177177 nonRelativeModuleNameCache ,
178178 ) ;
179179
180- const resolvedTypeReferenceDirectives = new Map < Path , ESMap < string , CachedResolvedTypeReferenceDirectiveWithFailedLookupLocations > > ( ) ;
181- const perDirectoryResolvedTypeReferenceDirectives : CacheWithRedirects < ESMap < string , CachedResolvedTypeReferenceDirectiveWithFailedLookupLocations > > = createCacheWithRedirects ( ) ;
180+ const resolvedTypeReferenceDirectives = new Map < Path , ModeAwareCache < CachedResolvedTypeReferenceDirectiveWithFailedLookupLocations > > ( ) ;
181+ const perDirectoryResolvedTypeReferenceDirectives : CacheWithRedirects < ModeAwareCache < CachedResolvedTypeReferenceDirectiveWithFailedLookupLocations > > = createCacheWithRedirects ( ) ;
182182 const typeReferenceDirectiveResolutionCache = createTypeReferenceDirectiveResolutionCache (
183183 getCurrentDirectory ( ) ,
184184 resolutionHost . getCanonicalFileName ,
@@ -354,27 +354,28 @@ namespace ts {
354354 names : readonly string [ ] ;
355355 containingFile : string ;
356356 redirectedReference : ResolvedProjectReference | undefined ;
357- cache : ESMap < Path , ESMap < string , T > > ;
358- perDirectoryCacheWithRedirects : CacheWithRedirects < ESMap < string , T > > ;
359- loader : ( name : string , containingFile : string , options : CompilerOptions , host : ModuleResolutionHost , redirectedReference ?: ResolvedProjectReference ) => T ;
357+ cache : ESMap < Path , ModeAwareCache < T > > ;
358+ perDirectoryCacheWithRedirects : CacheWithRedirects < ModeAwareCache < T > > ;
359+ loader : ( name : string , containingFile : string , options : CompilerOptions , host : ModuleResolutionHost , redirectedReference ?: ResolvedProjectReference , containingSourceFile ?: SourceFile ) => T ;
360360 getResolutionWithResolvedFileName : GetResolutionWithResolvedFileName < T , R > ;
361361 shouldRetryResolution : ( t : T ) => boolean ;
362362 reusedNames ?: readonly string [ ] ;
363363 logChanges ?: boolean ;
364+ containingSourceFile ?: SourceFile ;
364365 }
365366 function resolveNamesWithLocalCache < T extends ResolutionWithFailedLookupLocations , R extends ResolutionWithResolvedFileName > ( {
366367 names, containingFile, redirectedReference,
367368 cache, perDirectoryCacheWithRedirects,
368369 loader, getResolutionWithResolvedFileName,
369- shouldRetryResolution, reusedNames, logChanges
370+ shouldRetryResolution, reusedNames, logChanges, containingSourceFile
370371 } : ResolveNamesWithLocalCacheInput < T , R > ) : ( R | undefined ) [ ] {
371372 const path = resolutionHost . toPath ( containingFile ) ;
372- const resolutionsInFile = cache . get ( path ) || cache . set ( path , new Map ( ) ) . get ( path ) ! ;
373+ const resolutionsInFile = cache . get ( path ) || cache . set ( path , createModeAwareCache ( ) ) . get ( path ) ! ;
373374 const dirPath = getDirectoryPath ( path ) ;
374375 const perDirectoryCache = perDirectoryCacheWithRedirects . getOrCreateMapOfCacheRedirects ( redirectedReference ) ;
375376 let perDirectoryResolution = perDirectoryCache . get ( dirPath ) ;
376377 if ( ! perDirectoryResolution ) {
377- perDirectoryResolution = new Map ( ) ;
378+ perDirectoryResolution = createModeAwareCache ( ) ;
378379 perDirectoryCache . set ( dirPath , perDirectoryResolution ) ;
379380 }
380381 const resolvedModules : ( R | undefined ) [ ] = [ ] ;
@@ -388,16 +389,19 @@ namespace ts {
388389 ! redirectedReference || redirectedReference . sourceFile . path !== oldRedirect . sourceFile . path :
389390 ! ! redirectedReference ;
390391
391- const seenNamesInFile = new Map < string , true > ( ) ;
392+ const seenNamesInFile = createModeAwareCache < true > ( ) ;
393+ let i = 0 ;
392394 for ( const name of names ) {
393- let resolution = resolutionsInFile . get ( name ) ;
395+ const mode = containingSourceFile ? getModeForResolutionAtIndex ( containingSourceFile , i ) : undefined ;
396+ i ++ ;
397+ let resolution = resolutionsInFile . get ( name , mode ) ;
394398 // Resolution is valid if it is present and not invalidated
395- if ( ! seenNamesInFile . has ( name ) &&
399+ if ( ! seenNamesInFile . has ( name , mode ) &&
396400 unmatchedRedirects || ! resolution || resolution . isInvalidated ||
397401 // If the name is unresolved import that was invalidated, recalculate
398402 ( hasInvalidatedNonRelativeUnresolvedImport && ! isExternalModuleNameRelative ( name ) && shouldRetryResolution ( resolution ) ) ) {
399403 const existingResolution = resolution ;
400- const resolutionInDirectory = perDirectoryResolution . get ( name ) ;
404+ const resolutionInDirectory = perDirectoryResolution . get ( name , mode ) ;
401405 if ( resolutionInDirectory ) {
402406 resolution = resolutionInDirectory ;
403407 const host = resolutionHost . getCompilerHost ?.( ) || resolutionHost ;
@@ -425,10 +429,10 @@ namespace ts {
425429 }
426430 }
427431 else {
428- resolution = loader ( name , containingFile , compilerOptions , resolutionHost . getCompilerHost ?.( ) || resolutionHost , redirectedReference ) ;
429- perDirectoryResolution . set ( name , resolution ) ;
432+ resolution = loader ( name , containingFile , compilerOptions , resolutionHost . getCompilerHost ?.( ) || resolutionHost , redirectedReference , containingSourceFile ) ;
433+ perDirectoryResolution . set ( name , mode , resolution ) ;
430434 }
431- resolutionsInFile . set ( name , resolution ) ;
435+ resolutionsInFile . set ( name , mode , resolution ) ;
432436 watchFailedLookupLocationsOfExternalModuleResolutions ( name , resolution , path , getResolutionWithResolvedFileName ) ;
433437 if ( existingResolution ) {
434438 stopWatchFailedLookupLocationOfResolution ( existingResolution , path , getResolutionWithResolvedFileName ) ;
@@ -442,7 +446,7 @@ namespace ts {
442446 }
443447 else {
444448 const host = resolutionHost . getCompilerHost ?.( ) || resolutionHost ;
445- if ( isTraceEnabled ( compilerOptions , host ) && ! seenNamesInFile . has ( name ) ) {
449+ if ( isTraceEnabled ( compilerOptions , host ) && ! seenNamesInFile . has ( name , mode ) ) {
446450 const resolved = getResolutionWithResolvedFileName ( resolution ) ;
447451 trace (
448452 host ,
@@ -465,15 +469,15 @@ namespace ts {
465469 }
466470 }
467471 Debug . assert ( resolution !== undefined && ! resolution . isInvalidated ) ;
468- seenNamesInFile . set ( name , true ) ;
472+ seenNamesInFile . set ( name , mode , true ) ;
469473 resolvedModules . push ( getResolutionWithResolvedFileName ( resolution ) ) ;
470474 }
471475
472476 // Stop watching and remove the unused name
473- resolutionsInFile . forEach ( ( resolution , name ) => {
474- if ( ! seenNamesInFile . has ( name ) && ! contains ( reusedNames , name ) ) {
477+ resolutionsInFile . forEach ( ( resolution , name , mode ) => {
478+ if ( ! seenNamesInFile . has ( name , mode ) && ! contains ( reusedNames , name ) ) {
475479 stopWatchFailedLookupLocationOfResolution ( resolution , path , getResolutionWithResolvedFileName ) ;
476- resolutionsInFile . delete ( name ) ;
480+ resolutionsInFile . delete ( name , mode ) ;
477481 }
478482 } ) ;
479483
@@ -511,7 +515,7 @@ namespace ts {
511515 } ) ;
512516 }
513517
514- function resolveModuleNames ( moduleNames : string [ ] , containingFile : string , reusedNames : string [ ] | undefined , redirectedReference ?: ResolvedProjectReference ) : ( ResolvedModuleFull | undefined ) [ ] {
518+ function resolveModuleNames ( moduleNames : string [ ] , containingFile : string , reusedNames : string [ ] | undefined , redirectedReference ?: ResolvedProjectReference , containingSourceFile ?: SourceFile ) : ( ResolvedModuleFull | undefined ) [ ] {
515519 return resolveNamesWithLocalCache < CachedResolvedModuleWithFailedLookupLocations , ResolvedModuleFull > ( {
516520 names : moduleNames ,
517521 containingFile,
@@ -523,12 +527,14 @@ namespace ts {
523527 shouldRetryResolution : resolution => ! resolution . resolvedModule || ! resolutionExtensionIsTSOrJson ( resolution . resolvedModule . extension ) ,
524528 reusedNames,
525529 logChanges : logChangesWhenResolvingModule ,
530+ containingSourceFile,
526531 } ) ;
527532 }
528533
529- function getResolvedModuleWithFailedLookupLocationsFromCache ( moduleName : string , containingFile : string ) : CachedResolvedModuleWithFailedLookupLocations | undefined {
534+ function getResolvedModuleWithFailedLookupLocationsFromCache ( moduleName : string , containingFile : string , resolutionMode ?: ModuleKind . CommonJS | ModuleKind . ESNext ) : CachedResolvedModuleWithFailedLookupLocations | undefined {
530535 const cache = resolvedModuleNames . get ( resolutionHost . toPath ( containingFile ) ) ;
531- return cache && cache . get ( moduleName ) ;
536+ if ( ! cache ) return undefined ;
537+ return cache . get ( moduleName , resolutionMode ) ;
532538 }
533539
534540 function isNodeModulesAtTypesDirectory ( dirPath : Path ) {
@@ -751,7 +757,7 @@ namespace ts {
751757 }
752758
753759 function removeResolutionsOfFileFromCache < T extends ResolutionWithFailedLookupLocations , R extends ResolutionWithResolvedFileName > (
754- cache : ESMap < string , ESMap < string , T > > ,
760+ cache : ESMap < string , ModeAwareCache < T > > ,
755761 filePath : Path ,
756762 getResolutionWithResolvedFileName : GetResolutionWithResolvedFileName < T , R > ,
757763 ) {
0 commit comments