path: remove repetitive conditional operator in posix.resolve

PR-URL: https://github.com/nodejs/node/pull/54835
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
This commit is contained in:
Wiyeong Seo 2024-09-18 21:18:24 +09:00 committed by GitHub
parent d496d44145
commit e42ca5c1a9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1192,8 +1192,8 @@ const posix = {
let resolvedAbsolute = false; let resolvedAbsolute = false;
let slashCheck = false; let slashCheck = false;
for (let i = args.length - 1; i >= -1 && !resolvedAbsolute; i--) { for (let i = args.length - 1; i >= 0 && !resolvedAbsolute; i--) {
const path = i >= 0 ? args[i] : posixCwd(); const path = args[i];
validateString(path, `paths[${i}]`); validateString(path, `paths[${i}]`);
// Skip empty entries // Skip empty entries
@ -1215,6 +1215,13 @@ const posix = {
StringPrototypeCharCodeAt(path, 0) === CHAR_FORWARD_SLASH; StringPrototypeCharCodeAt(path, 0) === CHAR_FORWARD_SLASH;
} }
if (!resolvedAbsolute) {
const cwd = posixCwd();
resolvedPath = `${cwd}/${resolvedPath}`;
resolvedAbsolute =
StringPrototypeCharCodeAt(cwd, 0) === CHAR_FORWARD_SLASH;
}
// At this point the path should be resolved to a full absolute path, but // At this point the path should be resolved to a full absolute path, but
// handle relative paths to be safe (might happen when process.cwd() fails) // handle relative paths to be safe (might happen when process.cwd() fails)