mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Api cleanup for Module and Type Reference directive resolution (#51546)
* Refactoring so CacheWithRedirects has Key and Value type parameters * ModuleResolutionCache or TypeRefDirectiveCache will look in directory before solving, so ResolutionCache doesnt need this check * Test showing module resolution is not shared because resolution cache doesnt update own options * Enable traceResolution on some of the project reference tests * Simplify CacheWithRedirects and ensure the options are set in all common scenarios so cache can be shared between redirects * Make failedlookup etc optional in ResolvedModule/TypeRefefWithFailedLookupLocations Also make accidental public failed lookup internal * Add new API for module and type ref resolution * Store auto type reference resolutions * Modify test to show how using program partially doesnt report resolution diagnostics * Ensure that resolution diagnostics are reported in filePreocessingDiagnostics so they can be reused when program is reused * Some cleanup * Remove the newly added ReoslutionInfo in favor of new APIs * update
This commit is contained in:
+17
-15
@@ -98,7 +98,6 @@ import {
|
||||
maybeBind,
|
||||
ModuleResolutionCache,
|
||||
ModuleResolutionHost,
|
||||
ModuleResolutionInfo,
|
||||
noop,
|
||||
noopFileWatcher,
|
||||
normalizePath,
|
||||
@@ -120,11 +119,9 @@ import {
|
||||
removeFileExtension,
|
||||
ResolutionCache,
|
||||
resolutionExtensionIsTSOrJson,
|
||||
ResolutionMode,
|
||||
ResolvedModuleFull,
|
||||
ResolvedModuleWithFailedLookupLocations,
|
||||
ResolvedProjectReference,
|
||||
ResolvedTypeReferenceDirective,
|
||||
ResolvedTypeReferenceDirectiveWithFailedLookupLocations,
|
||||
resolvePackageNameToPackageJson,
|
||||
returnFalse,
|
||||
returnTrue,
|
||||
@@ -136,6 +133,7 @@ import {
|
||||
SourceFile,
|
||||
SourceMapper,
|
||||
startsWith,
|
||||
StringLiteralLike,
|
||||
stripQuotes,
|
||||
StructureIsReused,
|
||||
SymlinkCache,
|
||||
@@ -144,7 +142,6 @@ import {
|
||||
toPath,
|
||||
tracing,
|
||||
TypeAcquisition,
|
||||
TypeReferenceDirectiveResolutionInfo,
|
||||
updateErrorForNoInputFiles,
|
||||
updateMissingFilePathsWatch,
|
||||
WatchDirectoryFlags,
|
||||
@@ -554,7 +551,7 @@ export abstract class Project implements LanguageServiceHost, ModuleResolutionHo
|
||||
if (this.program && !this.symlinks.hasProcessedResolutions()) {
|
||||
this.symlinks.setSymlinksFromResolutions(
|
||||
this.program.getSourceFiles(),
|
||||
this.program.getResolvedTypeReferenceDirectives());
|
||||
this.program.getAutomaticTypeDirectiveResolutions());
|
||||
}
|
||||
return this.symlinks;
|
||||
}
|
||||
@@ -666,20 +663,25 @@ export abstract class Project implements LanguageServiceHost, ModuleResolutionHo
|
||||
return !this.isWatchedMissingFile(path) && this.directoryStructureHost.fileExists(file);
|
||||
}
|
||||
|
||||
resolveModuleNames(moduleNames: string[], containingFile: string, reusedNames?: string[], redirectedReference?: ResolvedProjectReference, _options?: CompilerOptions, containingSourceFile?: SourceFile, resolutionInfo?: ModuleResolutionInfo): (ResolvedModuleFull | undefined)[] {
|
||||
return this.resolutionCache.resolveModuleNames(moduleNames, containingFile, reusedNames, redirectedReference, containingSourceFile, resolutionInfo);
|
||||
/** @internal */
|
||||
resolveModuleNameLiterals(moduleLiterals: readonly StringLiteralLike[], containingFile: string, redirectedReference: ResolvedProjectReference | undefined, options: CompilerOptions, containingSourceFile: SourceFile, reusedNames: readonly StringLiteralLike[] | undefined): readonly ResolvedModuleWithFailedLookupLocations[] {
|
||||
return this.resolutionCache.resolveModuleNameLiterals(moduleLiterals, containingFile, redirectedReference, options, containingSourceFile, reusedNames);
|
||||
}
|
||||
|
||||
getModuleResolutionCache(): ModuleResolutionCache | undefined {
|
||||
return this.resolutionCache.getModuleResolutionCache();
|
||||
}
|
||||
|
||||
getResolvedModuleWithFailedLookupLocationsFromCache(moduleName: string, containingFile: string, resolutionMode?: ResolutionMode): ResolvedModuleWithFailedLookupLocations | undefined {
|
||||
return this.resolutionCache.getResolvedModuleWithFailedLookupLocationsFromCache(moduleName, containingFile, resolutionMode);
|
||||
}
|
||||
|
||||
resolveTypeReferenceDirectives(typeDirectiveNames: string[] | FileReference[], containingFile: string, redirectedReference?: ResolvedProjectReference, _options?: CompilerOptions, containingFileMode?: ResolutionMode, resolutionInfo?: TypeReferenceDirectiveResolutionInfo): (ResolvedTypeReferenceDirective | undefined)[] {
|
||||
return this.resolutionCache.resolveTypeReferenceDirectives(typeDirectiveNames, containingFile, redirectedReference, containingFileMode, resolutionInfo);
|
||||
/** @internal */
|
||||
resolveTypeReferenceDirectiveReferences<T extends string | FileReference>(typeDirectiveReferences: readonly T[], containingFile: string, redirectedReference: ResolvedProjectReference | undefined, options: CompilerOptions, containingSourceFile: SourceFile | undefined, reusedNames: readonly T[] | undefined): readonly ResolvedTypeReferenceDirectiveWithFailedLookupLocations[] {
|
||||
return this.resolutionCache.resolveTypeReferenceDirectiveReferences(
|
||||
typeDirectiveReferences,
|
||||
containingFile,
|
||||
redirectedReference,
|
||||
options,
|
||||
containingSourceFile,
|
||||
reusedNames,
|
||||
);
|
||||
}
|
||||
|
||||
directoryExists(path: string): boolean {
|
||||
@@ -2096,7 +2098,7 @@ function extractUnresolvedImportsFromSourceFile(file: SourceFile, ambientModules
|
||||
return getOrUpdate(cachedUnresolvedImportsPerFile, file.path, () => {
|
||||
if (!file.resolvedModules) return emptyArray;
|
||||
let unresolvedImports: string[] | undefined;
|
||||
file.resolvedModules.forEach((resolvedModule, name) => {
|
||||
file.resolvedModules.forEach(({ resolvedModule }, name) => {
|
||||
// pick unresolved non-relative names
|
||||
if ((!resolvedModule || !resolutionExtensionIsTSOrJson(resolvedModule.extension)) &&
|
||||
!isExternalModuleNameRelative(name) &&
|
||||
|
||||
@@ -1578,15 +1578,13 @@ export class Session<TMessage = string> implements EventSender {
|
||||
if (entrypoints && some(entrypoints, e => project.toPath(e) === path)) {
|
||||
// This file was the main entrypoint of a package. Try to resolve that same package name with
|
||||
// the auxiliary project that only resolves to implementation files.
|
||||
const [implementationResolution] = auxiliaryProject.resolveModuleNames([packageName], resolveFromFile);
|
||||
return implementationResolution?.resolvedFileName;
|
||||
return auxiliaryProject.resolutionCache.resolveSingleModuleNameWithoutWatching(packageName, resolveFromFile).resolvedModule?.resolvedFileName;
|
||||
}
|
||||
else {
|
||||
// It wasn't the main entrypoint but we are in node_modules. Try a subpath into the package.
|
||||
const pathToFileInPackage = fileName.substring(nodeModulesPathParts.packageRootIndex + 1);
|
||||
const specifier = `${packageName}/${removeFileExtension(pathToFileInPackage)}`;
|
||||
const [implementationResolution] = auxiliaryProject.resolveModuleNames([specifier], resolveFromFile);
|
||||
return implementationResolution?.resolvedFileName;
|
||||
return auxiliaryProject.resolutionCache.resolveSingleModuleNameWithoutWatching(specifier, resolveFromFile).resolvedModule?.resolvedFileName;
|
||||
}
|
||||
}
|
||||
// We're not in node_modules, and we only get to this function if non-dts module resolution failed.
|
||||
|
||||
Reference in New Issue
Block a user