Skip to content

Commit 3c0ebf5

Browse files
committed
tools: enable additional eslint rules
Enable additional rules that node either already adheres to or it makes sense to do so going forward: for-direction, accessor-pairs, no-lonely-if and symbol-description. Fix all instances of no-lonely-if in lib & test and disable accessor-pairs in test-util-inspect. PR-URL: #16243 Refs: https://eslint.org/docs/rules/for-direction Refs: https://eslint.org/docs/rules/accessor-pairs Refs: https://eslint.org/docs/rules/no-lonely-if Refs: https://eslint.org/docs/rules/symbol-description Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Vse Mozhet Byt <[email protected]> Reviewed-By: Refael Ackermann <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent bf1bace commit 3c0ebf5

22 files changed

+118
-138
lines changed

.eslintrc.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ overrides:
1818
rules:
1919
# Possible Errors
2020
# http://eslint.org/docs/rules/#possible-errors
21+
for-direction: error
2122
no-control-regex: error
2223
no-debugger: error
2324
no-dupe-args: error
@@ -41,6 +42,7 @@ rules:
4142

4243
# Best Practices
4344
# http://eslint.org/docs/rules/#best-practices
45+
accessor-pairs: error
4446
dot-location: [error, property]
4547
eqeqeq: [error, smart]
4648
no-fallthrough: error
@@ -122,6 +124,7 @@ rules:
122124
ignoreUrls: true,
123125
tabWidth: 2}]
124126
new-parens: error
127+
no-lonely-if: error
125128
no-mixed-spaces-and-tabs: error
126129
no-multiple-empty-lines: [error, {max: 2, maxEOF: 0, maxBOF: 0}]
127130
no-restricted-syntax: [error, {
@@ -172,6 +175,7 @@ rules:
172175
no-this-before-super: error
173176
prefer-const: [error, {ignoreReadBeforeAssign: true}]
174177
rest-spread-spacing: error
178+
symbol-description: error
175179
template-curly-spacing: error
176180

177181
# Custom rules in tools/eslint-rules

doc/.eslintrc.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ rules:
66
no-undef: off
77
no-unused-vars: off
88
strict: off
9+
symbol-description: off
910

1011
# add new ECMAScript features gradually
1112
no-var: error

lib/_http_incoming.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -301,10 +301,9 @@ function _addHeaderLine(field, value, dest) {
301301
} else {
302302
dest['set-cookie'] = [value];
303303
}
304-
} else {
304+
} else if (dest[field] === undefined) {
305305
// Drop duplicates
306-
if (dest[field] === undefined)
307-
dest[field] = value;
306+
dest[field] = value;
308307
}
309308
}
310309

lib/_http_outgoing.js

+11-13
Original file line numberDiff line numberDiff line change
@@ -404,20 +404,18 @@ function _storeHeader(firstLine, headers) {
404404
this.chunkedEncoding = false;
405405
} else if (!this.useChunkedEncodingByDefault) {
406406
this._last = true;
407+
} else if (!state.trailer &&
408+
!this._removedContLen &&
409+
typeof this._contentLength === 'number') {
410+
state.header += 'Content-Length: ' + this._contentLength + CRLF;
411+
} else if (!this._removedTE) {
412+
state.header += 'Transfer-Encoding: chunked\r\n';
413+
this.chunkedEncoding = true;
407414
} else {
408-
if (!state.trailer &&
409-
!this._removedContLen &&
410-
typeof this._contentLength === 'number') {
411-
state.header += 'Content-Length: ' + this._contentLength + CRLF;
412-
} else if (!this._removedTE) {
413-
state.header += 'Transfer-Encoding: chunked\r\n';
414-
this.chunkedEncoding = true;
415-
} else {
416-
// We should only be able to get here if both Content-Length and
417-
// Transfer-Encoding are removed by the user.
418-
// See: test/parallel/test-http-remove-header-stays-removed.js
419-
debug('Both Content-Length and Transfer-Encoding are removed');
420-
}
415+
// We should only be able to get here if both Content-Length and
416+
// Transfer-Encoding are removed by the user.
417+
// See: test/parallel/test-http-remove-header-stays-removed.js
418+
debug('Both Content-Length and Transfer-Encoding are removed');
421419
}
422420
}
423421

