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:
Sheetal Nandi
2022-12-05 11:56:33 -08:00
committed by GitHub
parent c07f51242c
commit 9e845d2248
73 changed files with 2770 additions and 1438 deletions

View File

@@ -959,7 +959,7 @@ export namespace Core {
if (!options.implementations && isStringLiteralLike(node)) {
if (isModuleSpecifierLike(node)) {
const fileIncludeReasons = program.getFileIncludeReasons();
const referencedFileName = node.getSourceFile().resolvedModules?.get(node.text, getModeForUsageLocation(node.getSourceFile(), node))?.resolvedFileName;
const referencedFileName = node.getSourceFile().resolvedModules?.get(node.text, getModeForUsageLocation(node.getSourceFile(), node))?.resolvedModule?.resolvedFileName;
const referencedFile = referencedFileName ? program.getSourceFile(referencedFileName) : undefined;
if (referencedFile) {
return [{ definition: { type: DefinitionKind.String, node }, references: getReferencesForNonModule(referencedFile, fileIncludeReasons, program) || emptyArray }];

View File

@@ -248,9 +248,9 @@ function getSourceFileToImport(
}
else {
const mode = getModeForUsageLocation(importingSourceFile, importLiteral);
const resolved = host.resolveModuleNames
? host.getResolvedModuleWithFailedLookupLocationsFromCache && host.getResolvedModuleWithFailedLookupLocationsFromCache(importLiteral.text, importingSourceFile.fileName, mode)
: program.getResolvedModuleWithFailedLookupLocationsFromCache(importLiteral.text, importingSourceFile.fileName, mode);
const resolved = host.resolveModuleNameLiterals || !host.resolveModuleNames ?
importingSourceFile.resolvedModules?.get(importLiteral.text, mode) :
host.getResolvedModuleWithFailedLookupLocationsFromCache && host.getResolvedModuleWithFailedLookupLocationsFromCache(importLiteral.text, importingSourceFile.fileName, mode);
return getSourceFileToImportFromResolved(importLiteral, resolved, oldToNew, program.getSourceFiles());
}
}

View File

@@ -161,7 +161,7 @@ export function getDefinitionAtPosition(program: Program, sourceFile: SourceFile
if (!symbol && isModuleSpecifierLike(fallbackNode)) {
// We couldn't resolve the module specifier as an external module, but it could
// be that module resolution succeeded but the target was not a module.
const ref = sourceFile.resolvedModules?.get(fallbackNode.text, getModeForUsageLocation(sourceFile, fallbackNode));
const ref = sourceFile.resolvedModules?.get(fallbackNode.text, getModeForUsageLocation(sourceFile, fallbackNode))?.resolvedModule;
if (ref) {
return [{
name: fallbackNode.text,
@@ -300,7 +300,7 @@ export function getReferenceAtPosition(sourceFile: SourceFile, position: number,
const typeReferenceDirective = findReferenceInPosition(sourceFile.typeReferenceDirectives, position);
if (typeReferenceDirective) {
const reference = program.getResolvedTypeReferenceDirectives().get(typeReferenceDirective.fileName, typeReferenceDirective.resolutionMode || sourceFile.impliedNodeFormat);
const reference = program.getResolvedTypeReferenceDirectives().get(typeReferenceDirective.fileName, typeReferenceDirective.resolutionMode || sourceFile.impliedNodeFormat)?.resolvedTypeReferenceDirective;
const file = reference && program.getSourceFile(reference.resolvedFileName!); // TODO:GH#18217
return file && { reference: typeReferenceDirective, fileName: file.fileName, file, unverified: false };
}
@@ -314,7 +314,7 @@ export function getReferenceAtPosition(sourceFile: SourceFile, position: number,
if (sourceFile.resolvedModules?.size()) {
const node = getTouchingToken(sourceFile, position);
if (isModuleSpecifierLike(node) && isExternalModuleNameRelative(node.text) && sourceFile.resolvedModules.has(node.text, getModeForUsageLocation(sourceFile, node))) {
const verifiedFileName = sourceFile.resolvedModules.get(node.text, getModeForUsageLocation(sourceFile, node))?.resolvedFileName;
const verifiedFileName = sourceFile.resolvedModules.get(node.text, getModeForUsageLocation(sourceFile, node))?.resolvedModule?.resolvedFileName;
const fileName = verifiedFileName || resolvePath(getDirectoryPath(sourceFile.fileName), node.text);
return {
file: program.getSourceFile(fileName),

View File

@@ -461,7 +461,7 @@ export function findModuleReferences(program: Program, sourceFiles: readonly Sou
}
}
for (const ref of referencingFile.typeReferenceDirectives) {
const referenced = program.getResolvedTypeReferenceDirectives().get(ref.fileName, ref.resolutionMode || referencingFile.impliedNodeFormat);
const referenced = program.getResolvedTypeReferenceDirectives().get(ref.fileName, ref.resolutionMode || referencingFile.impliedNodeFormat)?.resolvedTypeReferenceDirective;
if (referenced !== undefined && referenced.resolvedFileName === (searchSourceFile as SourceFile).fileName) {
refs.push({ kind: "reference", referencingFile, ref });
}

View File

@@ -253,9 +253,9 @@ import {
RenameInfo,
RenameInfoOptions,
RenameLocation,
ResolvedModuleFull,
ResolvedModuleWithFailedLookupLocations,
ResolvedProjectReference,
ResolvedTypeReferenceDirective,
ResolvedTypeReferenceDirectiveWithFailedLookupLocations,
returnFalse,
scanner,
ScriptElementKind,
@@ -1029,8 +1029,8 @@ class SourceFileObject extends NodeObject implements SourceFile {
public languageVariant!: LanguageVariant;
public identifiers!: Map<string, string>;
public nameTable: UnderscoreEscapedMap<number> | undefined;
public resolvedModules: ModeAwareCache<ResolvedModuleFull> | undefined;
public resolvedTypeReferenceDirectiveNames!: ModeAwareCache<ResolvedTypeReferenceDirective>;
public resolvedModules: ModeAwareCache<ResolvedModuleWithFailedLookupLocations> | undefined;
public resolvedTypeReferenceDirectiveNames!: ModeAwareCache<ResolvedTypeReferenceDirectiveWithFailedLookupLocations>;
public imports!: readonly StringLiteralLike[];
public moduleAugmentations!: StringLiteral[];
private namedDeclarations: Map<string, Declaration[]> | undefined;
@@ -1667,6 +1667,8 @@ export function createLanguageService(
getModuleResolutionCache: maybeBind(host, host.getModuleResolutionCache),
createHash: maybeBind(host, host.createHash),
resolveTypeReferenceDirectives: maybeBind(host, host.resolveTypeReferenceDirectives),
resolveModuleNameLiterals: maybeBind(host, host.resolveModuleNameLiterals),
resolveTypeReferenceDirectiveReferences: maybeBind(host, host.resolveTypeReferenceDirectiveReferences),
useSourceOfProjectReferenceRedirect: maybeBind(host, host.useSourceOfProjectReferenceRedirect),
getParsedCommandLine,
};

View File

@@ -16,7 +16,6 @@ import {
LineAndCharacter,
MinimalResolutionCacheHost,
ModuleResolutionCache,
ModuleResolutionInfo,
ModuleSpecifierCache,
ParsedCommandLine,
Path,
@@ -27,17 +26,18 @@ import {
ResolvedModuleWithFailedLookupLocations,
ResolvedProjectReference,
ResolvedTypeReferenceDirective,
ResolvedTypeReferenceDirectiveWithFailedLookupLocations,
ScriptKind,
SourceFile,
SourceFileLike,
SourceMapper,
StringLiteralLike,
Symbol,
SymlinkCache,
TextChangeRange,
textChanges,
TextRange,
TextSpan,
TypeReferenceDirectiveResolutionInfo,
UserPreferences,
} from "./_namespaces/ts";
@@ -357,9 +357,27 @@ export interface LanguageServiceHost extends GetEffectiveTypeRootsHost, MinimalR
*
* If this is implemented, `getResolvedModuleWithFailedLookupLocationsFromCache` should be too.
*/
resolveModuleNames?(moduleNames: string[], containingFile: string, reusedNames: string[] | undefined, redirectedReference: ResolvedProjectReference | undefined, options: CompilerOptions, containingSourceFile?: SourceFile, resolutionInfo?: ModuleResolutionInfo): (ResolvedModule | undefined)[];
/** @deprecated supply resolveModuleNameLiterals instead for resolution that can handle newer resolution modes like nodenext */
resolveModuleNames?(moduleNames: string[], containingFile: string, reusedNames: string[] | undefined, redirectedReference: ResolvedProjectReference | undefined, options: CompilerOptions, containingSourceFile?: SourceFile): (ResolvedModule | undefined)[];
getResolvedModuleWithFailedLookupLocationsFromCache?(modulename: string, containingFile: string, resolutionMode?: ResolutionMode): ResolvedModuleWithFailedLookupLocations | undefined;
resolveTypeReferenceDirectives?(typeDirectiveNames: string[] | FileReference[], containingFile: string, redirectedReference: ResolvedProjectReference | undefined, options: CompilerOptions, containingFileMode?: ResolutionMode, resolutionInfo?: TypeReferenceDirectiveResolutionInfo): (ResolvedTypeReferenceDirective | undefined)[];
/** @deprecated supply resolveTypeReferenceDirectiveReferences instead for resolution that can handle newer resolution modes like nodenext */
resolveTypeReferenceDirectives?(typeDirectiveNames: string[] | FileReference[], containingFile: string, redirectedReference: ResolvedProjectReference | undefined, options: CompilerOptions, containingFileMode?: ResolutionMode): (ResolvedTypeReferenceDirective | undefined)[];
resolveModuleNameLiterals?(
moduleLiterals: readonly StringLiteralLike[],
containingFile: string,
redirectedReference: ResolvedProjectReference | undefined,
options: CompilerOptions,
containingSourceFile: SourceFile,
reusedNames: readonly StringLiteralLike[] | undefined,
): readonly ResolvedModuleWithFailedLookupLocations[];
resolveTypeReferenceDirectiveReferences?<T extends FileReference | string>(
typeDirectiveReferences: readonly T[],
containingFile: string,
redirectedReference: ResolvedProjectReference | undefined,
options: CompilerOptions,
containingSourceFile: SourceFile | undefined,
reusedNames: readonly T[] | undefined
): readonly ResolvedTypeReferenceDirectiveWithFailedLookupLocations[];
/** @internal */ hasInvalidatedResolutions?: HasInvalidatedResolutions;
/** @internal */ hasChangedAutomaticTypeDirectiveNames?: HasChangedAutomaticTypeDirectiveNames;
/** @internal */ getGlobalTypingsCacheLocation?(): string | undefined;