Skip to content

Commit 9119344

Browse files
lpincacodebytere
authored andcommitted
tools: enable no-else-return lint rule
Refs: #32644 Refs: #32662 PR-URL: #32667 Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]>
1 parent e53de96 commit 9119344

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+158
-214
lines changed

.eslintrc.js

+1
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ module.exports = {
129129
'no-dupe-else-if': 'error',
130130
'no-duplicate-case': 'error',
131131
'no-duplicate-imports': 'error',
132+
'no-else-return': ['error', { allowElseIf: true }],
132133
'no-empty-character-class': 'error',
133134
'no-ex-assign': 'error',
134135
'no-extra-boolean-cast': 'error',

benchmark/es/spread-bench.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@ function makeTest(count, rest) {
1616
return function test(...args) {
1717
assert.strictEqual(count, args.length);
1818
};
19-
} else {
20-
return function test() {
21-
assert.strictEqual(count, arguments.length);
22-
};
2319
}
20+
return function test() {
21+
assert.strictEqual(count, arguments.length);
22+
};
2423
}
2524

2625
function main({ n, context, count, rest, method }) {

benchmark/scatter.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@ let printHeader = true;
3030
function csvEncodeValue(value) {
3131
if (typeof value === 'number') {
3232
return value.toString();
33-
} else {
34-
return `"${value.replace(/"/g, '""')}"`;
3533
}
34+
return `"${value.replace(/"/g, '""')}"`;
3635
}
3736

3837
(function recursive(i) {

lib/_http_incoming.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -248,9 +248,8 @@ function matchKnownFields(field, lowercased) {
248248
}
249249
if (lowercased) {
250250
return '\u0000' + field;
251-
} else {
252-
return matchKnownFields(field.toLowerCase(), true);
253251
}
252+
return matchKnownFields(field.toLowerCase(), true);
254253
}
255254
// Add the given (field, value) pair to the message
256255
//

lib/_stream_readable.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,7 @@ function howMuchToRead(n, state) {
372372
// Only flow one buffer at a time.
373373
if (state.flowing && state.length)
374374
return state.buffer.first().length;
375-
else
376-
return state.length;
375+
return state.length;
377376
}
378377
if (n <= state.length)
379378
return n;

lib/_stream_writable.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -298,10 +298,9 @@ Writable.prototype.write = function(chunk, encoding, cb) {
298298
process.nextTick(cb, err);
299299
errorOrDestroy(this, err, true);
300300
return false;
301-
} else {
302-
state.pendingcb++;
303-
return writeOrBuffer(this, state, chunk, encoding, cb);
304301
}
302+
state.pendingcb++;
303+
return writeOrBuffer(this, state, chunk, encoding, cb);
305304
};
306305

307306
Writable.prototype.cork = function() {

lib/_tls_wrap.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -1503,13 +1503,12 @@ function onConnectSecure() {
15031503
if (options.rejectUnauthorized) {
15041504
this.destroy(verifyError);
15051505
return;
1506-
} else {
1507-
debug('client emit secureConnect. rejectUnauthorized: %s, ' +
1508-
'authorizationError: %s', options.rejectUnauthorized,
1509-
this.authorizationError);
1510-
this.secureConnecting = false;
1511-
this.emit('secureConnect');
15121506
}
1507+
debug('client emit secureConnect. rejectUnauthorized: %s, ' +
1508+
'authorizationError: %s', options.rejectUnauthorized,
1509+
this.authorizationError);
1510+
this.secureConnecting = false;
1511+
this.emit('secureConnect');
15131512
} else {
15141513
this.authorized = true;
15151514
debug('client emit secureConnect. authorized:', this.authorized);

lib/dns.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -261,9 +261,8 @@ function resolve(hostname, rrtype, callback) {
261261

262262
if (typeof resolver === 'function') {
263263
return resolver.call(this, hostname, callback);
264-
} else {
265-
throw new ERR_INVALID_OPT_VALUE('rrtype', rrtype);
266264
}
265+
throw new ERR_INVALID_OPT_VALUE('rrtype', rrtype);
267266
}
268267

269268
function defaultResolverSetServers(servers) {

lib/events.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -577,9 +577,8 @@ EventEmitter.prototype.rawListeners = function rawListeners(type) {
577577
EventEmitter.listenerCount = function(emitter, type) {
578578
if (typeof emitter.listenerCount === 'function') {
579579
return emitter.listenerCount(type);
580-
} else {
581-
return listenerCount.call(emitter, type);
582580
}
581+
return listenerCount.call(emitter, type);
583582
};
584583

585584
EventEmitter.prototype.listenerCount = listenerCount;

lib/fs.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -1545,9 +1545,8 @@ function encodeRealpathResult(result, options) {
15451545
const asBuffer = Buffer.from(result);
15461546
if (options.encoding === 'buffer') {
15471547
return asBuffer;
1548-
} else {
1549-
return asBuffer.toString(options.encoding);
15501548
}
1549+
return asBuffer.toString(options.encoding);
15511550
}
15521551

15531552
// Finds the next portion of a (partial) path, up to the next path delimiter

lib/internal/crypto/keys.js

+11-13
Original file line numberDiff line numberDiff line change
@@ -277,14 +277,13 @@ function prepareAsymmetricKey(key, ctx) {
277277
const isPublic =
278278
(ctx === kConsumePrivate || ctx === kCreatePrivate) ? false : undefined;
279279
return { data, ...parseKeyEncoding(key, undefined, isPublic) };
280-
} else {
281-
throw new ERR_INVALID_ARG_TYPE(
282-
'key',
283-
['string', 'Buffer', 'TypedArray', 'DataView',
284-
...(ctx !== kCreatePrivate ? ['KeyObject'] : [])],
285-
key
286-
);
287280
}
281+
throw new ERR_INVALID_ARG_TYPE(
282+
'key',
283+
['string', 'Buffer', 'TypedArray', 'DataView',
284+
...(ctx !== kCreatePrivate ? ['KeyObject'] : [])],
285+
key
286+
);
288287
}
289288

290289
function preparePrivateKey(key) {
@@ -301,13 +300,12 @@ function prepareSecretKey(key, bufferOnly = false) {
301300
if (key.type !== 'secret')
302301
throw new ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE(key.type, 'secret');
303302
return key[kHandle];
304-
} else {
305-
throw new ERR_INVALID_ARG_TYPE(
306-
'key',
307-
['Buffer', 'TypedArray', 'DataView',
308-
...(bufferOnly ? [] : ['string', 'KeyObject'])],
309-
key);
310303
}
304+
throw new ERR_INVALID_ARG_TYPE(
305+
'key',
306+
['Buffer', 'TypedArray', 'DataView',
307+
...(bufferOnly ? [] : ['string', 'KeyObject'])],
308+
key);
311309
}
312310
return key;
313311
}

lib/internal/crypto/sig.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ function getDSASignatureEncoding(options) {
7777
return kSigEncDER;
7878
else if (dsaEncoding === 'ieee-p1363')
7979
return kSigEncP1363;
80-
else
81-
throw new ERR_INVALID_OPT_VALUE('dsaEncoding', dsaEncoding);
80+
throw new ERR_INVALID_OPT_VALUE('dsaEncoding', dsaEncoding);
8281
}
8382

8483
return kSigEncDER;
@@ -89,9 +88,8 @@ function getIntOption(name, options) {
8988
if (value !== undefined) {
9089
if (value === value >> 0) {
9190
return value;
92-
} else {
93-
throw new ERR_INVALID_OPT_VALUE(name, value);
9491
}
92+
throw new ERR_INVALID_OPT_VALUE(name, value);
9593
}
9694
return undefined;
9795
}

lib/internal/errors.js

+11-16
Original file line numberDiff line numberDiff line change
@@ -1090,10 +1090,9 @@ E('ERR_INVALID_MODULE_SPECIFIER', (pkgPath, subpath, base = undefined) => {
10901090
assert(subpath !== '.');
10911091
return `Package subpath '${subpath}' is not a valid module request for ` +
10921092
`the "exports" resolution of ${pkgPath}${sep}package.json`;
1093-
} else {
1094-
return `Package subpath '${subpath}' is not a valid module request for ` +
1095-
`the "exports" resolution of ${pkgPath} imported from ${base}`;
10961093
}
1094+
return `Package subpath '${subpath}' is not a valid module request for ` +
1095+
`the "exports" resolution of ${pkgPath} imported from ${base}`;
10971096
}, TypeError);
10981097
E('ERR_INVALID_OPT_VALUE', (name, value) =>
10991098
`The value "${String(value)}" is invalid for option "${name}"`,
@@ -1104,8 +1103,7 @@ E('ERR_INVALID_OPT_VALUE_ENCODING',
11041103
E('ERR_INVALID_PACKAGE_CONFIG', (path, message, hasMessage = true) => {
11051104
if (hasMessage)
11061105
return `Invalid package config ${path}${sep}package.json, ${message}`;
1107-
else
1108-
return `Invalid JSON in ${path} imported from ${message}`;
1106+
return `Invalid JSON in ${path} imported from ${message}`;
11091107
}, Error);
11101108
E('ERR_INVALID_PACKAGE_TARGET',
11111109
(pkgPath, key, subpath, target, base = undefined) => {
@@ -1116,11 +1114,10 @@ E('ERR_INVALID_PACKAGE_TARGET',
11161114
return `Invalid "exports" target ${JSONStringify(target)} defined ` +
11171115
`for '${subpath}' in the package config ${pkgPath} imported from ` +
11181116
`${base}.${relError ? '; targets must start with "./"' : ''}`;
1119-
} else {
1120-
return `Invalid "exports" main target ${target} defined in the ` +
1121-
`package config ${pkgPath} imported from ${base}${relError ?
1122-
'; targets must start with "./"' : ''}`;
11231117
}
1118+
return `Invalid "exports" main target ${target} defined in the ` +
1119+
`package config ${pkgPath} imported from ${base}${relError ?
1120+
'; targets must start with "./"' : ''}`;
11241121
} else if (key === '.') {
11251122
return `Invalid "exports" main target ${JSONStringify(target)} defined ` +
11261123
`in the package config ${pkgPath}${sep}package.json${relError ?
@@ -1130,11 +1127,10 @@ E('ERR_INVALID_PACKAGE_TARGET',
11301127
StringPrototypeSlice(key, 0, -subpath.length || key.length)}' in the ` +
11311128
`package config ${pkgPath}${sep}package.json; ` +
11321129
'targets must start with "./"';
1133-
} else {
1134-
return `Invalid "exports" target ${JSONStringify(target)} defined for '${
1135-
StringPrototypeSlice(key, 0, -subpath.length || key.length)}' in the ` +
1136-
`package config ${pkgPath}${sep}package.json`;
11371130
}
1131+
return `Invalid "exports" target ${JSONStringify(target)} defined for '${
1132+
StringPrototypeSlice(key, 0, -subpath.length || key.length)}' in the ` +
1133+
`package config ${pkgPath}${sep}package.json`;
11381134
}, Error);
11391135
E('ERR_INVALID_PERFORMANCE_MARK',
11401136
'The "%s" performance mark has not been set', Error);
@@ -1289,10 +1285,9 @@ E('ERR_PACKAGE_PATH_NOT_EXPORTED', (pkgPath, subpath, base = undefined) => {
12891285
} else if (base === undefined) {
12901286
return `Package subpath '${subpath}' is not defined by "exports" in ${
12911287
pkgPath}${sep}package.json`;
1292-
} else {
1293-
return `Package subpath '${subpath}' is not defined by "exports" in ${
1294-
pkgPath} imported from ${base}`;
12951288
}
1289+
return `Package subpath '${subpath}' is not defined by "exports" in ${
1290+
pkgPath} imported from ${base}`;
12961291
}, Error);
12971292
E('ERR_REQUIRE_ESM',
12981293
(filename, parentPath = null, packageJsonPath = null) => {

lib/internal/fs/streams.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -478,9 +478,8 @@ WriteStream.prototype.close = function(cb) {
478478
if (this.closed) {
479479
process.nextTick(cb);
480480
return;
481-
} else {
482-
this.on('close', cb);
483481
}
482+
this.on('close', cb);
484483
}
485484

486485
// If we are not autoClosing, we should call

lib/internal/fs/utils.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -301,10 +301,9 @@ function preprocessSymlinkDestination(path, type, linkPath) {
301301
// A relative target is relative to the link's parent directory.
302302
path = pathModule.resolve(linkPath, '..', path);
303303
return pathModule.toNamespacedPath(path);
304-
} else {
305-
// Windows symlinks don't tolerate forward slashes.
306-
return ('' + path).replace(/\//g, '\\');
307304
}
305+
// Windows symlinks don't tolerate forward slashes.
306+
return ('' + path).replace(/\//g, '\\');
308307
}
309308

310309
// Constructor for file stats.

lib/internal/modules/esm/get_source.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ async function defaultGetSource(url, { format } = {}, defaultGetSource) {
2828
return {
2929
source: Buffer.from(body, base64 ? 'base64' : 'utf8')
3030
};
31-
} else {
32-
throw new ERR_INVALID_URL_SCHEME(['file', 'data']);
3331
}
32+
throw new ERR_INVALID_URL_SCHEME(['file', 'data']);
3433
}
3534
exports.defaultGetSource = defaultGetSource;

lib/internal/modules/esm/resolve.js

+6-9
Original file line numberDiff line numberDiff line change
@@ -455,10 +455,9 @@ function packageMainResolve(packageJSONUrl, packageConfig, base, conditions) {
455455
if (packageConfig.main !== undefined) {
456456
return finalizeResolution(
457457
new URL(packageConfig.main, packageJSONUrl), base);
458-
} else {
459-
return finalizeResolution(
460-
new URL('index', packageJSONUrl), base);
461458
}
459+
return finalizeResolution(
460+
new URL('index', packageJSONUrl), base);
462461
}
463462
return legacyMainResolve(packageJSONUrl, packageConfig);
464463
}
@@ -578,10 +577,9 @@ function packageResolve(specifier, base, conditions) {
578577
} else if (packageSubpath === '') {
579578
return packageMainResolve(packageJSONUrl, packageConfig, base,
580579
conditions);
581-
} else {
582-
return packageExportsResolve(
583-
packageJSONUrl, packageSubpath, packageConfig, base, conditions);
584580
}
581+
return packageExportsResolve(
582+
packageJSONUrl, packageSubpath, packageConfig, base, conditions);
585583
}
586584
}
587585

@@ -611,10 +609,9 @@ function packageResolve(specifier, base, conditions) {
611609
} else if (packageConfig.exports !== undefined) {
612610
return packageExportsResolve(
613611
packageJSONUrl, packageSubpath, packageConfig, base, conditions);
614-
} else {
615-
return finalizeResolution(
616-
new URL(packageSubpath, packageJSONUrl), base);
617612
}
613+
return finalizeResolution(
614+
new URL(packageSubpath, packageJSONUrl), base);
618615
// Cross-platform root check.
619616
} while (packageJSONPath.length !== lastPath.length);
620617

lib/internal/policy/manifest.js

+20-21
Original file line numberDiff line numberDiff line change
@@ -145,32 +145,31 @@ class Manifest {
145145
const dependencyRedirectList = (toSpecifier) => {
146146
if (toSpecifier in dependencyMap !== true) {
147147
return null;
148-
} else {
149-
const to = dependencyMap[toSpecifier];
150-
if (to === true) {
151-
return true;
152-
}
153-
if (parsedURLs.has(to)) {
154-
return parsedURLs.get(to);
155-
} else if (canBeRequiredByUsers(to)) {
156-
const href = `node:${to}`;
157-
const resolvedURL = new URL(href);
158-
parsedURLs.set(to, resolvedURL);
159-
parsedURLs.set(href, resolvedURL);
160-
return resolvedURL;
161-
} else if (RegExpPrototypeTest(kRelativeURLStringPattern, to)) {
162-
const resolvedURL = new URL(to, manifestURL);
163-
const href = resourceURL.href;
164-
parsedURLs.set(to, resolvedURL);
165-
parsedURLs.set(href, resolvedURL);
166-
return resolvedURL;
167-
}
168-
const resolvedURL = new URL(to);
148+
}
149+
const to = dependencyMap[toSpecifier];
150+
if (to === true) {
151+
return true;
152+
}
153+
if (parsedURLs.has(to)) {
154+
return parsedURLs.get(to);
155+
} else if (canBeRequiredByUsers(to)) {
156+
const href = `node:${to}`;
157+
const resolvedURL = new URL(href);
158+
parsedURLs.set(to, resolvedURL);
159+
parsedURLs.set(href, resolvedURL);
160+
return resolvedURL;
161+
} else if (RegExpPrototypeTest(kRelativeURLStringPattern, to)) {
162+
const resolvedURL = new URL(to, manifestURL);
169163
const href = resourceURL.href;
170164
parsedURLs.set(to, resolvedURL);
171165
parsedURLs.set(href, resolvedURL);
172166
return resolvedURL;
173167
}
168+
const resolvedURL = new URL(to);
169+
const href = resourceURL.href;
170+
parsedURLs.set(to, resolvedURL);
171+
parsedURLs.set(href, resolvedURL);
172+
return resolvedURL;
174173
};
175174
dependencies.set(resourceHREF, dependencyRedirectList);
176175
} else if (dependencyMap === true) {

lib/internal/source_map/source_map.js

+7-8
Original file line numberDiff line numberDiff line change
@@ -196,15 +196,14 @@ class SourceMap {
196196
return {};
197197
} else if (!entry) {
198198
return {};
199-
} else {
200-
return {
201-
generatedLine: entry[0],
202-
generatedColumn: entry[1],
203-
originalSource: entry[2],
204-
originalLine: entry[3],
205-
originalColumn: entry[4]
206-
};
207199
}
200+
return {
201+
generatedLine: entry[0],
202+
generatedColumn: entry[1],
203+
originalSource: entry[2],
204+
originalLine: entry[3],
205+
originalColumn: entry[4]
206+
};
208207
}
209208

210209
/**

0 commit comments

Comments
 (0)