Skip to content

Commit a4e5f88

Browse files
committed
Refactor
1 parent 5c7dff6 commit a4e5f88

File tree

2 files changed

+107
-115
lines changed

2 files changed

+107
-115
lines changed

src/compiler/builder.ts

Lines changed: 25 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,8 +1276,8 @@ namespace ts {
12761276
let perDirPackageJsonMap: ESMap<Path, string> | undefined;
12771277
const getCanonicalFileName = createGetCanonicalFileName(state.program!.useCaseSensitiveFileNames());
12781278
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);
12811281
if (f.packageJsonScope) {
12821282
const dirPath = getDirectoryPath(f.resolvedPath);
12831283
if (!dirToPackageJsonMap?.has(dirPath)) {
@@ -1298,7 +1298,7 @@ namespace ts {
12981298
if (automaticTypeDirectiveNames.length) {
12991299
const currentDirectory = state.program!.getCurrentDirectory();
13001300
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());
13021302
}
13031303
return state.cacheResolutions = {
13041304
modules,
@@ -1310,70 +1310,32 @@ namespace ts {
13101310
};
13111311
}
13121312

1313-
function toPerDirectoryCache(
1313+
function toPerDirectoryCache<T extends ResolutionWithFailedLookupLocations, R extends ResolutionWithResolvedFileName>(
13141314
state: BuilderProgramState,
13151315
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,
13341320
moduleNameToDirectoryMap?: CacheWithRedirects<ModeAwareCacheKey, ESMap<Path, ResolvedModuleWithFailedLookupLocations>> | undefined,
13351321
): 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+
);
13771339
}
13781340

13791341
export enum BuilderProgramKind {

0 commit comments

Comments
 (0)