Skip to content

Commit 6bf9133

Browse files
committed
Update to PR feedback
1 parent 136b091 commit 6bf9133

2 files changed

Lines changed: 21 additions & 22 deletions

File tree

src/compiler/resolutionCache.ts

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,6 @@ namespace ts {
2626
isInvalidated?: boolean;
2727
}
2828

29-
interface ResolverWithGlobalCache {
30-
(primaryResult: ResolvedModuleWithFailedLookupLocations, moduleName: string, compilerOptions: CompilerOptions, host: ModuleResolutionHost): ResolvedModuleWithFailedLookupLocations | undefined;
31-
}
32-
33-
export function resolveWithGlobalCache(primaryResult: ResolvedModuleWithFailedLookupLocations, moduleName: string, compilerOptions: CompilerOptions, host: ModuleResolutionHost, globalCache: string | undefined, projectName: string): ResolvedModuleWithFailedLookupLocations | undefined {
34-
if (!isExternalModuleNameRelative(moduleName) && !(primaryResult.resolvedModule && extensionIsTypeScript(primaryResult.resolvedModule.extension)) && globalCache !== undefined) {
35-
// otherwise try to load typings from @types
36-
37-
// create different collection of failed lookup locations for second pass
38-
// if it will fail and we've already found something during the first pass - we don't want to pollute its results
39-
const { resolvedModule, failedLookupLocations } = loadModuleFromGlobalCache(moduleName, projectName, compilerOptions, host, globalCache);
40-
if (resolvedModule) {
41-
return { resolvedModule, failedLookupLocations: primaryResult.failedLookupLocations.concat(failedLookupLocations) };
42-
}
43-
}
44-
}
45-
4629
interface FailedLookupLocationsWatcher {
4730
fileWatcher: FileWatcher;
4831
refCount: number;
@@ -53,7 +36,8 @@ namespace ts {
5336
getCompilerOptions: () => CompilerOptions,
5437
watchForFailedLookupLocation: (failedLookupLocation: string, failedLookupLocationPath: Path, containingFile: string, name: string) => FileWatcher,
5538
log: (s: string) => void,
56-
resolveWithGlobalCache?: ResolverWithGlobalCache): ResolutionCache {
39+
projectName?: string,
40+
getGlobalCache?: () => string | undefined): ResolutionCache {
5741

5842
let host: ModuleResolutionHost;
5943
let filesWithChangedSetOfUnresolvedImports: Path[];
@@ -107,9 +91,24 @@ namespace ts {
10791

10892
function resolveModuleName(moduleName: string, containingFile: string, compilerOptions: CompilerOptions, host: ModuleResolutionHost): ResolvedModuleWithFailedLookupLocations {
10993
const primaryResult = ts.resolveModuleName(moduleName, containingFile, compilerOptions, host);
110-
// return result immediately only if it is .ts, .tsx or .d.ts
94+
// return result immediately only if global cache support is not enabled or if it is .ts, .tsx or .d.ts
95+
if (!getGlobalCache) {
96+
return primaryResult;
97+
}
98+
11199
// otherwise try to load typings from @types
112-
return (resolveWithGlobalCache && resolveWithGlobalCache(primaryResult, moduleName, compilerOptions, host)) || primaryResult;
100+
const globalCache = getGlobalCache();
101+
if (globalCache !== undefined && !isExternalModuleNameRelative(moduleName) && !(primaryResult.resolvedModule && extensionIsTypeScript(primaryResult.resolvedModule.extension))) {
102+
// create different collection of failed lookup locations for second pass
103+
// if it will fail and we've already found something during the first pass - we don't want to pollute its results
104+
const { resolvedModule, failedLookupLocations } = loadModuleFromGlobalCache(moduleName, projectName, compilerOptions, host, globalCache);
105+
if (resolvedModule) {
106+
return { resolvedModule, failedLookupLocations: primaryResult.failedLookupLocations.concat(failedLookupLocations) };
107+
}
108+
}
109+
110+
// Default return the result from the first pass
111+
return primaryResult;
113112
}
114113

115114
function resolveNamesWithLocalCache<T extends NameResolutionWithFailedLookupLocations, R>(

src/server/project.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,8 @@ namespace ts.server {
220220
() => this.compilerOptions,
221221
(failedLookupLocation, failedLookupLocationPath, containingFile, name) => this.watchFailedLookupLocation(failedLookupLocation, failedLookupLocationPath, containingFile, name),
222222
s => this.projectService.logger.info(s),
223-
(primaryResult, moduleName, compilerOptions, host) => resolveWithGlobalCache(primaryResult, moduleName, compilerOptions, host,
224-
this.getTypeAcquisition().enable ? this.projectService.typingsInstaller.globalTypingsCacheLocation : undefined, this.getProjectName())
223+
this.getProjectName(),
224+
() => this.getTypeAcquisition().enable ? this.projectService.typingsInstaller.globalTypingsCacheLocation : undefined
225225
);
226226
this.lsHost.compilationSettings = this.compilerOptions;
227227
this.resolutionCache.setModuleResolutionHost(this.lsHost);

0 commit comments

Comments
 (0)