mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-17 20:33:45 -05:00
Use project relative preference for declaration emit (#42232)
* Test where relative import isnt ideal in the declaration emit * use project relative preference for declaration emit Fixes #39117 * Fix incorrect path matching when calculating module specifier * Use correct baseUrl for the module specifier
This commit is contained in:
@@ -181,14 +181,13 @@ namespace ts.moduleSpecifiers {
|
||||
function getLocalModuleSpecifier(moduleFileName: string, info: Info, compilerOptions: CompilerOptions, host: ModuleSpecifierResolutionHost, { ending, relativePreference }: Preferences): string {
|
||||
const { baseUrl, paths, rootDirs } = compilerOptions;
|
||||
const { sourceDirectory, getCanonicalFileName } = info;
|
||||
|
||||
const relativePath = rootDirs && tryGetModuleNameFromRootDirs(rootDirs, moduleFileName, sourceDirectory, getCanonicalFileName, ending, compilerOptions) ||
|
||||
removeExtensionAndIndexPostFix(ensurePathIsNonModuleName(getRelativePathFromDirectory(sourceDirectory, moduleFileName, getCanonicalFileName)), ending, compilerOptions);
|
||||
if (!baseUrl && !paths || relativePreference === RelativePreference.Relative) {
|
||||
return relativePath;
|
||||
}
|
||||
|
||||
const baseDirectory = getPathsBasePath(compilerOptions, host) || baseUrl!;
|
||||
const baseDirectory = getNormalizedAbsolutePath(getPathsBasePath(compilerOptions, host) || baseUrl!, host.getCurrentDirectory());
|
||||
const relativeToBaseUrl = getRelativePathIfInDirectory(moduleFileName, baseDirectory, getCanonicalFileName);
|
||||
if (!relativeToBaseUrl) {
|
||||
return relativePath;
|
||||
@@ -206,7 +205,9 @@ namespace ts.moduleSpecifiers {
|
||||
}
|
||||
|
||||
if (relativePreference === RelativePreference.ExternalNonRelative) {
|
||||
const projectDirectory = host.getCurrentDirectory();
|
||||
const projectDirectory = compilerOptions.configFilePath ?
|
||||
toPath(getDirectoryPath(compilerOptions.configFilePath), host.getCurrentDirectory(), info.getCanonicalFileName) :
|
||||
info.getCanonicalFileName(host.getCurrentDirectory());
|
||||
const modulePath = toPath(moduleFileName, projectDirectory, getCanonicalFileName);
|
||||
const sourceIsInternal = startsWith(sourceDirectory, projectDirectory);
|
||||
const targetIsInternal = startsWith(modulePath, projectDirectory);
|
||||
@@ -446,7 +447,7 @@ namespace ts.moduleSpecifiers {
|
||||
startsWith(relativeToBaseUrl, prefix) &&
|
||||
endsWith(relativeToBaseUrl, suffix) ||
|
||||
!suffix && relativeToBaseUrl === removeTrailingDirectorySeparator(prefix)) {
|
||||
const matchedStar = relativeToBaseUrl.substr(prefix.length, relativeToBaseUrl.length - suffix.length);
|
||||
const matchedStar = relativeToBaseUrl.substr(prefix.length, relativeToBaseUrl.length - suffix.length - prefix.length);
|
||||
return key.replace("*", matchedStar);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user