Skip to content

Commit

Permalink
Restore regex fast path
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewbranch committed Dec 19, 2024
1 parent 2a880da commit 9ab5fbd
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/compiler/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,10 @@ export function getNormalizedAbsolutePath(path: string, currentDirectory: string
path = combinePaths(currentDirectory, path);
rootLength = getRootLength(path);
}
const simple = simpleNormalizePath(path);
if (simple !== undefined) {
return simple;
}
const root = path.substring(0, rootLength);
const normalizedRoot = root && normalizeSlashes(root);
// `normalized` is only initialized once `path` is determined to be non-normalized
Expand Down Expand Up @@ -720,10 +724,31 @@ export function getNormalizedAbsolutePath(path: string, currentDirectory: string

/** @internal */
export function normalizePath(path: string): string {
const normalized = getNormalizedAbsolutePath(path, "");
let normalized = simpleNormalizePath(path);
if (normalized !== undefined) {
return normalized;
}
normalized = getNormalizedAbsolutePath(path, "");
return normalized && hasTrailingDirectorySeparator(path) ? ensureTrailingDirectorySeparator(normalized) : normalized;
}

function simpleNormalizePath(path: string): string | undefined {
path = normalizeSlashes(path);
// Most paths don't require normalization
if (!relativePathSegmentRegExp.test(path)) {
return path;
}
// Some paths only require cleanup of `/./` or leading `./`
const simplified = path.replace(/\/\.\//g, "/").replace(/^\.\//, "");
if (simplified !== path) {
path = simplified;
if (!relativePathSegmentRegExp.test(path)) {
return path;
}
}
return undefined;
}

function getPathWithoutRoot(pathComponents: readonly string[]) {
if (pathComponents.length === 0) return "";
return pathComponents.slice(1).join(directorySeparator);
Expand Down

0 comments on commit 9ab5fbd

Please sign in to comment.