Skip to content

Commit 4ba5cb8

Browse files
committed
Refactor
1 parent 03e1204 commit 4ba5cb8

File tree

2 files changed

+118
-116
lines changed

2 files changed

+118
-116
lines changed

src/compiler/builder.ts

Lines changed: 31 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ import {
5656
getOptionsNameMap,
5757
getOwnKeys,
5858
getRelativePathFromDirectory,
59-
getResolvedFileNameForModuleNameToDirectorySet,
59+
GetResolutionWithResolvedFileName,
60+
getResolvedModuleOfResolution,
61+
getResolvedTypeReferenceDirectiveOfResolution,
6062
getTsBuildInfoEmitOutputFilePath,
6163
handleNoEmitOptions,
6264
HostForComputeHash,
@@ -90,6 +92,8 @@ import {
9092
ReadBuildProgramHost,
9193
ReadonlyCollection,
9294
ResolutionMode,
95+
ResolutionWithFailedLookupLocations,
96+
ResolutionWithResolvedFileName,
9397
ResolvedModuleFull,
9498
ResolvedModuleWithFailedLookupLocations,
9599
ResolvedProjectReference,
@@ -104,6 +108,7 @@ import {
104108
sourceFileMayBeEmitted,
105109
SourceMapEmitResult,
106110
toPath,
111+
toPerDirectoryResolution,
107112
tryAddToSet,
108113
WriteFileCallback,
109114
WriteFileCallbackData,
@@ -1472,8 +1477,8 @@ function getCacheResolutions(state: BuilderProgramState, getCanonicalFileName: G
14721477
const dirToPackageJsonMap = new Map<Path, string>();
14731478
let perDirPackageJsonMap: Map<Path, string> | undefined;
14741479
state.program!.getSourceFiles().forEach(f => {
1475-
modules = toPerDirectoryCache(state, getCanonicalFileName!, modules, f, f.resolvedModules, moduleNameToDirectoryMap);
1476-
typeRefs = toPerDirectoryCache(state, getCanonicalFileName!, typeRefs, f, f.resolvedTypeReferenceDirectiveNames);
1480+
modules = toPerDirectoryCache(state, getCanonicalFileName!, modules, getResolvedModuleOfResolution, f, f.resolvedModules, moduleNameToDirectoryMap);
1481+
typeRefs = toPerDirectoryCache(state, getCanonicalFileName!, typeRefs, getResolvedTypeReferenceDirectiveOfResolution, f, f.resolvedTypeReferenceDirectiveNames);
14771482
if (f.packageJsonScope) {
14781483
const dirPath = getDirectoryPath(f.resolvedPath);
14791484
if (!dirToPackageJsonMap?.has(dirPath)) {
@@ -1494,7 +1499,7 @@ function getCacheResolutions(state: BuilderProgramState, getCanonicalFileName: G
14941499
if (automaticTypeDirectiveNames.length) {
14951500
const currentDirectory = state.program!.getCurrentDirectory();
14961501
const containingPath = toPath(state.program!.getAutomaticTypeDirectiveContainingFile(), currentDirectory, getCanonicalFileName);
1497-
typeRefs = toPerDirectoryCache(state, getCanonicalFileName, typeRefs, containingPath, state.program!.getAutomaticTypeDirectiveResolutions());
1502+
typeRefs = toPerDirectoryCache(state, getCanonicalFileName, typeRefs, getResolvedTypeReferenceDirectiveOfResolution, containingPath, state.program!.getAutomaticTypeDirectiveResolutions());
14981503
}
14991504
return state.cacheResolutions = {
15001505
modules,
@@ -1506,70 +1511,32 @@ function getCacheResolutions(state: BuilderProgramState, getCanonicalFileName: G
15061511
};
15071512
}
15081513

1509-
function toPerDirectoryCache(
1514+
function toPerDirectoryCache<T extends ResolutionWithFailedLookupLocations, R extends ResolutionWithResolvedFileName>(
15101515
state: BuilderProgramState,
15111516
getCanonicalFileName: GetCanonicalFileName,
1512-
cacheWithRedirects: CacheWithRedirects<Path, ModeAwareCache<ResolvedTypeReferenceDirectiveWithFailedLookupLocations>> | undefined,
1513-
fOrPath: SourceFile | Path,
1514-
cache: ModeAwareCache<ResolvedTypeReferenceDirectiveWithFailedLookupLocations> | undefined,
1515-
): CacheWithRedirects<Path, ModeAwareCache<ResolvedTypeReferenceDirectiveWithFailedLookupLocations>> | undefined;
1516-
function toPerDirectoryCache(
1517-
state: BuilderProgramState,
1518-
getCanonicalFileName: GetCanonicalFileName,
1519-
cacheWithRedirects: CacheWithRedirects<Path, ModeAwareCache<ResolvedModuleWithFailedLookupLocations>> | undefined,
1520-
fOrPath: SourceFile | Path,
1521-
cache: ModeAwareCache<ResolvedModuleWithFailedLookupLocations> | undefined,
1522-
moduleNameToDirectoryMap: CacheWithRedirects<ModeAwareCacheKey, Map<Path, ResolvedModuleWithFailedLookupLocations>>,
1523-
): CacheWithRedirects<Path, ModeAwareCache<ResolvedModuleWithFailedLookupLocations>> | undefined;
1524-
function toPerDirectoryCache<T extends ResolvedModuleWithFailedLookupLocations | ResolvedTypeReferenceDirectiveWithFailedLookupLocations>(
1525-
state: BuilderProgramState,
1526-
getCanonicalFileName: GetCanonicalFileName,
1527-
cacheWithRedirects: CacheWithRedirects<Path, ModeAwareCache<T>> | undefined,
1528-
fOrPath: SourceFile | Path,
1529-
cache: ModeAwareCache<T> | undefined,
1517+
perDirCache: CacheWithRedirects<Path, ModeAwareCache<T>> | undefined,
1518+
getResolutionWithResolvedFileName: GetResolutionWithResolvedFileName<T, R>,
1519+
sourceFileOrPath: SourceFile | Path,
1520+
fileCacheFromProgram: ModeAwareCache<T> | undefined,
15301521
moduleNameToDirectoryMap?: CacheWithRedirects<ModeAwareCacheKey, Map<Path, ResolvedModuleWithFailedLookupLocations>> | undefined,
15311522
): CacheWithRedirects<Path, ModeAwareCache<T>> | undefined {
1532-
if (!cache?.size()) return cacheWithRedirects;
1533-
let dirPath: Path, redirectedReference: ResolvedProjectReference | undefined;
1534-
if (!isString(fOrPath)) {
1535-
redirectedReference = state.program!.getRedirectReferenceForResolution(fOrPath);
1536-
dirPath = getDirectoryPath(fOrPath.path);
1537-
}
1538-
else {
1539-
dirPath = getDirectoryPath(fOrPath);
1540-
}
1541-
let perDirResolutionCache = cacheWithRedirects?.getMapOfCacheRedirects(redirectedReference);
1542-
let dirCache = perDirResolutionCache?.get(dirPath);
1543-
cache.forEach((resolution, name, mode) => {
1544-
if (!(resolution as ResolvedModuleWithFailedLookupLocations).resolvedModule?.resolvedFileName && !(resolution as ResolvedTypeReferenceDirectiveWithFailedLookupLocations).resolvedTypeReferenceDirective?.resolvedFileName) return;
1545-
if (dirCache?.has(name, mode)) return;
1546-
// If there was already external module resolution that is same, set for child directory, dont set resolution for this directory
1547-
if (moduleNameToDirectoryMap &&
1548-
!isExternalModuleNameRelative(name) &&
1549-
moduleNameToDirectoryMap.getMapOfCacheRedirects(redirectedReference)?.get(createModeAwareCacheKey(name, mode))?.get(dirPath)) {
1550-
return;
1551-
}
1552-
if (!dirCache) {
1553-
perDirResolutionCache ??= (cacheWithRedirects ??= createCacheWithRedirects(state.compilerOptions)).getOrCreateMapOfCacheRedirects(redirectedReference);
1554-
perDirResolutionCache.set(dirPath, dirCache = createModeAwareCache());
1555-
}
1556-
dirCache.set(name, mode, resolution);
1557-
if (!moduleNameToDirectoryMap || isExternalModuleNameRelative(name)) return;
1558-
// put result in per-module name cache and delete everything that is not needed
1559-
const actualModuleNameToDirectoryMap = moduleNameToDirectoryMap.getOrCreateMapOfCacheRedirects(redirectedReference);
1560-
const key = createModeAwareCacheKey(name, mode);
1561-
let directoryPathMap = actualModuleNameToDirectoryMap.get(key);
1562-
if (!directoryPathMap) actualModuleNameToDirectoryMap.set(key, directoryPathMap = new Map());
1563-
moduleNameToDirectorySet(
1564-
directoryPathMap,
1565-
dirPath,
1566-
resolution as ResolvedModuleWithFailedLookupLocations,
1567-
getResolvedFileNameForModuleNameToDirectorySet,
1568-
dir => toPath(dir, state.program!.getCurrentDirectory(), getCanonicalFileName),
1569-
ancestorPath => perDirResolutionCache?.get(ancestorPath)?.delete(name, mode)
1570-
);
1571-
});
1572-
return cacheWithRedirects;
1523+
return toPerDirectoryResolution(
1524+
state.program!,
1525+
fileCacheFromProgram,
1526+
sourceFileOrPath,
1527+
perDirCache,
1528+
moduleNameToDirectoryMap,
1529+
noop,
1530+
(r, name, mode, redirectedReference, dirPath) =>
1531+
// If this is not resolved, dont put in per dir Cache
1532+
!getResolutionWithResolvedFileName(r)?.resolvedFileName ||
1533+
// If there was already external module resolution that is same, set for child directory, dont set resolution for this directory
1534+
(moduleNameToDirectoryMap &&
1535+
!isExternalModuleNameRelative(name) &&
1536+
moduleNameToDirectoryMap.getMapOfCacheRedirects(redirectedReference)?.get(createModeAwareCacheKey(name, mode))?.get(dirPath)),
1537+
dir => toPath(dir, state.program!.getCurrentDirectory(), getCanonicalFileName),
1538+
(ancestorPath, name, mode, perDirResolutionCache) => perDirResolutionCache?.get(ancestorPath)?.delete(name, mode),
1539+
);
15731540
}
15741541

15751542
/** @internal */

0 commit comments

Comments
 (0)