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: fix regression in posix.normalize #19520

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 12 additions & 5 deletions lib/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,17 @@ function isPathSeparator(code) {
return code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH;
}

function isPosixPathSeparator(code) {
return code === CHAR_FORWARD_SLASH;
}

function isWindowsDeviceRoot(code) {
return code >= CHAR_UPPERCASE_A && code <= CHAR_UPPERCASE_Z ||
code >= CHAR_LOWERCASE_A && code <= CHAR_LOWERCASE_Z;
}

// Resolves . and .. elements in a path with directory names
function normalizeString(path, allowAboveRoot, separator) {
function normalizeString(path, allowAboveRoot, separator, isPathSeparator) {
var res = '';
var lastSegmentLength = 0;
var lastSlash = -1;
Expand Down Expand Up @@ -272,7 +276,8 @@ const win32 = {
// fails)

// Normalize the tail path
resolvedTail = normalizeString(resolvedTail, !resolvedAbsolute, '\\');
resolvedTail = normalizeString(resolvedTail, !resolvedAbsolute, '\\',
isPathSeparator);

return (resolvedDevice + (resolvedAbsolute ? '\\' : '') + resolvedTail) ||
'.';
Expand Down Expand Up @@ -364,7 +369,8 @@ const win32 = {

var tail;
if (rootEnd < len)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can braces be added for the if and else here if we start having multi-line conditional bodies?

tail = normalizeString(path.slice(rootEnd), !isAbsolute, '\\');
tail = normalizeString(path.slice(rootEnd), !isAbsolute, '\\',
isPathSeparator);
else
tail = '';
if (tail.length === 0 && !isAbsolute)
Expand Down Expand Up @@ -1095,7 +1101,8 @@ const posix = {
// handle relative paths to be safe (might happen when process.cwd() fails)

// Normalize the path
resolvedPath = normalizeString(resolvedPath, !resolvedAbsolute, '/');
resolvedPath = normalizeString(resolvedPath, !resolvedAbsolute, '/',
isPosixPathSeparator);

if (resolvedAbsolute) {
if (resolvedPath.length > 0)
Expand All @@ -1121,7 +1128,7 @@ const posix = {
path.charCodeAt(path.length - 1) === CHAR_FORWARD_SLASH;

// Normalize the path
path = normalizeString(path, !isAbsolute, '/');
path = normalizeString(path, !isAbsolute, '/', isPosixPathSeparator);

if (path.length === 0 && !isAbsolute)
path = '.';
Expand Down
2 changes: 2 additions & 0 deletions test/parallel/test-path-normalize.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ assert.strictEqual(
path.win32.normalize('../.../../foobar/../../../bar/../../baz'),
'..\\..\\..\\..\\baz'
);
assert.strictEqual(path.win32.normalize('foo/bar\\baz'), 'foo\\bar\\baz');

assert.strictEqual(path.posix.normalize('./fixtures///b/../b/c.js'),
'fixtures/b/c.js');
Expand Down Expand Up @@ -68,3 +69,4 @@ assert.strictEqual(
path.posix.normalize('../.../../foobar/../../../bar/../../baz'),
'../../../../baz'
);
assert.strictEqual(path.posix.normalize('foo/bar\\baz'), 'foo/bar\\baz');