Skip to content

Commit 44f665b

Browse files
committed
Fix tests and improve based on benchmarks
1 parent 9ab5fbd commit 44f665b

File tree

1 file changed

+20
-24
lines changed

1 file changed

+20
-24
lines changed

src/compiler/path.ts

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -630,50 +630,46 @@ export function getNormalizedAbsolutePath(path: string, currentDirectory: string
630630
path = combinePaths(currentDirectory, path);
631631
rootLength = getRootLength(path);
632632
}
633-
const simple = simpleNormalizePath(path);
634-
if (simple !== undefined) {
635-
return simple;
633+
else {
634+
// combinePaths normalizes slashes, so not necessary in the other branch
635+
path = normalizeSlashes(path);
636636
}
637+
638+
const simpleNormalized = simpleNormalizePath(path);
639+
if (simpleNormalized !== undefined) {
640+
return simpleNormalized.length > rootLength ? removeTrailingDirectorySeparator(simpleNormalized) : simpleNormalized;
641+
}
642+
643+
const length = path.length;
637644
const root = path.substring(0, rootLength);
638-
const normalizedRoot = root && normalizeSlashes(root);
639645
// `normalized` is only initialized once `path` is determined to be non-normalized
640-
let normalized = normalizedRoot === root ? undefined : normalizedRoot;
646+
let normalized;
641647
let index = rootLength;
642648
let segmentStart = index;
643649
let normalizedUpTo = index;
644650
let seenNonDotDotSegment = rootLength !== 0;
645-
while (index < path.length) {
651+
while (index < length) {
646652
// At beginning of segment
647653
segmentStart = index;
648654
let ch = path.charCodeAt(index);
649-
while (isAnyDirectorySeparator(ch) && index + 1 < path.length) {
655+
while (ch === CharacterCodes.slash && index + 1 < length) {
650656
index++;
651657
ch = path.charCodeAt(index);
652658
}
653659
if (index > segmentStart) {
654-
if (normalized === undefined) {
655-
// Seen superfluous separator
656-
normalized = path.substring(0, segmentStart - 1);
657-
}
660+
// Seen superfluous separator
661+
normalized ??= path.substring(0, segmentStart - 1);
658662
segmentStart = index;
659663
}
660664
// Past any superfluous separators
661-
const sepIndex = path.indexOf(directorySeparator, index + 1);
662-
const altSepIndex = path.indexOf(altDirectorySeparator, index + 1);
663-
let segmentEnd = sepIndex === -1 ? altSepIndex : altSepIndex === -1 ? sepIndex : Math.min(sepIndex, altSepIndex);
665+
let segmentEnd = path.indexOf(directorySeparator, index + 1);
664666
if (segmentEnd === -1) {
665-
segmentEnd = path.length;
666-
}
667-
if (segmentEnd === altSepIndex && normalized === undefined) {
668-
// Seen backslash
669-
normalized = path.substring(0, segmentStart);
667+
segmentEnd = length;
670668
}
671669
const segmentLength = segmentEnd - segmentStart;
672670
if (segmentLength === 1 && path.charCodeAt(index) === CharacterCodes.dot) {
673671
// "." segment (skip)
674-
if (normalized === undefined) {
675-
normalized = path.substring(0, normalizedUpTo);
676-
}
672+
normalized ??= path.substring(0, normalizedUpTo);
677673
}
678674
else if (segmentLength === 2 && path.charCodeAt(index) === CharacterCodes.dot && path.charCodeAt(index + 1) === CharacterCodes.dot) {
679675
// ".." segment
@@ -699,7 +695,7 @@ export function getNormalizedAbsolutePath(path: string, currentDirectory: string
699695
normalized = normalized.substring(0, Math.max(rootLength, lastSlash));
700696
}
701697
else {
702-
normalized = normalizedRoot;
698+
normalized = root;
703699
}
704700
if (normalized.length === rootLength) {
705701
seenNonDotDotSegment = rootLength !== 0;
@@ -719,7 +715,7 @@ export function getNormalizedAbsolutePath(path: string, currentDirectory: string
719715
}
720716
index = segmentEnd + 1;
721717
}
722-
return normalized ?? (path.length > rootLength ? removeTrailingDirectorySeparator(path) : path);
718+
return normalized ?? (length > rootLength ? removeTrailingDirectorySeparator(path) : path);
723719
}
724720

725721
/** @internal */

0 commit comments

Comments
 (0)