Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f296a7f

Browse filesBrowse files
omsmithsilverwind
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 4d6b4c3 commit f296a7f
Copy full SHA for f296a7f

File tree

Expand file treeCollapse file tree

2 files changed

+30
-14
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+30
-14
lines changed
Open diff view settings
Collapse file

‎lib/path.js‎

Copy file name to clipboardExpand all lines: lib/path.js
+22-12Lines changed: 22 additions & 12 deletions
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;
@@ -1300,16 +1300,26 @@ const posix = {
13001300
var i = 0;
13011301
for (; i <= length; ++i) {
13021302
if (i === length) {
1303-
if (lastCommonSep === -1) {
1304-
lastCommonSep = i;
1305-
} else if (toLen > length && to.charCodeAt(i + 1) === 47/*/*/) {
1306-
// We get here if `from` is the exact base path for `to`.
1307-
// For example: from='/foo/bar'; to='/foo/bar/baz'
1308-
return to.slice(i + 2);
1309-
} else if (fromLen > length && from.charCodeAt(i + 1) === 47/*/*/) {
1310-
// We get here if `to` is the exact base path for `from`.
1311-
// For example: from='/foo/bar/baz'; to='/foo/bar'
1312-
lastCommonSep = i;
1303+
if (toLen > length) {
1304+
if (to.charCodeAt(toStart + i) === 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(toStart + i + 1);
1308+
} else if (i === 0) {
1309+
// We get here if `from` is the root
1310+
// For example: from='/'; to='/foo'
1311+
return to.slice(toStart + i);
1312+
}
1313+
} else if (fromLen > length) {
1314+
if (from.charCodeAt(fromStart + i) === 47/*/*/) {
1315+
// We get here if `to` is the exact base path for `from`.
1316+
// For example: from='/foo/bar/baz'; to='/foo/bar'
1317+
lastCommonSep = i;
1318+
} else if (i === 0) {
1319+
// We get here if `to` is the root.
1320+
// For example: from='/foo'; to='/'
1321+
lastCommonSep = 0;
1322+
}
13131323
}
13141324
break;
13151325
}
Collapse file

‎test/parallel/test-path.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-path.js
+8-2Lines changed: 8 additions & 2 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.