Skip to content

Commit ef69116

Browse files
authoredOct 21, 2022
Generate shortest rootDirs module specifier instead of first possible (#51244)
* Generate shortest rootDirs module specifier instead of first possible * Simplify `min`
1 parent bbb42f4 commit ef69116

File tree

4 files changed

+37
-11
lines changed

4 files changed

+37
-11
lines changed
 

‎src/compiler/core.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1937,8 +1937,10 @@ namespace ts {
19371937
return compareValues(a?.start, b?.start) || compareValues(a?.length, b?.length);
19381938
}
19391939

1940-
export function min<T>(a: T, b: T, compare: Comparer<T>): T {
1941-
return compare(a, b) === Comparison.LessThan ? a : b;
1940+
export function min<T>(items: readonly [T, ...T[]], compare: Comparer<T>): T;
1941+
export function min<T>(items: readonly T[], compare: Comparer<T>): T | undefined;
1942+
export function min<T>(items: readonly T[], compare: Comparer<T>): T | undefined {
1943+
return reduceLeft(items, (x, y) => compare(x, y) === Comparison.LessThan ? x : y);
19421944
}
19431945

19441946
/**

‎src/compiler/moduleSpecifiers.ts

+15-8
Original file line numberDiff line numberDiff line change
@@ -726,16 +726,23 @@ namespace ts.moduleSpecifiers {
726726
}
727727

728728
function tryGetModuleNameFromRootDirs(rootDirs: readonly string[], moduleFileName: string, sourceDirectory: string, getCanonicalFileName: (file: string) => string, ending: Ending, compilerOptions: CompilerOptions): string | undefined {
729-
const normalizedTargetPath = getPathRelativeToRootDirs(moduleFileName, rootDirs, getCanonicalFileName);
730-
if (normalizedTargetPath === undefined) {
729+
const normalizedTargetPaths = getPathsRelativeToRootDirs(moduleFileName, rootDirs, getCanonicalFileName);
730+
if (normalizedTargetPaths === undefined) {
731+
return undefined;
732+
}
733+
734+
const normalizedSourcePaths = getPathsRelativeToRootDirs(sourceDirectory, rootDirs, getCanonicalFileName);
735+
const relativePaths = flatMap(normalizedSourcePaths, sourcePath => {
736+
return map(normalizedTargetPaths, targetPath => ensurePathIsNonModuleName(getRelativePathFromDirectory(sourcePath, targetPath, getCanonicalFileName)));
737+
});
738+
const shortest = min(relativePaths, compareNumberOfDirectorySeparators);
739+
if (!shortest) {
731740
return undefined;
732741
}
733742

734-
const normalizedSourcePath = getPathRelativeToRootDirs(sourceDirectory, rootDirs, getCanonicalFileName);
735-
const relativePath = normalizedSourcePath !== undefined ? ensurePathIsNonModuleName(getRelativePathFromDirectory(normalizedSourcePath, normalizedTargetPath, getCanonicalFileName)) : normalizedTargetPath;
736743
return getEmitModuleResolutionKind(compilerOptions) === ModuleResolutionKind.NodeJs
737-
? removeExtensionAndIndexPostFix(relativePath, ending, compilerOptions)
738-
: removeFileExtension(relativePath);
744+
? removeExtensionAndIndexPostFix(shortest, ending, compilerOptions)
745+
: removeFileExtension(shortest);
739746
}
740747

741748
function tryGetModuleNameAsNodeModule({ path, isRedirect }: ModulePath, { getCanonicalFileName, sourceDirectory }: Info, importingSourceFile: SourceFile , host: ModuleSpecifierResolutionHost, options: CompilerOptions, userPreferences: UserPreferences, packageNameOnly?: boolean, overrideMode?: ModuleKind.ESNext | ModuleKind.CommonJS): string | undefined {
@@ -882,8 +889,8 @@ namespace ts.moduleSpecifiers {
882889
}
883890
}
884891

885-
function getPathRelativeToRootDirs(path: string, rootDirs: readonly string[], getCanonicalFileName: GetCanonicalFileName): string | undefined {
886-
return firstDefined(rootDirs, rootDir => {
892+
function getPathsRelativeToRootDirs(path: string, rootDirs: readonly string[], getCanonicalFileName: GetCanonicalFileName): string[] | undefined {
893+
return mapDefined(rootDirs, rootDir => {
887894
const relativePath = getRelativePathIfInDirectory(path, rootDir, getCanonicalFileName);
888895
return relativePath !== undefined && isPathRelativeToParent(relativePath) ? undefined : relativePath;
889896
});

‎src/services/patternMatcher.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ namespace ts {
259259
}
260260

261261
function betterMatch(a: PatternMatch | undefined, b: PatternMatch | undefined): PatternMatch | undefined {
262-
return min(a, b, compareMatches);
262+
return min([a, b], compareMatches);
263263
}
264264
function compareMatches(a: PatternMatch | undefined, b: PatternMatch | undefined): Comparison {
265265
return a === undefined ? Comparison.GreaterThan : b === undefined ? Comparison.LessThan
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// @Filename: /tsconfig.json
4+
//// {
5+
//// "compilerOptions": {
6+
//// "module": "commonjs",
7+
//// "rootDirs": [".", "./some/other/root"]
8+
//// }
9+
//// }
10+
11+
// @Filename: /some/other/root/types.ts
12+
//// export type Something = {};
13+
14+
// @Filename: /index.ts
15+
//// const s: Something/**/
16+
17+
verify.importFixModuleSpecifiers("", ["./types"]);

0 commit comments

Comments
 (0)