Skip to content

Commit 7178588

Browse files
authored
lib: prefer logical assignment
PR-URL: #55044 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: LiviaMedeiros <livia@cirno.name>
1 parent 09d10b5 commit 7178588

Some content is hidden

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

73 files changed

+177
-301
lines changed

benchmark/_http-benchmarkers.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ class TestDoubleBenchmarker {
110110
}
111111

112112
create(options) {
113-
process.env.duration = process.env.duration || options.duration || 5;
113+
process.env.duration ||= options.duration || 5;
114114

115115
const scheme = options.scheme || 'http';
116116
const env = {

benchmark/common.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,7 @@ class Benchmark {
113113
}
114114
const [, key, value] = match;
115115
if (configs[key] !== undefined) {
116-
if (!cliOptions[key])
117-
cliOptions[key] = [];
116+
cliOptions[key] ||= [];
118117
cliOptions[key].push(
119118
// Infer the type from the config object and parse accordingly
120119
typeof configs[key][0] === 'number' ? +value : value,
@@ -177,10 +176,9 @@ class Benchmark {
177176

178177
http(options, cb) {
179178
const http_options = { ...options };
180-
http_options.benchmarker = http_options.benchmarker ||
181-
this.config.benchmarker ||
182-
this.extra_options.benchmarker ||
183-
http_benchmarkers.default_http_benchmarker;
179+
http_options.benchmarker ||= this.config.benchmarker ||
180+
this.extra_options.benchmarker ||
181+
http_benchmarkers.default_http_benchmarker;
184182
http_benchmarkers.run(
185183
http_options, (error, code, used_benchmarker, result, elapsed) => {
186184
if (cb) {

benchmark/es/defaultparams-bench.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ const bench = common.createBenchmark(main, {
99
});
1010

1111
function oldStyleDefaults(x, y) {
12-
x = x || 1;
13-
y = y || 2;
12+
x ||= 1;
13+
y ||= 2;
1414
assert.strictEqual(x, 1);
1515
assert.strictEqual(y, 2);
1616
}

benchmark/perf_hooks/resourcetiming.js

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,12 @@ function createTimingInfo({
2121
finalConnectionTimingInfo = null,
2222
}) {
2323
if (finalConnectionTimingInfo !== null) {
24-
finalConnectionTimingInfo.domainLookupStartTime =
25-
finalConnectionTimingInfo.domainLookupStartTime || 0;
26-
finalConnectionTimingInfo.domainLookupEndTime =
27-
finalConnectionTimingInfo.domainLookupEndTime || 0;
28-
finalConnectionTimingInfo.connectionStartTime =
29-
finalConnectionTimingInfo.connectionStartTime || 0;
30-
finalConnectionTimingInfo.connectionEndTime =
31-
finalConnectionTimingInfo.connectionEndTime || 0;
32-
finalConnectionTimingInfo.secureConnectionStartTime =
33-
finalConnectionTimingInfo.secureConnectionStartTime || 0;
34-
finalConnectionTimingInfo.ALPNNegotiatedProtocol =
35-
finalConnectionTimingInfo.ALPNNegotiatedProtocol || [];
24+
finalConnectionTimingInfo.domainLookupStartTime ||= 0;
25+
finalConnectionTimingInfo.domainLookupEndTime ||= 0;
26+
finalConnectionTimingInfo.connectionStartTime ||= 0;
27+
finalConnectionTimingInfo.connectionEndTime ||= 0;
28+
finalConnectionTimingInfo.secureConnectionStartTime ||= 0;
29+
finalConnectionTimingInfo.ALPNNegotiatedProtocol ||= [];
3630
}
3731
return {
3832
startTime,

benchmark/zlib/deflate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const bench = common.createBenchmark(main, {
1010

1111
function main({ n, method, inputLen }) {
1212
// Default method value for testing.
13-
method = method || 'deflate';
13+
method ||= 'deflate';
1414
const chunk = Buffer.alloc(inputLen, 'a');
1515

1616
switch (method) {

benchmark/zlib/inflate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const bench = common.createBenchmark(main, {
1010

1111
function main({ n, method, inputLen }) {
1212
// Default method value for tests.
13-
method = method || 'inflate';
13+
method ||= 'inflate';
1414
const chunk = zlib.deflateSync(Buffer.alloc(inputLen, 'a'));
1515

1616
let i = 0;

eslint.config.mjs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ export default [
144144
ignorePattern: '.*',
145145
},
146146
}],
147+
'logical-assignment-operators': ['error', 'always', { enforceForIfStatements: true }],
147148
'default-case-last': 'error',
148149
'dot-notation': 'error',
149150
'eqeqeq': ['error', 'smart'],

lib/_http_agent.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -246,9 +246,7 @@ Agent.prototype.addRequest = function addRequest(req, options, port/* legacy */,
246246
normalizeServerName(options, req);
247247

248248
const name = this.getName(options);
249-
if (!this.sockets[name]) {
250-
this.sockets[name] = [];
251-
}
249+
this.sockets[name] ||= [];
252250

253251
const freeSockets = this.freeSockets[name];
254252
let socket;
@@ -284,9 +282,7 @@ Agent.prototype.addRequest = function addRequest(req, options, port/* legacy */,
284282
} else {
285283
debug('wait for socket');
286284
// We are over limit so we'll add it to the queue.
287-
if (!this.requests[name]) {
288-
this.requests[name] = [];
289-
}
285+
this.requests[name] ||= [];
290286

291287
// Used to create sockets for pending requests from different origin
292288
req[kRequestOptions] = options;
@@ -313,9 +309,7 @@ Agent.prototype.createSocket = function createSocket(req, options, cb) {
313309
const oncreate = once((err, s) => {
314310
if (err)
315311
return cb(err);
316-
if (!this.sockets[name]) {
317-
this.sockets[name] = [];
318-
}
312+
this.sockets[name] ||= [];
319313
this.sockets[name].push(s);
320314
this.totalSocketCount++;
321315
debug('sockets', name, this.sockets[name].length, this.totalSocketCount);

lib/_http_client.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,8 +347,8 @@ function ClientRequest(input, options, cb) {
347347
opts = { ...optsWithoutSignal };
348348
if (opts.socketPath) {
349349
opts.path = opts.socketPath;
350-
} else if (opts.path) {
351-
opts.path = undefined;
350+
} else {
351+
opts.path &&= undefined;
352352
}
353353
}
354354
if (typeof opts.createConnection === 'function') {

lib/_http_server.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,7 @@ function writeHead(statusCode, reason, obj) {
357357
this.statusMessage = reason;
358358
} else {
359359
// writeHead(statusCode[, headers])
360-
if (!this.statusMessage)
361-
this.statusMessage = STATUS_CODES[statusCode] || 'unknown';
360+
this.statusMessage ||= STATUS_CODES[statusCode] || 'unknown';
362361
obj ??= reason;
363362
}
364363
this.statusCode = statusCode;
@@ -510,9 +509,7 @@ function storeHTTPOptions(options) {
510509

511510
function setupConnectionsTracking() {
512511
// Start connection handling
513-
if (!this[kConnections]) {
514-
this[kConnections] = new ConnectionsList();
515-
}
512+
this[kConnections] ||= new ConnectionsList();
516513

517514
if (this[kConnectionsCheckingInterval]) {
518515
clearInterval(this[kConnectionsCheckingInterval]);
@@ -923,8 +920,7 @@ function onParserExecuteCommon(server, socket, parser, state, ret, d) {
923920
const req = parser.incoming;
924921
debug('SERVER upgrade or connect', req.method);
925922

926-
if (!d)
927-
d = parser.getCurrentBuffer();
923+
d ||= parser.getCurrentBuffer();
928924

929925
socket.removeListener('data', state.onData);
930926
socket.removeListener('end', state.onEnd);
@@ -962,7 +958,7 @@ function onParserExecuteCommon(server, socket, parser, state, ret, d) {
962958
}
963959

964960
function clearIncoming(req) {
965-
req = req || this;
961+
req ||= this;
966962
const parser = req.socket?.parser;
967963
// Reset the .incoming property so that the request object can be gc'ed.
968964
if (parser && parser.incoming === req) {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy