Skip to content

Commit cccc44b

Browse files
BridgeARaddaleax
authored andcommitted
path: refactor more path code for simplicity
1) Consolidate format to a single function. 2) Move some code that can only be reached in some code branches that was formerly executed in all cases. 3) Explicitly check for the string length of zero instead of converting the string to a boolean. 4) Consolidate nested if statements if possible e.g., if (foo) { if (bar) { /* do stuff */ } } to reduce indentation depth. 5) Simplify checks by removing extra length checks when comparing two strings. 6) Use object shorthand notation where possible. PR-URL: #25278 Reviewed-By: Michaël Zasso <[email protected]>
1 parent 6c44e68 commit cccc44b

File tree

1 file changed

+45
-58
lines changed

1 file changed

+45
-58
lines changed

lib/path.js

+45-58
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ function normalizeString(path, allowAboveRoot, separator, isPathSeparator) {
110110
}
111111

112112
function _format(sep, pathObject) {
113+
if (pathObject === null || typeof pathObject !== 'object') {
114+
throw new ERR_INVALID_ARG_TYPE('pathObject', 'Object', pathObject);
115+
}
113116
const dir = pathObject.dir || pathObject.root;
114117
const base = pathObject.base ||
115118
`${pathObject.name || ''}${pathObject.ext || ''}`;
@@ -121,16 +124,22 @@ function _format(sep, pathObject) {
121124

122125
const win32 = {
123126
// path.resolve([from ...], to)
124-
resolve: function resolve() {
125-
var resolvedDevice = '';
126-
var resolvedTail = '';
127-
var resolvedAbsolute = false;
127+
resolve(...args) {
128+
let resolvedDevice = '';
129+
let resolvedTail = '';
130+
let resolvedAbsolute = false;
128131

129-
for (var i = arguments.length - 1; i >= -1; i--) {
130-
var path;
132+
for (var i = args.length - 1; i >= -1; i--) {
133+
let path;
131134
if (i >= 0) {
132-
path = arguments[i];
133-
} else if (!resolvedDevice) {
135+
path = args[i];
136+
validateString(path, 'path');
137+
138+
// Skip empty entries
139+
if (path.length === 0) {
140+
continue;
141+
}
142+
} else if (resolvedDevice.length === 0) {
134143
path = process.cwd();
135144
} else {
136145
// Windows has the concept of drive-specific current working
@@ -149,17 +158,10 @@ const win32 = {
149158
}
150159
}
151160

152-
validateString(path, 'path');
153-
154-
// Skip empty entries
155-
if (path.length === 0) {
156-
continue;
157-
}
158-
159-
var len = path.length;
160-
var rootEnd = 0;
161-
var device = '';
162-
var isAbsolute = false;
161+
const len = path.length;
162+
let rootEnd = 0;
163+
let device = '';
164+
let isAbsolute = false;
163165
const code = path.charCodeAt(0);
164166

165167
// Try to match a root
@@ -409,16 +411,14 @@ const win32 = {
409411
if (isPathSeparator(firstPart.charCodeAt(0))) {
410412
++slashCount;
411413
const firstLen = firstPart.length;
412-
if (firstLen > 1) {
413-
if (isPathSeparator(firstPart.charCodeAt(1))) {
414-
++slashCount;
415-
if (firstLen > 2) {
416-
if (isPathSeparator(firstPart.charCodeAt(2)))
417-
++slashCount;
418-
else {
419-
// We matched a UNC path in the first part
420-
needsReplace = false;
421-
}
414+
if (firstLen > 1 && isPathSeparator(firstPart.charCodeAt(1))) {
415+
++slashCount;
416+
if (firstLen > 2) {
417+
if (isPathSeparator(firstPart.charCodeAt(2)))
418+
++slashCount;
419+
else {
420+
// We matched a UNC path in the first part
421+
needsReplace = false;
422422
}
423423
}
424424
}
@@ -699,16 +699,14 @@ const win32 = {
699699
// Check for a drive letter prefix so as not to mistake the following
700700
// path separator as an extra separator at the end of the path that can be
701701
// disregarded
702-
if (path.length >= 2) {
703-
const drive = path.charCodeAt(0);
704-
if (isWindowsDeviceRoot(drive)) {
705-
if (path.charCodeAt(1) === CHAR_COLON)
706-
start = 2;
707-
}
702+
if (path.length >= 2 &&
703+
isWindowsDeviceRoot(path.charCodeAt(0)) &&
704+
path.charCodeAt(1) === CHAR_COLON) {
705+
start = 2;
708706
}
709707

710708
if (ext !== undefined && ext.length > 0 && ext.length <= path.length) {
711-
if (ext.length === path.length && ext === path)
709+
if (ext === path)
712710
return '';
713711
var extIdx = ext.length - 1;
714712
var firstNonSlashEnd = -1;
@@ -839,16 +837,9 @@ const win32 = {
839837
return path.slice(startDot, end);
840838
},
841839

840+
format: _format.bind(null, '\\'),
842841

843-
format: function format(pathObject) {
844-
if (pathObject === null || typeof pathObject !== 'object') {
845-
throw new ERR_INVALID_ARG_TYPE('pathObject', 'Object', pathObject);
846-
}
847-
return _format('\\', pathObject);
848-
},
849-
850-
851-
parse: function parse(path) {
842+
parse(path) {
852843
validateString(path, 'path');
853844

854845
const ret = { root: '', dir: '', base: '', ext: '', name: '' };
@@ -1056,9 +1047,12 @@ const posix = {
10561047
// Normalize the path
10571048
path = normalizeString(path, !isAbsolute, '/', isPosixPathSeparator);
10581049

1059-
if (path.length === 0 && !isAbsolute)
1060-
path = '.';
1061-
if (path.length > 0 && trailingSeparator)
1050+
if (path.length === 0) {
1051+
if (isAbsolute)
1052+
return '/';
1053+
return trailingSeparator ? './' : '.';
1054+
}
1055+
if (trailingSeparator)
10621056
path += '/';
10631057

10641058
return isAbsolute ? `/${path}` : path;
@@ -1219,7 +1213,7 @@ const posix = {
12191213
var i;
12201214

12211215
if (ext !== undefined && ext.length > 0 && ext.length <= path.length) {
1222-
if (ext.length === path.length && ext === path)
1216+
if (ext === path)
12231217
return '';
12241218
var extIdx = ext.length - 1;
12251219
var firstNonSlashEnd = -1;
@@ -1338,16 +1332,9 @@ const posix = {
13381332
return path.slice(startDot, end);
13391333
},
13401334

1335+
format: _format.bind(null, '/'),
13411336

1342-
format: function format(pathObject) {
1343-
if (pathObject === null || typeof pathObject !== 'object') {
1344-
throw new ERR_INVALID_ARG_TYPE('pathObject', 'Object', pathObject);
1345-
}
1346-
return _format('/', pathObject);
1347-
},
1348-
1349-
1350-
parse: function parse(path) {
1337+
parse(path) {
13511338
validateString(path, 'path');
13521339

13531340
const ret = { root: '', dir: '', base: '', ext: '', name: '' };

0 commit comments

Comments
 (0)