Skip to content

Commit

Permalink
Just separate patterns into exact strings and maches. Remove all sort…
Browse files Browse the repository at this point in the history
…ing logic.
  • Loading branch information
DanielRosenwasser committed Jul 11, 2024
1 parent 012259e commit 94b522a
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 22 deletions.
3 changes: 0 additions & 3 deletions src/compiler/moduleNameResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
29 changes: 10 additions & 19 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10006,26 +10006,25 @@ export function tryParsePattern(pattern: string): string | Pattern | undefined {
/** @internal */
export interface ParsedPatterns {
matchableStringSet: ReadonlySet<string> | undefined;
sortedPatterns: (readonly Pattern[]) | undefined;
patterns: (readonly Pattern[]) | undefined;
}

const parsedPatternsCache = new WeakMap<MapLike<string[]>, 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<string[]>, sortByAggregateLength: boolean = false): ParsedPatterns {
export function tryParsePatterns(paths: MapLike<string[]>): ParsedPatterns {
let result = parsedPatternsCache.get(paths);
if (result !== undefined) {
return result;
}

let matchableStringSet: Set<string> | undefined;
let sortedPatterns: Pattern[] | undefined;
let patterns: Pattern[] | undefined;

const pathList = getOwnKeys(paths);
for (const path of pathList) {
Expand All @@ -10037,23 +10036,15 @@ export function tryParsePatterns(paths: MapLike<string[]>, 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,
},
);

Expand Down Expand Up @@ -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 */
Expand Down

0 comments on commit 94b522a

Please sign in to comment.