Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

path: remove dead code #26916

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 14 additions & 37 deletions lib/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -1053,29 +1053,18 @@ const posix = {
if (from === to)
return '';

// Trim leading forward slashes.
from = posix.resolve(from);
to = posix.resolve(to);

if (from === to)
return '';

// Trim any leading backslashes
let fromStart = 1;
while (fromStart < from.length &&
from.charCodeAt(fromStart) === CHAR_FORWARD_SLASH) {
fromStart++;
}
const fromStart = 1;
const fromEnd = from.length;
const fromLen = (fromEnd - fromStart);

// Trim any leading backslashes
let toStart = 1;
while (toStart < to.length &&
to.charCodeAt(toStart) === CHAR_FORWARD_SLASH) {
toStart++;
}
const toEnd = to.length;
const toLen = (toEnd - toStart);
const fromLen = fromEnd - fromStart;
const toStart = 1;
const toLen = to.length - toStart;

// Compare paths to find the longest common path from root
const length = (fromLen < toLen ? fromLen : toLen);
Expand All @@ -1100,38 +1089,26 @@ const posix = {
// For example: from='/'; to='/foo'
return to.slice(toStart + i);
}
} else if (fromLen > length) {
if (from.charCodeAt(fromStart + i) === CHAR_FORWARD_SLASH) {
// We get here if `to` is the exact base path for `from`.
// For example: from='/foo/bar/baz'; to='/foo/bar'
lastCommonSep = i;
} else if (i === 0) {
// We get here if `to` is the root.
// For example: from='/foo'; to='/'
lastCommonSep = 0;
}
} else if (fromLen > length &&
from.charCodeAt(fromStart + i) === CHAR_FORWARD_SLASH) {
// We get here if `to` is the exact base path for `from`.
// For example: from='/foo/bar/baz'; to='/foo/bar'
lastCommonSep = i;
}
}

var out = '';
let out = '';
// Generate the relative path based on the path difference between `to`
// and `from`
// and `from`.
for (i = fromStart + lastCommonSep + 1; i <= fromEnd; ++i) {
if (i === fromEnd || from.charCodeAt(i) === CHAR_FORWARD_SLASH) {
out += out.length === 0 ? '..' : '/..';
}
}

toStart += lastCommonSep;

// Lastly, append the rest of the destination (`to`) path that comes after
// the common path parts
if (out.length > 0)
return `${out}${to.slice(toStart)}`;

if (to.charCodeAt(toStart) === CHAR_FORWARD_SLASH)
++toStart;
return to.slice(toStart);
// the common path parts.
sam-github marked this conversation as resolved.
Show resolved Hide resolved
return `${out}${to.slice(toStart + lastCommonSep)}`;
},

toNamespacedPath(path) {
Expand Down