Skip to content

Commit b62739c

Browse files
BridgeARBethGriggs
authored andcommitted
path: remove dead code
A couple of code parts could not be reached due to resolving the path in the beginning. That "normalizes" the path in a way that some code branches became obsolete. PR-URL: #26916 Reviewed-By: Sam Roberts <vieuxtech@gmail.com> Reviewed-By: Michaël Zasso <targos@protonmail.com> Signed-off-by: Beth Griggs <Bethany.Griggs@uk.ibm.com>
1 parent 1aa6e99 commit b62739c

File tree

1 file changed

+14
-37
lines changed

1 file changed

+14
-37
lines changed

lib/path.js

+14-37
Original file line numberDiff line numberDiff line change
@@ -1054,29 +1054,18 @@ const posix = {
10541054
if (from === to)
10551055
return '';
10561056

1057+
// Trim leading forward slashes.
10571058
from = posix.resolve(from);
10581059
to = posix.resolve(to);
10591060

10601061
if (from === to)
10611062
return '';
10621063

1063-
// Trim any leading backslashes
1064-
let fromStart = 1;
1065-
while (fromStart < from.length &&
1066-
from.charCodeAt(fromStart) === CHAR_FORWARD_SLASH) {
1067-
fromStart++;
1068-
}
1064+
const fromStart = 1;
10691065
const fromEnd = from.length;
1070-
const fromLen = (fromEnd - fromStart);
1071-
1072-
// Trim any leading backslashes
1073-
let toStart = 1;
1074-
while (toStart < to.length &&
1075-
to.charCodeAt(toStart) === CHAR_FORWARD_SLASH) {
1076-
toStart++;
1077-
}
1078-
const toEnd = to.length;
1079-
const toLen = (toEnd - toStart);
1066+
const fromLen = fromEnd - fromStart;
1067+
const toStart = 1;
1068+
const toLen = to.length - toStart;
10801069

10811070
// Compare paths to find the longest common path from root
10821071
const length = (fromLen < toLen ? fromLen : toLen);
@@ -1101,38 +1090,26 @@ const posix = {
11011090
// For example: from='/'; to='/foo'
11021091
return to.slice(toStart + i);
11031092
}
1104-
} else if (fromLen > length) {
1105-
if (from.charCodeAt(fromStart + i) === CHAR_FORWARD_SLASH) {
1106-
// We get here if `to` is the exact base path for `from`.
1107-
// For example: from='/foo/bar/baz'; to='/foo/bar'
1108-
lastCommonSep = i;
1109-
} else if (i === 0) {
1110-
// We get here if `to` is the root.
1111-
// For example: from='/foo'; to='/'
1112-
lastCommonSep = 0;
1113-
}
1093+
} else if (fromLen > length &&
1094+
from.charCodeAt(fromStart + i) === CHAR_FORWARD_SLASH) {
1095+
// We get here if `to` is the exact base path for `from`.
1096+
// For example: from='/foo/bar/baz'; to='/foo/bar'
1097+
lastCommonSep = i;
11141098
}
11151099
}
11161100

1117-
var out = '';
1101+
let out = '';
11181102
// Generate the relative path based on the path difference between `to`
1119-
// and `from`
1103+
// and `from`.
11201104
for (i = fromStart + lastCommonSep + 1; i <= fromEnd; ++i) {
11211105
if (i === fromEnd || from.charCodeAt(i) === CHAR_FORWARD_SLASH) {
11221106
out += out.length === 0 ? '..' : '/..';
11231107
}
11241108
}
11251109

1126-
toStart += lastCommonSep;
1127-
11281110
// Lastly, append the rest of the destination (`to`) path that comes after
1129-
// the common path parts
1130-
if (out.length > 0)
1131-
return `${out}${to.slice(toStart)}`;
1132-
1133-
if (to.charCodeAt(toStart) === CHAR_FORWARD_SLASH)
1134-
++toStart;
1135-
return to.slice(toStart);
1111+
// the common path parts.
1112+
return `${out}${to.slice(toStart + lastCommonSep)}`;
11361113
},
11371114

11381115
toNamespacedPath(path) {

0 commit comments

Comments
 (0)