Skip to content

Commit dc99355

Browse files
author
Andy Hanson
committed
Move most of resolveModuleNameForLsHost to lsHost
1 parent df20cf3 commit dc99355

File tree

2 files changed

+30
-34
lines changed

2 files changed

+30
-34
lines changed

src/compiler/moduleNameResolver.ts

Lines changed: 17 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ namespace ts {
77
host.trace(formatMessage.apply(undefined, arguments));
88
}
99

10-
/* @internal */
11-
export function isTraceEnabled(compilerOptions: CompilerOptions, host: ModuleResolutionHost): boolean {
10+
function isTraceEnabled(compilerOptions: CompilerOptions, host: ModuleResolutionHost): boolean {
1211
return compilerOptions.traceResolution && host.trace !== undefined;
1312
}
1413

@@ -79,37 +78,6 @@ namespace ts {
7978
traceEnabled: boolean;
8079
}
8180

82-
/** LsHost uses a global cache of automatically-installed typings to help it resolve modules. */
83-
/* @internal */
84-
export function resolveModuleNameForLsHost(moduleName: string, containingFile: string, compilerOptions: CompilerOptions, host: ModuleResolutionHost, globalCache: string | undefined, projectName: string): ResolvedModuleWithFailedLookupLocations {
85-
const primaryResult = resolveModuleName(moduleName, containingFile, compilerOptions, host);
86-
if (primaryResult.resolvedModule && primaryResult.resolvedModule.resolvedTsFileName) {
87-
// return result immediately only if it is .ts, .tsx or .d.ts
88-
// otherwise try to load typings from @types
89-
return primaryResult;
90-
}
91-
92-
// create different collection of failed lookup locations for second pass
93-
// if it will fail and we've already found something during the first pass - we don't want to pollute its results
94-
const secondaryLookupFailedLookupLocations: string[] = [];
95-
if (globalCache !== undefined) {
96-
const traceEnabled = isTraceEnabled(compilerOptions, host);
97-
if (traceEnabled) {
98-
trace(host, Diagnostics.Auto_discovery_for_typings_is_enabled_in_project_0_Running_extra_resolution_pass_for_module_1_using_cache_location_2, projectName, moduleName, globalCache);
99-
}
100-
const state: ModuleResolutionState = { compilerOptions, host, traceEnabled };
101-
const resolved = loadModuleFromNodeModules(Extensions.All, moduleName, globalCache, secondaryLookupFailedLookupLocations, state, /*checkOneLevel*/ true);
102-
if (resolved) {
103-
return createResolvedModuleWithFailedLookupLocations(resolved, /*isExternalLibraryImport*/ true, primaryResult.failedLookupLocations.concat(secondaryLookupFailedLookupLocations));
104-
}
105-
}
106-
107-
if (!primaryResult.resolvedModule && secondaryLookupFailedLookupLocations.length) {
108-
primaryResult.failedLookupLocations = primaryResult.failedLookupLocations.concat(secondaryLookupFailedLookupLocations);
109-
}
110-
return primaryResult;
111-
}
112-
11381
function tryReadTypesSection(packageJsonPath: string, baseDirectory: string, state: ModuleResolutionState): string {
11482
const jsonContent = readJson(packageJsonPath, state.host);
11583

@@ -850,4 +818,20 @@ namespace ts {
850818
containingDirectory = parentPath;
851819
}
852820
}
821+
822+
/**
823+
* LSHost may load a module from a global cache of typings.
824+
* This is the minumum code needed to expose that functionality; the rest is in LSHost.
825+
*/
826+
/* @internal */
827+
export function loadModuleFromGlobalCache(moduleName: string, projectName: string, compilerOptions: CompilerOptions, host: ModuleResolutionHost, globalCache: string): ResolvedModuleWithFailedLookupLocations {
828+
const traceEnabled = isTraceEnabled(compilerOptions, host);
829+
if (traceEnabled) {
830+
trace(host, Diagnostics.Auto_discovery_for_typings_is_enabled_in_project_0_Running_extra_resolution_pass_for_module_1_using_cache_location_2, projectName, moduleName, globalCache);
831+
}
832+
const state: ModuleResolutionState = { compilerOptions, host, traceEnabled };
833+
const failedLookupLocations: string[] = [];
834+
const resolved = loadModuleFromNodeModules(Extensions.All, moduleName, globalCache, failedLookupLocations, state, /*checkOneLevel*/ true);
835+
return createResolvedModuleWithFailedLookupLocations(resolved, /*isExternalLibraryImport*/ true, failedLookupLocations);
836+
}
853837
}

src/server/lsHost.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,19 @@ namespace ts.server {
2323
const globalCache = this.project.getTypingOptions().enableAutoDiscovery
2424
? this.project.projectService.typingsInstaller.globalTypingsCacheLocation
2525
: undefined;
26-
return resolveModuleNameForLsHost(moduleName, containingFile, compilerOptions, host, globalCache, this.project.getProjectName());
26+
const primaryResult = resolveModuleName(moduleName, containingFile, compilerOptions, host);
27+
// return result immediately only if it is .ts, .tsx or .d.ts
28+
if (!(primaryResult.resolvedModule && primaryResult.resolvedModule.resolvedTsFileName) && globalCache !== undefined) {
29+
// otherwise try to load typings from @types
30+
31+
// create different collection of failed lookup locations for second pass
32+
// if it will fail and we've already found something during the first pass - we don't want to pollute its results
33+
const { resolvedModule, failedLookupLocations } = loadModuleFromGlobalCache(moduleName, this.project.getProjectName(), compilerOptions, host, globalCache);
34+
if (resolvedModule) {
35+
return { resolvedModule, failedLookupLocations: primaryResult.failedLookupLocations.concat(failedLookupLocations) };
36+
}
37+
}
38+
return primaryResult;
2739
};
2840
}
2941

0 commit comments

Comments
 (0)