Skip to content

Commit a131c72

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

39 files changed

+151
-204
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
@@ -239,9 +239,8 @@ function matchKnownFields(field, lowercased) {
239239
}
240240
if (lowercased) {
241241
return '\u0000' + field;
242-
} else {
243-
return matchKnownFields(field.toLowerCase(), true);
244242
}
243+
return matchKnownFields(field.toLowerCase(), true);
245244
}
246245
// Add the given (field, value) pair to the message
247246
//

lib/_stream_readable.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -365,8 +365,7 @@ function howMuchToRead(n, state) {
365365
// Only flow one buffer at a time
366366
if (state.flowing && state.length)
367367
return state.buffer.first().length;
368-
else
369-
return state.length;
368+
return state.length;
370369
}
371370
if (n <= state.length)
372371
return n;

lib/_tls_wrap.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -1518,12 +1518,11 @@ function onConnectSecure() {
15181518
if (options.rejectUnauthorized) {
15191519
this.destroy(verifyError);
15201520
return;
1521-
} else {
1522-
debug('client emit secureConnect. rejectUnauthorized: %s, ' +
1523-
'authorizationError: %s', options.rejectUnauthorized,
1524-
this.authorizationError);
1525-
this.emit('secureConnect');
15261521
}
1522+
debug('client emit secureConnect. rejectUnauthorized: %s, ' +
1523+
'authorizationError: %s', options.rejectUnauthorized,
1524+
this.authorizationError);
1525+
this.emit('secureConnect');
15271526
} else {
15281527
this.authorized = true;
15291528
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
@@ -1515,9 +1515,8 @@ function encodeRealpathResult(result, options) {
15151515
const asBuffer = Buffer.from(result);
15161516
if (options.encoding === 'buffer') {
15171517
return asBuffer;
1518-
} else {
1519-
return asBuffer.toString(options.encoding);
15201518
}
1519+
return asBuffer.toString(options.encoding);
15211520
}
15221521

15231522
// 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
@@ -70,8 +70,7 @@ function getDSASignatureEncoding(options) {
7070
return kSigEncDER;
7171
else if (dsaEncoding === 'ieee-p1363')
7272
return kSigEncP1363;
73-
else
74-
throw new ERR_INVALID_OPT_VALUE('dsaEncoding', dsaEncoding);
73+
throw new ERR_INVALID_OPT_VALUE('dsaEncoding', dsaEncoding);
7574
}
7675

7776
return kSigEncDER;
@@ -82,9 +81,8 @@ function getIntOption(name, options) {
8281
if (value !== undefined) {
8382
if (value === value >> 0) {
8483
return value;
85-
} else {
86-
throw new ERR_INVALID_OPT_VALUE(name, value);
8784
}
85+
throw new ERR_INVALID_OPT_VALUE(name, value);
8886
}
8987
return undefined;
9088
}

lib/internal/errors.js

+11-16
Original file line numberDiff line numberDiff line change
@@ -1106,10 +1106,9 @@ E('ERR_INVALID_MODULE_SPECIFIER', (pkgPath, subpath, base = undefined) => {
11061106
assert(subpath !== '.');
11071107
return `Package subpath '${subpath}' is not a valid module request for ` +
11081108
`the "exports" resolution of ${pkgPath}${sep}package.json`;
1109-
} else {
1110-
return `Package subpath '${subpath}' is not a valid module request for ` +
1111-
`the "exports" resolution of ${pkgPath} imported from ${base}`;
11121109
}
1110+
return `Package subpath '${subpath}' is not a valid module request for ` +
1111+
`the "exports" resolution of ${pkgPath} imported from ${base}`;
11131112
}, TypeError);
11141113
E('ERR_INVALID_OPT_VALUE', (name, value) =>
11151114
`The value "${String(value)}" is invalid for option "${name}"`,
@@ -1120,8 +1119,7 @@ E('ERR_INVALID_OPT_VALUE_ENCODING',
11201119
E('ERR_INVALID_PACKAGE_CONFIG', (path, message, hasMessage = true) => {
11211120
if (hasMessage)
11221121
return `Invalid package config ${path}${sep}package.json, ${message}`;
1123-
else
1124-
return `Invalid JSON in ${path} imported from ${message}`;
1122+
return `Invalid JSON in ${path} imported from ${message}`;
11251123
}, Error);
11261124
E('ERR_INVALID_PACKAGE_TARGET',
11271125
(pkgPath, key, subpath, target, base = undefined) => {
@@ -1132,11 +1130,10 @@ E('ERR_INVALID_PACKAGE_TARGET',
11321130
return `Invalid "exports" target ${JSONStringify(target)} defined ` +
11331131
`for '${subpath}' in the package config ${pkgPath} imported from ` +
11341132
`${base}.${relError ? '; targets must start with "./"' : ''}`;
1135-
} else {
1136-
return `Invalid "exports" main target ${target} defined in the ` +
1137-
`package config ${pkgPath} imported from ${base}${relError ?
1138-
'; targets must start with "./"' : ''}`;
11391133
}
1134+
return `Invalid "exports" main target ${target} defined in the ` +
1135+
`package config ${pkgPath} imported from ${base}${relError ?
1136+
'; targets must start with "./"' : ''}`;
11401137
} else if (key === '.') {
11411138
return `Invalid "exports" main target ${JSONStringify(target)} defined ` +
11421139
`in the package config ${pkgPath}${sep}package.json${relError ?
@@ -1146,11 +1143,10 @@ E('ERR_INVALID_PACKAGE_TARGET',
11461143
StringPrototypeSlice(key, 0, -subpath.length || key.length)}' in the ` +
11471144
`package config ${pkgPath}${sep}package.json; ` +
11481145
'targets must start with "./"';
1149-
} else {
1150-
return `Invalid "exports" target ${JSONStringify(target)} defined for '${
1151-
StringPrototypeSlice(key, 0, -subpath.length || key.length)}' in the ` +
1152-
`package config ${pkgPath}${sep}package.json`;
11531146
}
1147+
return `Invalid "exports" target ${JSONStringify(target)} defined for '${
1148+
StringPrototypeSlice(key, 0, -subpath.length || key.length)}' in the ` +
1149+
`package config ${pkgPath}${sep}package.json`;
11541150
}, Error);
11551151
E('ERR_INVALID_PERFORMANCE_MARK',
11561152
'The "%s" performance mark has not been set', Error);
@@ -1305,10 +1301,9 @@ E('ERR_PACKAGE_PATH_NOT_EXPORTED', (pkgPath, subpath, base = undefined) => {
13051301
} else if (base === undefined) {
13061302
return `Package subpath '${subpath}' is not defined by "exports" in ${
13071303
pkgPath}${sep}package.json`;
1308-
} else {
1309-
return `Package subpath '${subpath}' is not defined by "exports" in ${
1310-
pkgPath} imported from ${base}`;
13111304
}
1305+
return `Package subpath '${subpath}' is not defined by "exports" in ${
1306+
pkgPath} imported from ${base}`;
13121307
}, Error);
13131308
E('ERR_REQUIRE_ESM',
13141309
(filename, parentPath = null, packageJsonPath = null) => {

lib/internal/fs/streams.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -452,9 +452,8 @@ WriteStream.prototype.close = function(cb) {
452452
if (this.closed) {
453453
process.nextTick(cb);
454454
return;
455-
} else {
456-
this.on('close', cb);
457455
}
456+
this.on('close', cb);
458457
}
459458

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

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
@@ -430,10 +430,9 @@ function packageMainResolve(packageJSONUrl, packageConfig, base, conditions) {
430430
if (packageConfig.main !== undefined) {
431431
return finalizeResolution(
432432
new URL(packageConfig.main, packageJSONUrl), base);
433-
} else {
434-
return finalizeResolution(
435-
new URL('index', packageJSONUrl), base);
436433
}
434+
return finalizeResolution(
435+
new URL('index', packageJSONUrl), base);
437436
}
438437
return legacyMainResolve(packageJSONUrl, packageConfig);
439438
}
@@ -553,10 +552,9 @@ function packageResolve(specifier, base, conditions) {
553552
} else if (packageSubpath === '') {
554553
return packageMainResolve(packageJSONUrl, packageConfig, base,
555554
conditions);
556-
} else {
557-
return packageExportsResolve(
558-
packageJSONUrl, packageSubpath, packageConfig, base, conditions);
559555
}
556+
return packageExportsResolve(
557+
packageJSONUrl, packageSubpath, packageConfig, base, conditions);
560558
}
561559
}
562560

