Allow export map entries to remap back to input files for a program (#47925)

* Allow export map entries to remap back to input files for a program

* Fix file casing issues on windows

* Implement abiguity error, doesnt quite work

* Refine selection logic in error case to use getCommonSourceDirectory, add more tests
This commit is contained in:
Wesley Wigham
2022-05-05 12:53:56 -07:00
committed by GitHub
parent 1e157ef1b2
commit 8e433cda3d
55 changed files with 1035 additions and 13 deletions

View File

@@ -1364,6 +1364,36 @@ namespace ts {
return program;
function addResolutionDiagnostics(list: Diagnostic[] | undefined) {
if (!list) return;
for (const elem of list) {
programDiagnostics.add(elem);
}
}
function pullDiagnosticsFromCache(names: string[] | readonly FileReference[], containingFile: SourceFile) {
if (!moduleResolutionCache) return;
const containingFileName = getNormalizedAbsolutePath(containingFile.originalFileName, currentDirectory);
const containingFileMode = !isString(containingFile) ? containingFile.impliedNodeFormat : undefined;
const containingDir = getDirectoryPath(containingFileName);
const redirectedReference = getRedirectReferenceForResolution(containingFile);
let i = 0;
for (const n of names) {
// mimics logic done in the resolution cache, should be resilient to upgrading it to use `FileReference`s for non-type-reference modal lookups to make it rely on the index in the list less
const mode = typeof n === "string" ? getModeForResolutionAtIndex(containingFile, i) : getModeForFileReference(n, containingFileMode);
const name = typeof n === "string" ? n : n.fileName;
i++;
// only nonrelative names hit the cache, and, at least as of right now, only nonrelative names can issue diagnostics
// (Since diagnostics are only issued via import or export map lookup)
// This may totally change if/when the issue of output paths not mapping to input files is fixed in a broader context
// When it is, how we extract diagnostics from the module name resolver will have the be refined - the current cache
// APIs wrapping the underlying resolver make it almost impossible to smuggle the diagnostics out in a generalized way
if (isExternalModuleNameRelative(name)) continue;
const diags = moduleResolutionCache.getOrCreateCacheForModuleName(name, mode, redirectedReference).get(containingDir)?.resolutionDiagnostics;
addResolutionDiagnostics(diags);
}
}
function resolveModuleNamesWorker(moduleNames: string[], containingFile: SourceFile, reusedNames: string[] | undefined): readonly ResolvedModuleFull[] {
if (!moduleNames.length) return emptyArray;
const containingFileName = getNormalizedAbsolutePath(containingFile.originalFileName, currentDirectory);
@@ -1374,6 +1404,7 @@ namespace ts {
performance.mark("afterResolveModule");
performance.measure("ResolveModule", "beforeResolveModule", "afterResolveModule");
tracing?.pop();
pullDiagnosticsFromCache(moduleNames, containingFile);
return result;
}