Skip to content

Commit 6c7cd9e

Browse files
BridgeARaddaleax
authored andcommitted
path: minor refactoring
1) Consolidate nested if statements if possible `if (foo) { if bar () { /* do stuff */ } }`) to reduce indentation depth. 2) Remove obsolete else cases to reduce indentation. PR-URL: #25278 Reviewed-By: Michaël Zasso <[email protected]>
1 parent cccc44b commit 6c7cd9e

File tree

1 file changed

+57
-68
lines changed

1 file changed

+57
-68
lines changed

lib/path.js

+57-68
Original file line numberDiff line numberDiff line change
@@ -206,20 +206,16 @@ const win32 = {
206206
} else {
207207
rootEnd = 1;
208208
}
209-
} else if (isWindowsDeviceRoot(code)) {
209+
} else if (isWindowsDeviceRoot(code) &&
210+
path.charCodeAt(1) === CHAR_COLON) {
210211
// Possible device root
211-
212-
if (path.charCodeAt(1) === CHAR_COLON) {
213-
device = path.slice(0, 2);
214-
rootEnd = 2;
215-
if (len > 2) {
216-
if (isPathSeparator(path.charCodeAt(2))) {
217-
// Treat separator following drive name as an absolute path
218-
// indicator
219-
isAbsolute = true;
220-
rootEnd = 3;
221-
}
222-
}
212+
device = path.slice(0, 2);
213+
rootEnd = 2;
214+
if (len > 2 && isPathSeparator(path.charCodeAt(2))) {
215+
// Treat separator following drive name as an absolute path
216+
// indicator
217+
isAbsolute = true;
218+
rootEnd = 3;
223219
}
224220
}
225221
} else if (isPathSeparator(code)) {
@@ -567,26 +563,23 @@ const win32 = {
567563

568564
const resolvedPath = win32.resolve(path);
569565

570-
if (resolvedPath.length >= 3) {
571-
if (resolvedPath.charCodeAt(0) === CHAR_BACKWARD_SLASH) {
572-
// Possible UNC root
573-
574-
if (resolvedPath.charCodeAt(1) === CHAR_BACKWARD_SLASH) {
575-
const code = resolvedPath.charCodeAt(2);
576-
if (code !== CHAR_QUESTION_MARK && code !== CHAR_DOT) {
577-
// Matched non-long UNC root, convert the path to a long UNC path
578-
return '\\\\?\\UNC\\' + resolvedPath.slice(2);
579-
}
580-
}
581-
} else if (isWindowsDeviceRoot(resolvedPath.charCodeAt(0))) {
582-
// Possible device root
566+
if (resolvedPath.length <= 2)
567+
return path;
583568

584-
if (resolvedPath.charCodeAt(1) === CHAR_COLON &&
585-
resolvedPath.charCodeAt(2) === CHAR_BACKWARD_SLASH) {
586-
// Matched device root, convert the path to a long UNC path
587-
return '\\\\?\\' + resolvedPath;
569+
if (resolvedPath.charCodeAt(0) === CHAR_BACKWARD_SLASH) {
570+
// Possible UNC root
571+
if (resolvedPath.charCodeAt(1) === CHAR_BACKWARD_SLASH) {
572+
const code = resolvedPath.charCodeAt(2);
573+
if (code !== CHAR_QUESTION_MARK && code !== CHAR_DOT) {
574+
// Matched non-long UNC root, convert the path to a long UNC path
575+
return `\\\\?\\UNC\\${resolvedPath.slice(2)}`;
588576
}
589577
}
578+
} else if (isWindowsDeviceRoot(resolvedPath.charCodeAt(0)) &&
579+
resolvedPath.charCodeAt(1) === CHAR_COLON &&
580+
resolvedPath.charCodeAt(2) === CHAR_BACKWARD_SLASH) {
581+
// Matched device root, convert the path to a long UNC path
582+
return `\\\\?\\${resolvedPath}`;
590583
}
591584

592585
return path;
@@ -749,29 +742,27 @@ const win32 = {
749742
else if (end === -1)
750743
end = path.length;
751744
return path.slice(start, end);
752-
} else {
753-
for (i = path.length - 1; i >= start; --i) {
754-
if (isPathSeparator(path.charCodeAt(i))) {
755-
// If we reached a path separator that was not part of a set of path
756-
// separators at the end of the string, stop now
757-
if (!matchedSlash) {
758-
start = i + 1;
759-
break;
760-
}
761-
} else if (end === -1) {
762-
// We saw the first non-path separator, mark this as the end of our
763-
// path component
764-
matchedSlash = false;
765-
end = i + 1;
745+
}
746+
for (i = path.length - 1; i >= start; --i) {
747+
if (isPathSeparator(path.charCodeAt(i))) {
748+
// If we reached a path separator that was not part of a set of path
749+
// separators at the end of the string, stop now
750+
if (!matchedSlash) {
751+
start = i + 1;
752+
break;
766753
}
754+
} else if (end === -1) {
755+
// We saw the first non-path separator, mark this as the end of our
756+
// path component
757+
matchedSlash = false;
758+
end = i + 1;
767759
}
768-
769-
if (end === -1)
770-
return '';
771-
return path.slice(start, end);
772760
}
773-
},
774761

762+
if (end === -1)
763+
return '';
764+
return path.slice(start, end);
765+
},
775766

776767
extname(path) {
777768
validateString(path, 'path');
@@ -1256,29 +1247,27 @@ const posix = {
12561247
else if (end === -1)
12571248
end = path.length;
12581249
return path.slice(start, end);
1259-
} else {
1260-
for (i = path.length - 1; i >= 0; --i) {
1261-
if (path.charCodeAt(i) === CHAR_FORWARD_SLASH) {
1262-
// If we reached a path separator that was not part of a set of path
1263-
// separators at the end of the string, stop now
1264-
if (!matchedSlash) {
1265-
start = i + 1;
1266-
break;
1267-
}
1268-
} else if (end === -1) {
1269-
// We saw the first non-path separator, mark this as the end of our
1270-
// path component
1271-
matchedSlash = false;
1272-
end = i + 1;
1250+
}
1251+
for (i = path.length - 1; i >= 0; --i) {
1252+
if (path.charCodeAt(i) === CHAR_FORWARD_SLASH) {
1253+
// If we reached a path separator that was not part of a set of path
1254+
// separators at the end of the string, stop now
1255+
if (!matchedSlash) {
1256+
start = i + 1;
1257+
break;
12731258
}
1259+
} else if (end === -1) {
1260+
// We saw the first non-path separator, mark this as the end of our
1261+
// path component
1262+
matchedSlash = false;
1263+
end = i + 1;
12741264
}
1275-
1276-
if (end === -1)
1277-
return '';
1278-
return path.slice(start, end);
12791265
}
1280-
},
12811266

1267+
if (end === -1)
1268+
return '';
1269+
return path.slice(start, end);
1270+
},
12821271

12831272
extname(path) {
12841273
validateString(path, 'path');

0 commit comments

Comments
 (0)