@@ -585,10 +583,9 @@ function packageResolve(specifier, base, conditions) {
585583
} else if (packageConfig.exports !== undefined) {
586584
return packageExportsResolve(
587585
packageJSONUrl, packageSubpath, packageConfig, base, conditions);
588-
} else {
589-
return finalizeResolution(
590-
new URL(packageSubpath, packageJSONUrl), base);
591586
}
587+
return finalizeResolution(
588+
new URL(packageSubpath, packageJSONUrl), base);
592589
// Cross-platform root check.
593590
} while (packageJSONPath.length !== lastPath.length);
594591

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
/**

lib/internal/source_map/source_map_cache.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,8 @@ function sourceMapCacheToObject() {
192192

193193
if (ObjectKeys(obj).length === 0) {
194194
return undefined;
195-
} else {
196-
return obj;
197195
}
196+
return obj;
198197
}
199198

200199
// Since WeakMap can't be iterated over, we use Module._cache's
@@ -243,9 +242,8 @@ function findSourceMap(uri, error) {
243242
}
244243
if (sourceMap && sourceMap.data) {
245244
return new SourceMap(sourceMap.data);
246-
} else {
247-
return undefined;
248245
}
246+
return undefined;
249247
}
250248

251249
module.exports = {

0 commit comments

Comments
 (0)