path: refactor path joining logic for clarity and performance

PR-URL: https://github.com/nodejs/node/pull/59781
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Minwoo Jung <nodecorelab@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
This commit is contained in:
Lee Jiho 2025-09-09 03:44:18 +09:00 committed by GitHub
parent 9d1c9c789b
commit 26607a650e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -24,6 +24,7 @@
const {
ArrayPrototypeIncludes,
ArrayPrototypeJoin,
ArrayPrototypePush,
ArrayPrototypeSlice,
FunctionPrototypeBind,
StringPrototypeCharCodeAt,
@ -506,22 +507,21 @@ const win32 = {
if (args.length === 0)
return '.';
let joined;
let firstPart;
const path = [];
for (let i = 0; i < args.length; ++i) {
const arg = args[i];
validateString(arg, 'path');
if (arg.length > 0) {
if (joined === undefined)
joined = firstPart = arg;
else
joined += `\\${arg}`;
ArrayPrototypePush(path, arg);
}
}
if (joined === undefined)
if (path.length === 0)
return '.';
const firstPart = path[0];
let joined = ArrayPrototypeJoin(path, '\\');
// Make sure that the joined path doesn't start with two slashes, because
// normalize() will mistake it for a UNC path then.
//