Support auto-import from paths alias without baseUrl (#40546)

This commit is contained in:
Andrew Branch
2020-09-14 15:23:47 -07:00
committed by GitHub
parent ec36d73e7a
commit 575baf5c7f
4 changed files with 35 additions and 7 deletions

View File

@@ -82,7 +82,7 @@ namespace ts.moduleSpecifiers {
const info = getInfo(importingSourceFileName, host);
const modulePaths = getAllModulePaths(importingSourceFileName, toFileName, host);
return firstDefined(modulePaths, modulePath => tryGetModuleNameAsNodeModule(modulePath, info, host, compilerOptions)) ||
getLocalModuleSpecifier(toFileName, info, compilerOptions, preferences);
getLocalModuleSpecifier(toFileName, info, compilerOptions, host, preferences);
}
/** Returns an import for each symlink and for the realpath. */
@@ -121,7 +121,7 @@ namespace ts.moduleSpecifiers {
}
if (!specifier && !modulePath.isRedirect) {
const local = getLocalModuleSpecifier(modulePath.path, info, compilerOptions, preferences);
const local = getLocalModuleSpecifier(modulePath.path, info, compilerOptions, host, preferences);
if (pathIsBareSpecifier(local)) {
pathsSpecifiers = append(pathsSpecifiers, local);
}
@@ -156,16 +156,17 @@ 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, host: ModuleSpecifierResolutionHost, { ending, relativePreference }: Preferences): string {
const { baseUrl, paths, rootDirs, bundledPackageName } = compilerOptions;
const relativePath = rootDirs && tryGetModuleNameFromRootDirs(rootDirs, moduleFileName, sourceDirectory, getCanonicalFileName, ending, compilerOptions) ||
removeExtensionAndIndexPostFix(ensurePathIsNonModuleName(getRelativePathFromDirectory(sourceDirectory, moduleFileName, getCanonicalFileName)), ending, compilerOptions);
if (!baseUrl || relativePreference === RelativePreference.Relative) {
if (!baseUrl && !paths || relativePreference === RelativePreference.Relative) {
return relativePath;
}
const relativeToBaseUrl = getRelativePathIfInDirectory(moduleFileName, baseUrl, getCanonicalFileName);
const baseDirectory = getPathsBasePath(compilerOptions, host) || baseUrl!;
const relativeToBaseUrl = getRelativePathIfInDirectory(moduleFileName, baseDirectory, getCanonicalFileName);
if (!relativeToBaseUrl) {
return relativePath;
}