lib/_http_server.js

+6-8
Original file line numberDiff line numberDiff line change
@@ -433,8 +433,8 @@ function socketOnEnd(server, socket, parser, state) {
433433
state.outgoing[state.outgoing.length - 1]._last = true;
434434
} else if (socket._httpMessage) {
435435
socket._httpMessage._last = true;
436-
} else {
437-
if (socket.writable) socket.end();
436+
} else if (socket.writable) {
437+
socket.end();
438438
}
439439
}
440440

@@ -602,13 +602,11 @@ function parserOnIncoming(server, socket, state, req, keepAlive) {
602602
res.writeContinue();
603603
server.emit('request', req, res);
604604
}
605+
} else if (server.listenerCount('checkExpectation') > 0) {
606+
server.emit('checkExpectation', req, res);
605607
} else {
606-
if (server.listenerCount('checkExpectation') > 0) {
607-
server.emit('checkExpectation', req, res);
608-
} else {
609-
res.writeHead(417);
610-
res.end();
611-
}
608+
res.writeHead(417);
609+
res.end();
612610
}
613611
} else {
614612
server.emit('request', req, res);

lib/_stream_readable.js

+12-13
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,19 @@ const kProxyEvents = ['error', 'close', 'destroy', 'pause', 'resume'];
4040
function prependListener(emitter, event, fn) {
4141
// Sadly this is not cacheable as some libraries bundle their own
4242
// event emitter implementation with them.
43-
if (typeof emitter.prependListener === 'function') {
43+
if (typeof emitter.prependListener === 'function')
4444
return emitter.prependListener(event, fn);
45-
} else {
46-
// This is a hack to make sure that our error handler is attached before any
47-
// userland ones. NEVER DO THIS. This is here only because this code needs
48-
// to continue to work with older versions of Node.js that do not include
49-
// the prependListener() method. The goal is to eventually remove this hack.
50-
if (!emitter._events || !emitter._events[event])
51-
emitter.on(event, fn);
52-
else if (Array.isArray(emitter._events[event]))
53-
emitter._events[event].unshift(fn);
54-
else
55-
emitter._events[event] = [fn, emitter._events[event]];
56-
}
45+
46+
// This is a hack to make sure that our error handler is attached before any
47+
// userland ones. NEVER DO THIS. This is here only because this code needs
48+
// to continue to work with older versions of Node.js that do not include
49+
// the prependListener() method. The goal is to eventually remove this hack.
50+
if (!emitter._events || !emitter._events[event])
51+
emitter.on(event, fn);
52+
else if (Array.isArray(emitter._events[event]))
53+
emitter._events[event].unshift(fn);
54+
else
55+
emitter._events[event] = [fn, emitter._events[event]];
5756
}
5857

5958
function ReadableState(options, stream) {

lib/_tls_legacy.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -878,8 +878,8 @@ SecurePair.prototype.error = function(returnOnly) {
878878
/peer did not return a certificate/.test(err.message)) {
879879
// Not really an error.
880880
this.destroy();
881-
} else {
882-
if (!returnOnly) this.cleartext.emit('error', err);
881+
} else if (!returnOnly) {
882+
this.cleartext.emit('error', err);
883883
}
884884
return err;
885885
};

lib/child_process.js

+6-8
Original file line numberDiff line numberDiff line change
@@ -324,11 +324,10 @@ exports.execFile = function(file /*, args, options, callback*/) {
324324
if (stdoutLen > options.maxBuffer) {
325325
ex = new Error('stdout maxBuffer exceeded');
326326
kill();
327+
} else if (encoding) {
328+
_stdout += chunk;
327329
} else {
328-
if (encoding)
329-
_stdout += chunk;
330-
else
331-
_stdout.push(chunk);
330+
_stdout.push(chunk);
332331
}
333332
});
334333
}
@@ -343,11 +342,10 @@ exports.execFile = function(file /*, args, options, callback*/) {
343342
if (stderrLen > options.maxBuffer) {
344343
ex = new Error('stderr maxBuffer exceeded');
345344
kill();
345+
} else if (encoding) {
346+
_stderr += chunk;
346347
} else {
347-
if (encoding)
348-
_stderr += chunk;
349-
else
350-
_stderr.push(chunk);
348+
_stderr.push(chunk);
351349
}
352350
});
353351
}

