From 90cce6ec7cb628ba6e39be08f5cf19f29572cdd9 Mon Sep 17 00:00:00 2001 From: Wiyeong Seo Date: Wed, 18 Sep 2024 21:18:24 +0900 Subject: [PATCH] path: remove repetitive conditional operator in `posix.resolve` PR-URL: https://github.com/nodejs/node/pull/54835 Reviewed-By: Benjamin Gruenbaum Reviewed-By: Yagiz Nizipli Reviewed-By: James M Snell --- lib/path.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/path.js b/lib/path.js index e9b56e89a40ee7..eefa9bc5db0729 100644 --- a/lib/path.js +++ b/lib/path.js @@ -1163,8 +1163,8 @@ const posix = { let resolvedPath = ''; let resolvedAbsolute = false; - for (let i = args.length - 1; i >= -1 && !resolvedAbsolute; i--) { - const path = i >= 0 ? args[i] : posixCwd(); + for (let i = args.length - 1; i >= 0 && !resolvedAbsolute; i--) { + const path = args[i]; validateString(path, `paths[${i}]`); // Skip empty entries @@ -1177,6 +1177,13 @@ const posix = { 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 // handle relative paths to be safe (might happen when process.cwd() fails)