This commit is contained in:
Sheetal Nandi 2020-02-05 08:57:00 -08:00
parent 09441107c1
commit cfaca9a578
5 changed files with 27 additions and 7 deletions

View file

@ -760,6 +760,7 @@ namespace ts {
isSourceFileFromExternalLibrary: returnFalse,
getResolvedProjectReferenceToRedirect: returnUndefined,
isSourceOfProjectReferenceRedirect: returnFalse,
getRefFileMap: returnUndefined,
writeFile: (name, text, writeByteOrderMark) => {
switch (name) {
case jsFilePath:
@ -798,7 +799,8 @@ namespace ts {
useCaseSensitiveFileNames: () => host.useCaseSensitiveFileNames(),
getProgramBuildInfo: returnUndefined,
getSourceFileFromReference: returnUndefined,
redirectTargetsMap: createMultiMap()
redirectTargetsMap: createMultiMap(),
};
emitFiles(
notImplementedResolver,

View file

@ -60,8 +60,9 @@ namespace ts.moduleSpecifiers {
files: readonly SourceFile[],
preferences: UserPreferences = {},
redirectTargetsMap: RedirectTargetsMap,
refFileMap?: MultiMap<RefFile> | undefined
): string {
return getModuleSpecifierWorker(compilerOptions, importingSourceFileName, toFileName, host, files, redirectTargetsMap, getPreferences(preferences, compilerOptions, importingSourceFile));
return getModuleSpecifierWorker(compilerOptions, importingSourceFileName, toFileName, host, files, redirectTargetsMap, getPreferences(preferences, compilerOptions, importingSourceFile), refFileMap);
}
export function getNodeModulesPackageName(
@ -85,12 +86,13 @@ namespace ts.moduleSpecifiers {
host: ModuleSpecifierResolutionHost,
files: readonly SourceFile[],
redirectTargetsMap: RedirectTargetsMap,
preferences: Preferences
preferences: Preferences,
refFileMap?: MultiMap<RefFile> | undefined
): string {
const info = getInfo(importingSourceFileName, host);
const modulePaths = getAllModulePaths(files, importingSourceFileName, toFileName, info.getCanonicalFileName, host, redirectTargetsMap);
return firstDefined(modulePaths, moduleFileName => tryGetModuleNameAsNodeModule(moduleFileName, info, host, compilerOptions)) ||
getLocalModuleSpecifier(toFileName, info, compilerOptions, preferences);
getLocalModuleSpecifier(toFileName, info, compilerOptions, preferences, files, refFileMap);
}
/** Returns an import for each symlink and for the realpath. */
@ -112,7 +114,7 @@ namespace ts.moduleSpecifiers {
const preferences = getPreferences(userPreferences, compilerOptions, importingSourceFile);
const global = mapDefined(modulePaths, moduleFileName => tryGetModuleNameAsNodeModule(moduleFileName, info, host, compilerOptions));
return global.length ? global : modulePaths.map(moduleFileName => getLocalModuleSpecifier(moduleFileName, info, compilerOptions, preferences));
return global.length ? global : modulePaths.map(moduleFileName => getLocalModuleSpecifier(moduleFileName, info, compilerOptions, preferences, files));
}
interface Info {
@ -126,7 +128,7 @@ namespace ts.moduleSpecifiers {
return { getCanonicalFileName, sourceDirectory };
}
function getLocalModuleSpecifier(moduleFileName: string, { getCanonicalFileName, sourceDirectory }: Info, compilerOptions: CompilerOptions, { ending, relativePreference }: Preferences): string {
function getLocalModuleSpecifier(moduleFileName: string, { getCanonicalFileName, sourceDirectory }: Info, compilerOptions: CompilerOptions, { ending, relativePreference }: Preferences, files: readonly SourceFile[], refFileMap?: MultiMap<RefFile> | undefined): string {
const { baseUrl, paths, rootDirs } = compilerOptions;
const relativePath = rootDirs && tryGetModuleNameFromRootDirs(rootDirs, moduleFileName, sourceDirectory, getCanonicalFileName, ending, compilerOptions) ||
@ -135,6 +137,19 @@ namespace ts.moduleSpecifiers {
return relativePath;
}
if (refFileMap && relativePreference === RelativePreference.Auto) {
const references = refFileMap.get(getCanonicalFileName(moduleFileName));
const nonRelativeReference = forEach(
references,
ref => {
if (ref.kind !== RefFileKind.ReferenceFile) return true;
const file = find(files, file => file.path == ref.file)!;
return !pathIsRelative(file.referencedFiles[ref.index].fileName);
}
);
if (!nonRelativeReference) return relativePath
}
const relativeToBaseUrl = getRelativePathIfInDirectory(moduleFileName, baseUrl, getCanonicalFileName);
if (!relativeToBaseUrl) {
return relativePath;

View file

@ -1467,6 +1467,7 @@ namespace ts {
getNewLine: () => host.getNewLine(),
getSourceFile: program.getSourceFile,
getSourceFileByPath: program.getSourceFileByPath,
getRefFileMap: () => refFileMap,
getSourceFiles: program.getSourceFiles,
getLibFileFromReference: program.getLibFileFromReference,
isSourceFileFromExternalLibrary,

View file

@ -353,7 +353,8 @@ namespace ts {
host,
host.getSourceFiles(),
/*preferences*/ undefined,
host.redirectTargetsMap
host.redirectTargetsMap,
host.getRefFileMap()
);
if (!pathIsRelative(specifier)) {
// If some compiler option/symlink/whatever allows access to the file containing the ambient module declaration

View file

@ -5862,6 +5862,7 @@ namespace ts {
getCurrentDirectory(): string;
getLibFileFromReference(ref: FileReference): SourceFile | undefined;
getRefFileMap(): MultiMap<RefFile> | undefined;
getCommonSourceDirectory(): string;
getCanonicalFileName(fileName: string): string;