From 94b522acfd587562931c15bd66b0735c3e5d66c4 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Thu, 11 Jul 2024 22:19:06 +0000 Subject: [PATCH] Just separate patterns into exact strings and maches. Remove all sorting logic. --- src/compiler/moduleNameResolver.ts | 3 --- src/compiler/utilities.ts | 29 ++++++++++------------------- 2 files changed, 10 insertions(+), 22 deletions(-) diff --git a/src/compiler/moduleNameResolver.ts b/src/compiler/moduleNameResolver.ts index 5291540dee4e4..46bda7af60046 100644 --- a/src/compiler/moduleNameResolver.ts +++ b/src/compiler/moduleNameResolver.ts @@ -1568,7 +1568,6 @@ function tryLoadModuleUsingPathsIfEligible(extensions: Extensions, moduleName: s trace(state.host, Diagnostics.paths_option_is_specified_looking_for_a_pattern_to_match_module_name_0, moduleName); } const baseDirectory = getPathsBasePath(state.compilerOptions, state.host)!; // Always defined when 'paths' is defined - // TODO: do we need to sort by aggregate length? const pathPatterns = tryParsePatterns(paths); return tryLoadModuleUsingPaths(extensions, moduleName, baseDirectory, paths, pathPatterns, loader, /*onlyRecordFailures*/ false, state); } @@ -2525,7 +2524,6 @@ function loadNodeModuleFromDirectoryWorker(extensions: Extensions, candidate: st if (state.traceEnabled) { 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); } - // TODO: do we need to sort by aggregate length? const pathPatterns = tryParsePatterns(versionPaths.paths); const result = tryLoadModuleUsingPaths(extensions, moduleName, candidate, versionPaths.paths, pathPatterns, loader, onlyRecordFailuresForPackageFile || onlyRecordFailuresForIndex, state); if (result) { @@ -3121,7 +3119,6 @@ function loadModuleFromSpecificNodeModulesDirectory(extensions: Extensions, modu 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); } const packageDirectoryExists = nodeModulesDirectoryExists && directoryProbablyExists(packageDirectory, state.host); - // TODO: do we need to sort by aggregate length? const pathPatterns = tryParsePatterns(versionPaths.paths); const fromPaths = tryLoadModuleUsingPaths(extensions, rest, packageDirectory, versionPaths.paths, pathPatterns, loader, !packageDirectoryExists, state); if (fromPaths) { diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 591e5be1644dc..424d21d1bdfb6 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -10006,26 +10006,25 @@ export function tryParsePattern(pattern: string): string | Pattern | undefined { /** @internal */ export interface ParsedPatterns { matchableStringSet: ReadonlySet | undefined; - sortedPatterns: (readonly Pattern[]) | undefined; + patterns: (readonly Pattern[]) | undefined; } const parsedPatternsCache = new WeakMap, ParsedPatterns>(); /** - * Divides patterns into a set of exact specifiers and sorted patterns. - * NOTE that this function caches, and assumes the same `paths` argument will - * never be provided with a different value for `sortByAggregateLength`. + * Divides patterns into a set of exact specifiers and patterns. + * NOTE that this function caches the result based on object identity. * * @internal */ -export function tryParsePatterns(paths: MapLike, sortByAggregateLength: boolean = false): ParsedPatterns { +export function tryParsePatterns(paths: MapLike): ParsedPatterns { let result = parsedPatternsCache.get(paths); if (result !== undefined) { return result; } let matchableStringSet: Set | undefined; - let sortedPatterns: Pattern[] | undefined; + let patterns: Pattern[] | undefined; const pathList = getOwnKeys(paths); for (const path of pathList) { @@ -10037,23 +10036,15 @@ export function tryParsePatterns(paths: MapLike, sortByAggregateLength (matchableStringSet ??= new Set()).add(patternOrStr); } else { - (sortedPatterns ??= []).push(patternOrStr); + (patterns ??= []).push(patternOrStr); } } - sortedPatterns?.sort((a, b) => { - const prefixComparison = compareStringsCaseSensitive(a.prefix, b.prefix); - if (prefixComparison === 0 && sortByAggregateLength) { - return a.suffix.length - b.suffix.length; - } - return prefixComparison; - }); - parsedPatternsCache.set( paths, result = { matchableStringSet, - sortedPatterns, + patterns, }, ); @@ -10121,17 +10112,17 @@ export const emptyFileSystemEntries: FileSystemEntries = { * @internal */ export function matchPatternOrExact(patternOrStrings: ParsedPatterns, candidate: string): string | Pattern | undefined { - const { matchableStringSet, sortedPatterns } = patternOrStrings; + const { matchableStringSet, patterns } = patternOrStrings; if (matchableStringSet?.has(candidate)) { return candidate; } - if (sortedPatterns === undefined || sortedPatterns.length === 0) { + if (patterns === undefined || patterns.length === 0) { return undefined; } - return findBestPatternMatch(sortedPatterns, _ => _, candidate); + return findBestPatternMatch(patterns, _ => _, candidate); } /** @internal */