lib/events.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -280,13 +280,11 @@ function _addListener(target, type, listener, prepend) {
280280
// Adding the second element, need to change to array.
281281
existing = events[type] =
282282
prepend ? [listener, existing] : [existing, listener];
283-
} else {
284283
// If we've already got an array, just append.
285-
if (prepend) {
286-
existing.unshift(listener);
287-
} else {
288-
existing.push(listener);
289-
}
284+
} else if (prepend) {
285+
existing.unshift(listener);
286+
} else {
287+
existing.push(listener);
290288
}
291289

292290
// Check for listener leak

lib/fs.js

+11-13
Original file line numberDiff line numberDiff line change
@@ -1262,21 +1262,19 @@ function writeAll(fd, isUserFd, buffer, offset, length, position, callback) {
12621262
callback(writeErr);
12631263
});
12641264
}
1265-
} else {
1266-
if (written === length) {
1267-
if (isUserFd) {
1268-
callback(null);
1269-
} else {
1270-
fs.close(fd, callback);
1271-
}
1265+
} else if (written === length) {
1266+
if (isUserFd) {
1267+
callback(null);
12721268
} else {
1273-
offset += written;
1274-
length -= written;
1275-
if (position !== null) {
1276-
position += written;
1277-
}
1278-
writeAll(fd, isUserFd, buffer, offset, length, position, callback);
1269+
fs.close(fd, callback);
1270+
}
1271+
} else {
1272+
offset += written;
1273+
length -= written;
1274+
if (position !== null) {
1275+
position += written;
12791276
}
1277+
writeAll(fd, isUserFd, buffer, offset, length, position, callback);
12801278
}
12811279
});
12821280
}

lib/internal/http2/core.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -187,12 +187,11 @@ function onSessionHeaders(id, cat, flags, headers) {
187187
}
188188
} else if (cat === NGHTTP2_HCAT_PUSH_RESPONSE) {
189189
event = 'push';
190-
} else { // cat === NGHTTP2_HCAT_HEADERS:
191-
if (!endOfStream && status !== undefined && status >= 200) {
192-
event = 'response';
193-
} else {
194-
event = endOfStream ? 'trailers' : 'headers';
195-
}
190+
// cat === NGHTTP2_HCAT_HEADERS:
191+
} else if (!endOfStream && status !== undefined && status >= 200) {
192+
event = 'response';
193+
} else {
194+
event = endOfStream ? 'trailers' : 'headers';
196195
}
197196
debug(`[${sessionName(owner[kType])}] emitting stream '${event}' event`);
198197
process.nextTick(emit, stream, event, obj, flags, headers);

lib/path.js

+8-10
Original file line numberDiff line numberDiff line change
@@ -455,17 +455,15 @@ const win32 = {
455455
} else {
456456
return '';
457457
}
458+
} else if (isAbsolute) {
459+
if (tail.length > 0)
460+
return device + '\\' + tail;
461+
else
462+
return device + '\\';
463+
} else if (tail.length > 0) {
464+
return device + tail;
458465
} else {
459-
if (isAbsolute) {
460-
if (tail.length > 0)
461-
return device + '\\' + tail;
462-
else
463-
return device + '\\';
464-
} else if (tail.length > 0) {
465-
return device + tail;
466-
} else {
467-
return device;
468-
}
466+
return device;
469467
}
470468
},
471469

