path: fix posix.relative() on Windows

Fixes: https://github.com/nodejs/node/issues/13683

PR-URL: https://github.com/nodejs/node/pull/37747
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
Rich Trott
2021-03-13 18:33:07 -08:00
parent e46c680bf2
commit b0d5e036d8
4 changed files with 25 additions and 17 deletions

View File

@@ -23,11 +23,15 @@
const {
FunctionPrototypeBind,
RegExp,
StringPrototypeCharCodeAt,
StringPrototypeIndexOf,
StringPrototypeLastIndexOf,
StringPrototypeReplace,
StringPrototypeSlice,
StringPrototypeToLowerCase,
} = primordials;
const {
CHAR_UPPERCASE_A,
CHAR_LOWERCASE_A,
@@ -1014,7 +1018,17 @@ const posix = {
let resolvedAbsolute = false;
for (let i = args.length - 1; i >= -1 && !resolvedAbsolute; i--) {
const path = i >= 0 ? args[i] : process.cwd();
let path;
if (i >= 0) {
path = args[i];
} else {
const _ = StringPrototypeReplace(
process.cwd(),
new RegExp(`\\${module.exports.sep}`, 'g'),
posix.sep
);
path = StringPrototypeSlice(_, StringPrototypeIndexOf(_, posix.sep));
}
validateString(path, 'path');