From 6bf9133461fd1f3f2cae1fbc02ee4a6319a000a6 Mon Sep 17 00:00:00 2001 From: Sheetal Nandi Date: Mon, 14 Aug 2017 15:52:20 -0700 Subject: [PATCH] Update to PR feedback --- src/compiler/resolutionCache.ts | 39 ++++++++++++++++----------------- src/server/project.ts | 4 ++-- 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/src/compiler/resolutionCache.ts b/src/compiler/resolutionCache.ts index dc2e61cd578..e50e32dfcef 100644 --- a/src/compiler/resolutionCache.ts +++ b/src/compiler/resolutionCache.ts @@ -26,23 +26,6 @@ namespace ts { isInvalidated?: boolean; } - interface ResolverWithGlobalCache { - (primaryResult: ResolvedModuleWithFailedLookupLocations, moduleName: string, compilerOptions: CompilerOptions, host: ModuleResolutionHost): ResolvedModuleWithFailedLookupLocations | undefined; - } - - export function resolveWithGlobalCache(primaryResult: ResolvedModuleWithFailedLookupLocations, moduleName: string, compilerOptions: CompilerOptions, host: ModuleResolutionHost, globalCache: string | undefined, projectName: string): ResolvedModuleWithFailedLookupLocations | undefined { - if (!isExternalModuleNameRelative(moduleName) && !(primaryResult.resolvedModule && extensionIsTypeScript(primaryResult.resolvedModule.extension)) && globalCache !== undefined) { - // otherwise try to load typings from @types - - // create different collection of failed lookup locations for second pass - // if it will fail and we've already found something during the first pass - we don't want to pollute its results - const { resolvedModule, failedLookupLocations } = loadModuleFromGlobalCache(moduleName, projectName, compilerOptions, host, globalCache); - if (resolvedModule) { - return { resolvedModule, failedLookupLocations: primaryResult.failedLookupLocations.concat(failedLookupLocations) }; - } - } - } - interface FailedLookupLocationsWatcher { fileWatcher: FileWatcher; refCount: number; @@ -53,7 +36,8 @@ namespace ts { getCompilerOptions: () => CompilerOptions, watchForFailedLookupLocation: (failedLookupLocation: string, failedLookupLocationPath: Path, containingFile: string, name: string) => FileWatcher, log: (s: string) => void, - resolveWithGlobalCache?: ResolverWithGlobalCache): ResolutionCache { + projectName?: string, + getGlobalCache?: () => string | undefined): ResolutionCache { let host: ModuleResolutionHost; let filesWithChangedSetOfUnresolvedImports: Path[]; @@ -107,9 +91,24 @@ namespace ts { function resolveModuleName(moduleName: string, containingFile: string, compilerOptions: CompilerOptions, host: ModuleResolutionHost): ResolvedModuleWithFailedLookupLocations { const primaryResult = ts.resolveModuleName(moduleName, containingFile, compilerOptions, host); - // return result immediately only if it is .ts, .tsx or .d.ts + // return result immediately only if global cache support is not enabled or if it is .ts, .tsx or .d.ts + if (!getGlobalCache) { + return primaryResult; + } + // otherwise try to load typings from @types - return (resolveWithGlobalCache && resolveWithGlobalCache(primaryResult, moduleName, compilerOptions, host)) || primaryResult; + const globalCache = getGlobalCache(); + if (globalCache !== undefined && !isExternalModuleNameRelative(moduleName) && !(primaryResult.resolvedModule && extensionIsTypeScript(primaryResult.resolvedModule.extension))) { + // create different collection of failed lookup locations for second pass + // if it will fail and we've already found something during the first pass - we don't want to pollute its results + const { resolvedModule, failedLookupLocations } = loadModuleFromGlobalCache(moduleName, projectName, compilerOptions, host, globalCache); + if (resolvedModule) { + return { resolvedModule, failedLookupLocations: primaryResult.failedLookupLocations.concat(failedLookupLocations) }; + } + } + + // Default return the result from the first pass + return primaryResult; } function resolveNamesWithLocalCache( diff --git a/src/server/project.ts b/src/server/project.ts index d50621c50bb..fc6dca31ecd 100644 --- a/src/server/project.ts +++ b/src/server/project.ts @@ -220,8 +220,8 @@ namespace ts.server { () => this.compilerOptions, (failedLookupLocation, failedLookupLocationPath, containingFile, name) => this.watchFailedLookupLocation(failedLookupLocation, failedLookupLocationPath, containingFile, name), s => this.projectService.logger.info(s), - (primaryResult, moduleName, compilerOptions, host) => resolveWithGlobalCache(primaryResult, moduleName, compilerOptions, host, - this.getTypeAcquisition().enable ? this.projectService.typingsInstaller.globalTypingsCacheLocation : undefined, this.getProjectName()) + this.getProjectName(), + () => this.getTypeAcquisition().enable ? this.projectService.typingsInstaller.globalTypingsCacheLocation : undefined ); this.lsHost.compilationSettings = this.compilerOptions; this.resolutionCache.setModuleResolutionHost(this.lsHost);