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: https://github.com/nodejs/node/pull/26916
Reviewed-By: Sam Roberts <vieuxtech@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
This commit is contained in:
Ruben Bridgewater 2019-03-26 06:48:17 +01:00
parent 520b3e63ea
commit 8df9fdcc39
No known key found for this signature in database
GPG Key ID: F07496B3EB3C1762

View File

@ -1054,29 +1054,18 @@ const posix = {
if (from === to) if (from === to)
return ''; return '';
// Trim leading forward slashes.
from = posix.resolve(from); from = posix.resolve(from);
to = posix.resolve(to); to = posix.resolve(to);
if (from === to) if (from === to)
return ''; return '';
// Trim any leading backslashes const fromStart = 1;
let fromStart = 1;
while (fromStart < from.length &&
from.charCodeAt(fromStart) === CHAR_FORWARD_SLASH) {
fromStart++;
}
const fromEnd = from.length; const fromEnd = from.length;
const fromLen = (fromEnd - fromStart); const fromLen = fromEnd - fromStart;
const toStart = 1;
// Trim any leading backslashes const toLen = to.length - toStart;
let toStart = 1;
while (toStart < to.length &&
to.charCodeAt(toStart) === CHAR_FORWARD_SLASH) {
toStart++;
}
const toEnd = to.length;
const toLen = (toEnd - toStart);
// Compare paths to find the longest common path from root // Compare paths to find the longest common path from root
const length = (fromLen < toLen ? fromLen : toLen); const length = (fromLen < toLen ? fromLen : toLen);
@ -1101,38 +1090,26 @@ const posix = {
// For example: from='/'; to='/foo' // For example: from='/'; to='/foo'
return to.slice(toStart + i); return to.slice(toStart + i);
} }
} else if (fromLen > length) { } else if (fromLen > length &&
if (from.charCodeAt(fromStart + i) === CHAR_FORWARD_SLASH) { from.charCodeAt(fromStart + i) === CHAR_FORWARD_SLASH) {
// We get here if `to` is the exact base path for `from`. // We get here if `to` is the exact base path for `from`.
// For example: from='/foo/bar/baz'; to='/foo/bar' // For example: from='/foo/bar/baz'; to='/foo/bar'
lastCommonSep = i; lastCommonSep = i;
} else if (i === 0) {
// We get here if `to` is the root.
// For example: from='/foo'; to='/'
lastCommonSep = 0;
}
} }
} }
var out = ''; let out = '';
// Generate the relative path based on the path difference between `to` // Generate the relative path based on the path difference between `to`
// and `from` // and `from`.
for (i = fromStart + lastCommonSep + 1; i <= fromEnd; ++i) { for (i = fromStart + lastCommonSep + 1; i <= fromEnd; ++i) {
if (i === fromEnd || from.charCodeAt(i) === CHAR_FORWARD_SLASH) { if (i === fromEnd || from.charCodeAt(i) === CHAR_FORWARD_SLASH) {
out += out.length === 0 ? '..' : '/..'; out += out.length === 0 ? '..' : '/..';
} }
} }
toStart += lastCommonSep;
// Lastly, append the rest of the destination (`to`) path that comes after // Lastly, append the rest of the destination (`to`) path that comes after
// the common path parts // the common path parts.
if (out.length > 0) return `${out}${to.slice(toStart + lastCommonSep)}`;
return `${out}${to.slice(toStart)}`;
if (to.charCodeAt(toStart) === CHAR_FORWARD_SLASH)
++toStart;
return to.slice(toStart);
}, },
toNamespacedPath(path) { toNamespacedPath(path) {