lib/querystring.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,8 @@ function parse(qs, sep, eq, options) {
315315
sepIdx = eqIdx = 0;
316316
continue;
317317
}
318-
} else {
319-
if (lastPos < end)
320-
value += qs.slice(lastPos, end);
318+
} else if (lastPos < end) {
319+
value += qs.slice(lastPos, end);
321320
}
322321

323322
if (key.length > 0 && keyEncoded)

lib/url.js

+7-9
Original file line numberDiff line numberDiff line change
@@ -128,16 +128,14 @@ Url.prototype.parse = function parse(url, parseQueryString, slashesDenoteHost) {
128128
if (isWs)
129129
continue;
130130
lastPos = start = i;
131-
} else {
132-
if (inWs) {
133-
if (!isWs) {
134-
end = -1;
135-
inWs = false;
136-
}
137-
} else if (isWs) {
138-
end = i;
139-
inWs = true;
131+
} else if (inWs) {
132+
if (!isWs) {
133+
end = -1;
134+
inWs = false;
140135
}
136+
} else if (isWs) {
137+
end = i;
138+
inWs = true;
141139
}
142140

143141
// Only convert backslashes while we haven't seen a split character

test/.eslintrc.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ rules:
55
# http://eslint.org/docs/rules/#ecmascript-6
66
no-var: error
77
prefer-const: error
8+
symbol-description: off
89

910
# Custom rules in tools/eslint-rules
1011
prefer-assert-iferror: error

test/abort/test-abort-uncaught-exception.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,10 @@ function run(flags, signals) {
2424
assert.strictEqual(code, 0xC0000005);
2525
else
2626
assert.strictEqual(code, 1);
27+
} else if (signals) {
28+
assert(signals.includes(sig), `Unexpected signal ${sig}`);
2729
} else {
28-
if (signals)
29-
assert(signals.includes(sig), `Unexpected signal ${sig}`);
30-
else
31-
assert.strictEqual(sig, null);
30+
assert.strictEqual(sig, null);
3231
}
3332
}));
3433
}

test/parallel/test-cluster-disconnect-unshared-tcp.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,8 @@ if (cluster.isMaster) {
3737
unbound.disconnect();
3838
unbound.on('disconnect', cluster.disconnect);
3939
}
40-
} else {
41-
if (process.env.BOUND === 'y') {
42-
const source = net.createServer();
40+
} else if (process.env.BOUND === 'y') {
41+
const source = net.createServer();
4342

44-
source.listen(0);
45-
}
43+
source.listen(0);
4644
}

test/parallel/test-cluster-disconnect-unshared-udp.js

+3-5
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,8 @@ if (cluster.isMaster) {
4040
unbound.disconnect();
4141
unbound.on('disconnect', cluster.disconnect);
4242
}
43-
} else {
44-
if (process.env.BOUND === 'y') {
45-
const source = dgram.createSocket('udp4');
43+
} else if (process.env.BOUND === 'y') {
44+
const source = dgram.createSocket('udp4');
4645

47-
source.bind(0);
48-
}
46+
source.bind(0);
4947
}

test/parallel/test-cluster-worker-destroy.js

+10-12
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,15 @@ if (cluster.isMaster) {
4141
worker.on('disconnect', common.mustCall());
4242
worker.on('exit', common.mustCall());
4343
});
44-
} else {
45-
if (cluster.worker.id === 1) {
46-
// Call destroy when worker is disconnected
47-
cluster.worker.process.on('disconnect', function() {
48-
cluster.worker.destroy();
49-
});
50-
51-
const w = cluster.worker.disconnect();
52-
assert.strictEqual(w, cluster.worker);
53-
} else {
54-
// Call destroy when worker is not disconnected yet
44+
} else if (cluster.worker.id === 1) {
45+
// Call destroy when worker is disconnected
46+
cluster.worker.process.on('disconnect', function() {
5547
cluster.worker.destroy();
56-
}
48+
});
49+
50+
const w = cluster.worker.disconnect();
51+
assert.strictEqual(w, cluster.worker);
52+
} else {
53+
// Call destroy when worker is not disconnected yet
54+
cluster.worker.destroy();
5755
}

0 commit comments

Comments
 (0)