Skip to content

Commit 91d218d

Browse files
omsmithFishrock123
authored andcommitted
path: fix path.relative() for prefixes at root
Fixes #5485 PR-URL: #5490 Reviewed-By: Roman Reiss <me@silverwind.io> Reviewed-By: Brian White <mscdex@mscdex.net>
1 parent 0ebbf6c commit 91d218d

File tree

2 files changed

+30
-14
lines changed

2 files changed

+30
-14
lines changed

lib/path.js

+22-12
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,7 @@ const win32 = {
618618
// We get here if `from` is the exact base path for `to`.
619619
// For example: from='C:\\foo\\bar'; to='C:\\foo\\bar\\baz'
620620
return toOrig.slice(toStart + i + 1);
621-
} else if (lastCommonSep === 2) {
621+
} else if (i === 2) {
622622
// We get here if `from` is the device root.
623623
// For example: from='C:\\'; to='C:\\foo'
624624
return toOrig.slice(toStart + i);
@@ -629,7 +629,7 @@ const win32 = {
629629
// We get here if `to` is the exact base path for `from`.
630630
// For example: from='C:\\foo\\bar'; to='C:\\foo'
631631
lastCommonSep = i;
632-
} else if (lastCommonSep === 2) {
632+
} else if (i === 2) {
633633
// We get here if `to` is the device root.
634634
// For example: from='C:\\foo\\bar'; to='C:\\'
635635
lastCommonSep = 3;
@@ -1299,16 +1299,26 @@ const posix = {
12991299
var i = 0;
13001300
for (; i <= length; ++i) {
13011301
if (i === length) {
1302-
if (lastCommonSep === -1) {
1303-
lastCommonSep = i;
1304-
} else if (toLen > length && to.charCodeAt(i + 1) === 47/*/*/) {
1305-
// We get here if `from` is the exact base path for `to`.
1306-
// For example: from='/foo/bar'; to='/foo/bar/baz'
1307-
return to.slice(i + 2);
1308-
} else if (fromLen > length && from.charCodeAt(i + 1) === 47/*/*/) {
1309-
// We get here if `to` is the exact base path for `from`.
1310-
// For example: from='/foo/bar/baz'; to='/foo/bar'
1311-
lastCommonSep = i;
1302+
if (toLen > length) {
1303+
if (to.charCodeAt(toStart + i) === 47/*/*/) {
1304+
// We get here if `from` is the exact base path for `to`.
1305+
// For example: from='/foo/bar'; to='/foo/bar/baz'
1306+
return to.slice(toStart + i + 1);
1307+
} else if (i === 0) {
1308+
// We get here if `from` is the root
1309+
// For example: from='/'; to='/foo'
1310+
return to.slice(toStart + i);
1311+
}
1312+
} else if (fromLen > length) {
1313+
if (from.charCodeAt(fromStart + i) === 47/*/*/) {
1314+
// We get here if `to` is the exact base path for `from`.
1315+
// For example: from='/foo/bar/baz'; to='/foo/bar'
1316+
lastCommonSep = i;
1317+
} else if (i === 0) {
1318+
// We get here if `to` is the root.
1319+
// For example: from='/foo'; to='/'
1320+
lastCommonSep = 0;
1321+
}
13121322
}
13131323
break;
13141324
}

test/parallel/test-path.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,11 @@ const relativeTests = [
476476
['\\\\foo\\bar', '\\\\foo\\bar\\baz', 'baz'],
477477
['\\\\foo\\bar\\baz', '\\\\foo\\bar', '..'],
478478
['\\\\foo\\bar\\baz-quux', '\\\\foo\\bar\\baz', '..\\baz'],
479-
['\\\\foo\\bar\\baz', '\\\\foo\\bar\\baz-quux', '..\\baz-quux']
479+
['\\\\foo\\bar\\baz', '\\\\foo\\bar\\baz-quux', '..\\baz-quux'],
480+
['C:\\baz-quux', 'C:\\baz', '..\\baz'],
481+
['C:\\baz', 'C:\\baz-quux', '..\\baz-quux'],
482+
['\\\\foo\\baz-quux', '\\\\foo\\baz', '..\\baz'],
483+
['\\\\foo\\baz', '\\\\foo\\baz-quux', '..\\baz-quux']
480484
]
481485
],
482486
[ path.posix.relative,
@@ -490,7 +494,9 @@ const relativeTests = [
490494
['/foo/test', '/foo/test/bar/package.json', 'bar/package.json'],
491495
['/Users/a/web/b/test/mails', '/Users/a/web/b', '../..'],
492496
['/foo/bar/baz-quux', '/foo/bar/baz', '../baz'],
493-
['/foo/bar/baz', '/foo/bar/baz-quux', '../baz-quux']
497+
['/foo/bar/baz', '/foo/bar/baz-quux', '../baz-quux'],
498+
['/baz-quux', '/baz', '../baz'],
499+
['/baz', '/baz-quux', '../baz-quux']
494500
]
495501
]
496502
];

0 commit comments

Comments
 (0)