Skip to content

Commit 4db7c86

Browse files
committed
restore old algorithm
1 parent d2dc17d commit 4db7c86

File tree

1 file changed

+17
-14
lines changed

1 file changed

+17
-14
lines changed

src/compiler/moduleNameResolver.ts

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -423,21 +423,24 @@ namespace ts {
423423
function getCommonPrefix(directory: Path, resolution: string) {
424424
const resolutionDirectory = toPath(getDirectoryPath(resolution), currentDirectory, getCanonicalFileName);
425425

426-
let current = directory;
427-
let parent = getDirectoryPath(current);
428-
while (
429-
// keep going until we find a matching prefix
430-
!startsWith(resolutionDirectory, current) ||
431-
// keep going if the prefix is not a complete directory segment, e.g. '/dir' as prefix of '/directory'
432-
resolutionDirectory.length > current.length && resolutionDirectory[current.length] !== directorySeparator && current !== parent
433-
) {
434-
if (current === parent) {
435-
return undefined;
436-
}
437-
current = parent;
438-
parent = getDirectoryPath(current);
426+
// find first position where directory and resolution differs
427+
let i = 0;
428+
const limit = Math.min(directory.length, resolutionDirectory.length);
429+
while (i < limit && directory.charCodeAt(i) === resolutionDirectory.charCodeAt(i)) {
430+
i++;
431+
}
432+
if (i === directory.length && (resolutionDirectory.length === i || resolutionDirectory[i] === directorySeparator)) {
433+
return directory;
434+
}
435+
const rootLength = getRootLength(directory);
436+
if (i < rootLength) {
437+
return undefined;
438+
}
439+
const sep = directory.lastIndexOf(directorySeparator, i - 1);
440+
if (sep === -1) {
441+
return undefined;
439442
}
440-
return current;
443+
return directory.substr(0, Math.max(sep, rootLength));
441444
}
442445
}
443446
}

0 commit comments

Comments
 (0)