@@ -1276,8 +1276,8 @@ namespace ts {
1276
1276
let perDirPackageJsonMap : ESMap < Path , string > | undefined ;
1277
1277
const getCanonicalFileName = createGetCanonicalFileName ( state . program ! . useCaseSensitiveFileNames ( ) ) ;
1278
1278
state . program ! . getSourceFiles ( ) . forEach ( f => {
1279
- modules = toPerDirectoryCache ( state , getCanonicalFileName , modules , f , f . resolvedModules , moduleNameToDirectoryMap ) ;
1280
- typeRefs = toPerDirectoryCache ( state , getCanonicalFileName , typeRefs , f , f . resolvedTypeReferenceDirectiveNames ) ;
1279
+ modules = toPerDirectoryCache ( state , getCanonicalFileName , modules , getResolvedModuleOfResolution , f , f . resolvedModules , moduleNameToDirectoryMap ) ;
1280
+ typeRefs = toPerDirectoryCache ( state , getCanonicalFileName , typeRefs , getResolvedTypeReferenceDirectiveOfResolution , f , f . resolvedTypeReferenceDirectiveNames ) ;
1281
1281
if ( f . packageJsonScope ) {
1282
1282
const dirPath = getDirectoryPath ( f . resolvedPath ) ;
1283
1283
if ( ! dirToPackageJsonMap ?. has ( dirPath ) ) {
@@ -1298,7 +1298,7 @@ namespace ts {
1298
1298
if ( automaticTypeDirectiveNames . length ) {
1299
1299
const currentDirectory = state . program ! . getCurrentDirectory ( ) ;
1300
1300
const containingPath = toPath ( state . program ! . getAutomaticTypeDirectiveContainingFile ( ) , currentDirectory , getCanonicalFileName ) ;
1301
- typeRefs = toPerDirectoryCache ( state , getCanonicalFileName , typeRefs , containingPath , state . program ! . getAutomaticTypeDirectiveResolutions ( ) ) ;
1301
+ typeRefs = toPerDirectoryCache ( state , getCanonicalFileName , typeRefs , getResolvedTypeReferenceDirectiveOfResolution , containingPath , state . program ! . getAutomaticTypeDirectiveResolutions ( ) ) ;
1302
1302
}
1303
1303
return state . cacheResolutions = {
1304
1304
modules,
@@ -1310,70 +1310,32 @@ namespace ts {
1310
1310
} ;
1311
1311
}
1312
1312
1313
- function toPerDirectoryCache (
1313
+ function toPerDirectoryCache < T extends ResolutionWithFailedLookupLocations , R extends ResolutionWithResolvedFileName > (
1314
1314
state : BuilderProgramState ,
1315
1315
getCanonicalFileName : GetCanonicalFileName ,
1316
- cacheWithRedirects : CacheWithRedirects < Path , ModeAwareCache < ResolvedTypeReferenceDirectiveWithFailedLookupLocations > > | undefined ,
1317
- fOrPath : SourceFile | Path ,
1318
- cache : ModeAwareCache < ResolvedTypeReferenceDirectiveWithFailedLookupLocations > | undefined ,
1319
- ) : CacheWithRedirects < Path , ModeAwareCache < ResolvedTypeReferenceDirectiveWithFailedLookupLocations > > | undefined ;
1320
- function toPerDirectoryCache (
1321
- state : BuilderProgramState ,
1322
- getCanonicalFileName : GetCanonicalFileName ,
1323
- cacheWithRedirects : CacheWithRedirects < Path , ModeAwareCache < ResolvedModuleWithFailedLookupLocations > > | undefined ,
1324
- f : SourceFile ,
1325
- cache : ModeAwareCache < ResolvedModuleWithFailedLookupLocations > | undefined ,
1326
- moduleNameToDirectoryMap : CacheWithRedirects < ModeAwareCacheKey , ESMap < Path , ResolvedModuleWithFailedLookupLocations > > ,
1327
- ) : CacheWithRedirects < Path , ModeAwareCache < ResolvedModuleWithFailedLookupLocations > > | undefined ;
1328
- function toPerDirectoryCache < T extends ResolvedModuleWithFailedLookupLocations | ResolvedTypeReferenceDirectiveWithFailedLookupLocations > (
1329
- state : BuilderProgramState ,
1330
- getCanonicalFileName : GetCanonicalFileName ,
1331
- cacheWithRedirects : CacheWithRedirects < Path , ModeAwareCache < T > > | undefined ,
1332
- fOrPath : SourceFile | Path ,
1333
- cache : ModeAwareCache < T > | undefined ,
1316
+ perDirCache : CacheWithRedirects < Path , ModeAwareCache < T > > | undefined ,
1317
+ getResolutionWithResolvedFileName : GetResolutionWithResolvedFileName < T , R > ,
1318
+ sourceFileOrPath : SourceFile | Path ,
1319
+ fileCacheFromProgram : ModeAwareCache < T > | undefined ,
1334
1320
moduleNameToDirectoryMap ?: CacheWithRedirects < ModeAwareCacheKey , ESMap < Path , ResolvedModuleWithFailedLookupLocations > > | undefined ,
1335
1321
) : CacheWithRedirects < Path , ModeAwareCache < T > > | undefined {
1336
- if ( ! cache ?. size ( ) ) return cacheWithRedirects ;
1337
- let dirPath : Path , redirectedReference : ResolvedProjectReference | undefined ;
1338
- if ( ! isString ( fOrPath ) ) {
1339
- redirectedReference = state . program ! . getRedirectReferenceForResolution ( fOrPath ) ;
1340
- dirPath = getDirectoryPath ( fOrPath . path ) ;
1341
- }
1342
- else {
1343
- dirPath = getDirectoryPath ( fOrPath ) ;
1344
- }
1345
- let perDirResolutionCache = cacheWithRedirects ?. getMapOfCacheRedirects ( redirectedReference ) ;
1346
- let dirCache = perDirResolutionCache ?. get ( dirPath ) ;
1347
- cache . forEach ( ( resolution , name , mode ) => {
1348
- if ( ! ( resolution as ResolvedModuleWithFailedLookupLocations ) . resolvedModule ?. resolvedFileName && ! ( resolution as ResolvedTypeReferenceDirectiveWithFailedLookupLocations ) . resolvedTypeReferenceDirective ?. resolvedFileName ) return ;
1349
- if ( dirCache ?. has ( name , mode ) ) return ;
1350
- // If there was already external module resolution that is same, set for child directory, dont set resolution for this directory
1351
- if ( moduleNameToDirectoryMap &&
1352
- ! isExternalModuleNameRelative ( name ) &&
1353
- moduleNameToDirectoryMap . getMapOfCacheRedirects ( redirectedReference ) ?. get ( getModeAwareCacheKey ( name , mode ) ) ?. get ( dirPath ) ) {
1354
- return ;
1355
- }
1356
- if ( ! dirCache ) {
1357
- perDirResolutionCache ??= ( cacheWithRedirects ??= createCacheWithRedirects ( state . compilerOptions ) ) . getOrCreateMapOfCacheRedirects ( redirectedReference ) ;
1358
- perDirResolutionCache . set ( dirPath , dirCache = createModeAwareCache ( ) ) ;
1359
- }
1360
- dirCache . set ( name , mode , resolution ) ;
1361
- if ( ! moduleNameToDirectoryMap || isExternalModuleNameRelative ( name ) ) return ;
1362
- // put result in per-module name cache and delete everything that is not needed
1363
- const actualModuleNameToDirectoryMap = moduleNameToDirectoryMap . getOrCreateMapOfCacheRedirects ( redirectedReference ) ;
1364
- const key = getModeAwareCacheKey ( name , mode ) ;
1365
- let directoryPathMap = actualModuleNameToDirectoryMap . get ( key ) ;
1366
- if ( ! directoryPathMap ) actualModuleNameToDirectoryMap . set ( key , directoryPathMap = new Map ( ) ) ;
1367
- moduleNameToDirectorySet (
1368
- directoryPathMap ,
1369
- dirPath ,
1370
- resolution as ResolvedModuleWithFailedLookupLocations ,
1371
- getResolvedFileNameForModuleNameToDirectorySet ,
1372
- dir => toPath ( dir , state . program ! . getCurrentDirectory ( ) , getCanonicalFileName ) ,
1373
- ancestorPath => perDirResolutionCache ?. get ( ancestorPath ) ?. delete ( name , mode )
1374
- ) ;
1375
- } ) ;
1376
- return cacheWithRedirects ;
1322
+ return toPerDirectoryResolution (
1323
+ state . program ! ,
1324
+ fileCacheFromProgram ,
1325
+ sourceFileOrPath ,
1326
+ perDirCache ,
1327
+ moduleNameToDirectoryMap ,
1328
+ noop ,
1329
+ ( r , name , mode , _key , redirectedReference , dirPath ) =>
1330
+ // If this is not resolved, dont put in per dir Cache
1331
+ ! getResolutionWithResolvedFileName ( r ) ?. resolvedFileName ||
1332
+ // If there was already external module resolution that is same, set for child directory, dont set resolution for this directory
1333
+ ( moduleNameToDirectoryMap &&
1334
+ ! isExternalModuleNameRelative ( name ) &&
1335
+ moduleNameToDirectoryMap . getMapOfCacheRedirects ( redirectedReference ) ?. get ( getModeAwareCacheKey ( name , mode ) ) ?. get ( dirPath ) ) ,
1336
+ dir => toPath ( dir , state . program ! . getCurrentDirectory ( ) , getCanonicalFileName ) ,
1337
+ ( ancestorPath , name , mode , perDirResolutionCache ) => perDirResolutionCache ?. get ( ancestorPath ) ?. delete ( name , mode ) ,
1338
+ ) ;
1377
1339
}
1378
1340
1379
1341
export enum BuilderProgramKind {
0 commit comments