Skip to content

Commit 94b522a

Browse files
Just separate patterns into exact strings and maches. Remove all sorting logic.
1 parent 012259e commit 94b522a

File tree

2 files changed

+10
-22
lines changed

2 files changed

+10
-22
lines changed

src/compiler/moduleNameResolver.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,7 +1568,6 @@ function tryLoadModuleUsingPathsIfEligible(extensions: Extensions, moduleName: s
15681568
trace(state.host, Diagnostics.paths_option_is_specified_looking_for_a_pattern_to_match_module_name_0, moduleName);
15691569
}
15701570
const baseDirectory = getPathsBasePath(state.compilerOptions, state.host)!; // Always defined when 'paths' is defined
1571-
// TODO: do we need to sort by aggregate length?
15721571
const pathPatterns = tryParsePatterns(paths);
15731572
return tryLoadModuleUsingPaths(extensions, moduleName, baseDirectory, paths, pathPatterns, loader, /*onlyRecordFailures*/ false, state);
15741573
}
@@ -2525,7 +2524,6 @@ function loadNodeModuleFromDirectoryWorker(extensions: Extensions, candidate: st
25252524
if (state.traceEnabled) {
25262525
trace(state.host, Diagnostics.package_json_has_a_typesVersions_entry_0_that_matches_compiler_version_1_looking_for_a_pattern_to_match_module_name_2, versionPaths.version, version, moduleName);
25272526
}
2528-
// TODO: do we need to sort by aggregate length?
25292527
const pathPatterns = tryParsePatterns(versionPaths.paths);
25302528
const result = tryLoadModuleUsingPaths(extensions, moduleName, candidate, versionPaths.paths, pathPatterns, loader, onlyRecordFailuresForPackageFile || onlyRecordFailuresForIndex, state);
25312529
if (result) {
@@ -3121,7 +3119,6 @@ function loadModuleFromSpecificNodeModulesDirectory(extensions: Extensions, modu
31213119
trace(state.host, Diagnostics.package_json_has_a_typesVersions_entry_0_that_matches_compiler_version_1_looking_for_a_pattern_to_match_module_name_2, versionPaths.version, version, rest);
31223120
}
31233121
const packageDirectoryExists = nodeModulesDirectoryExists && directoryProbablyExists(packageDirectory, state.host);
3124-
// TODO: do we need to sort by aggregate length?
31253122
const pathPatterns = tryParsePatterns(versionPaths.paths);
31263123
const fromPaths = tryLoadModuleUsingPaths(extensions, rest, packageDirectory, versionPaths.paths, pathPatterns, loader, !packageDirectoryExists, state);
31273124
if (fromPaths) {

src/compiler/utilities.ts

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10006,26 +10006,25 @@ export function tryParsePattern(pattern: string): string | Pattern | undefined {
1000610006
/** @internal */
1000710007
export interface ParsedPatterns {
1000810008
matchableStringSet: ReadonlySet<string> | undefined;
10009-
sortedPatterns: (readonly Pattern[]) | undefined;
10009+
patterns: (readonly Pattern[]) | undefined;
1001010010
}
1001110011

1001210012
const parsedPatternsCache = new WeakMap<MapLike<string[]>, ParsedPatterns>();
1001310013

1001410014
/**
10015-
* Divides patterns into a set of exact specifiers and sorted patterns.
10016-
* NOTE that this function caches, and assumes the same `paths` argument will
10017-
* never be provided with a different value for `sortByAggregateLength`.
10015+
* Divides patterns into a set of exact specifiers and patterns.
10016+
* NOTE that this function caches the result based on object identity.
1001810017
*
1001910018
* @internal
1002010019
*/
10021-
export function tryParsePatterns(paths: MapLike<string[]>, sortByAggregateLength: boolean = false): ParsedPatterns {
10020+
export function tryParsePatterns(paths: MapLike<string[]>): ParsedPatterns {
1002210021
let result = parsedPatternsCache.get(paths);
1002310022
if (result !== undefined) {
1002410023
return result;
1002510024
}
1002610025

1002710026
let matchableStringSet: Set<string> | undefined;
10028-
let sortedPatterns: Pattern[] | undefined;
10027+
let patterns: Pattern[] | undefined;
1002910028

1003010029
const pathList = getOwnKeys(paths);
1003110030
for (const path of pathList) {
@@ -10037,23 +10036,15 @@ export function tryParsePatterns(paths: MapLike<string[]>, sortByAggregateLength
1003710036
(matchableStringSet ??= new Set()).add(patternOrStr);
1003810037
}
1003910038
else {
10040-
(sortedPatterns ??= []).push(patternOrStr);
10039+
(patterns ??= []).push(patternOrStr);
1004110040
}
1004210041
}
1004310042

10044-
sortedPatterns?.sort((a, b) => {
10045-
const prefixComparison = compareStringsCaseSensitive(a.prefix, b.prefix);
10046-
if (prefixComparison === 0 && sortByAggregateLength) {
10047-
return a.suffix.length - b.suffix.length;
10048-
}
10049-
return prefixComparison;
10050-
});
10051-
1005210043
parsedPatternsCache.set(
1005310044
paths,
1005410045
result = {
1005510046
matchableStringSet,
10056-
sortedPatterns,
10047+
patterns,
1005710048
},
1005810049
);
1005910050

@@ -10121,17 +10112,17 @@ export const emptyFileSystemEntries: FileSystemEntries = {
1012110112
* @internal
1012210113
*/
1012310114
export function matchPatternOrExact(patternOrStrings: ParsedPatterns, candidate: string): string | Pattern | undefined {
10124-
const { matchableStringSet, sortedPatterns } = patternOrStrings;
10115+
const { matchableStringSet, patterns } = patternOrStrings;
1012510116

1012610117
if (matchableStringSet?.has(candidate)) {
1012710118
return candidate;
1012810119
}
1012910120

10130-
if (sortedPatterns === undefined || sortedPatterns.length === 0) {
10121+
if (patterns === undefined || patterns.length === 0) {
1013110122
return undefined;
1013210123
}
1013310124

10134-
return findBestPatternMatch(sortedPatterns, _ => _, candidate);
10125+
return findBestPatternMatch(patterns, _ => _, candidate);
1013510126
}
1013610127

1013710128
/** @internal */

0 commit comments

Comments
 (0)