From 095c0de94d818088cacf2c33ad4913768c15024a Mon Sep 17 00:00:00 2001 From: Rich Trott Date: Tue, 20 Jun 2017 14:37:00 -0700 Subject: [PATCH] benchmark,lib,test: use braces for multiline block MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit For if/else and loops where the bodies span more than one line, use curly braces. PR-URL: https://github.com/nodejs/node/pull/13828 Ref: https://github.com/nodejs/node/pull/13623#discussion_r123048602 Reviewed-By: Anna Henningsen Reviewed-By: Colin Ihrig Reviewed-By: Michaƫl Zasso Reviewed-By: James M Snell --- benchmark/buffers/buffer-iterate.js | 13 +++++++----- benchmark/dgram/array-vs-concat.js | 6 ++++-- benchmark/dgram/multi-buffer.js | 6 ++++-- benchmark/dgram/offset-length.js | 6 ++++-- benchmark/dgram/single-buffer.js | 6 ++++-- benchmark/url/url-searchparams-iteration.js | 3 ++- lib/_http_client.js | 5 +++-- lib/_http_outgoing.js | 8 +++---- lib/_http_server.js | 6 ++++-- lib/buffer.js | 7 ++++--- lib/child_process.js | 7 ++++--- lib/fs.js | 9 +++++--- lib/inspector.js | 12 +++++++---- lib/internal/process.js | 10 +++++---- lib/net.js | 12 +++++++---- lib/readline.js | 6 ++++-- lib/tls.js | 3 ++- lib/util.js | 3 ++- test/common/index.js | 21 ++++++++++++------- .../test-domain-uncaught-exception.js | 5 +++-- test/parallel/test-fs-null-bytes.js | 6 ++++-- test/pummel/test-net-write-callbacks.js | 3 ++- 22 files changed, 103 insertions(+), 60 deletions(-) diff --git a/benchmark/buffers/buffer-iterate.js b/benchmark/buffers/buffer-iterate.js index fb8abc1aee1c63..7c2044422245c4 100644 --- a/benchmark/buffers/buffer-iterate.js +++ b/benchmark/buffers/buffer-iterate.js @@ -29,9 +29,11 @@ function main(conf) { function benchFor(buffer, n) { bench.start(); - for (var k = 0; k < n; k++) - for (var i = 0; i < buffer.length; i++) + for (var k = 0; k < n; k++) { + for (var i = 0; i < buffer.length; i++) { assert(buffer[i] === 0); + } + } bench.end(n); } @@ -39,10 +41,11 @@ function benchFor(buffer, n) { function benchForOf(buffer, n) { bench.start(); - for (var k = 0; k < n; k++) - for (var b of buffer) + for (var k = 0; k < n; k++) { + for (var b of buffer) { assert(b === 0); - + } + } bench.end(n); } diff --git a/benchmark/dgram/array-vs-concat.js b/benchmark/dgram/array-vs-concat.js index 8b1d34d0e746a4..681abd6afa13e0 100644 --- a/benchmark/dgram/array-vs-concat.js +++ b/benchmark/dgram/array-vs-concat.js @@ -46,17 +46,19 @@ function server() { var onsend = type === 'concat' ? onsendConcat : onsendMulti; function onsendConcat() { - if (sent++ % num === 0) + if (sent++ % num === 0) { for (var i = 0; i < num; i++) { socket.send(Buffer.concat(chunk), PORT, '127.0.0.1', onsend); } + } } function onsendMulti() { - if (sent++ % num === 0) + if (sent++ % num === 0) { for (var i = 0; i < num; i++) { socket.send(chunk, PORT, '127.0.0.1', onsend); } + } } socket.on('listening', function() { diff --git a/benchmark/dgram/multi-buffer.js b/benchmark/dgram/multi-buffer.js index 3277547119c35e..6a7fc9bfaf83ee 100644 --- a/benchmark/dgram/multi-buffer.js +++ b/benchmark/dgram/multi-buffer.js @@ -45,9 +45,11 @@ function server() { var socket = dgram.createSocket('udp4'); function onsend() { - if (sent++ % num === 0) - for (var i = 0; i < num; i++) + if (sent++ % num === 0) { + for (var i = 0; i < num; i++) { socket.send(chunk, PORT, '127.0.0.1', onsend); + } + } } socket.on('listening', function() { diff --git a/benchmark/dgram/offset-length.js b/benchmark/dgram/offset-length.js index 5b7762b21e70e1..b897707ded5e58 100644 --- a/benchmark/dgram/offset-length.js +++ b/benchmark/dgram/offset-length.js @@ -37,9 +37,11 @@ function server() { var socket = dgram.createSocket('udp4'); function onsend() { - if (sent++ % num === 0) - for (var i = 0; i < num; i++) + if (sent++ % num === 0) { + for (var i = 0; i < num; i++) { socket.send(chunk, 0, chunk.length, PORT, '127.0.0.1', onsend); + } + } } socket.on('listening', function() { diff --git a/benchmark/dgram/single-buffer.js b/benchmark/dgram/single-buffer.js index e01b60b4297c1d..8b81d7fbfc0794 100644 --- a/benchmark/dgram/single-buffer.js +++ b/benchmark/dgram/single-buffer.js @@ -37,9 +37,11 @@ function server() { var socket = dgram.createSocket('udp4'); function onsend() { - if (sent++ % num === 0) - for (var i = 0; i < num; i++) + if (sent++ % num === 0) { + for (var i = 0; i < num; i++) { socket.send(chunk, PORT, '127.0.0.1', onsend); + } + } } socket.on('listening', function() { diff --git a/benchmark/url/url-searchparams-iteration.js b/benchmark/url/url-searchparams-iteration.js index 833271ef306309..89919af7255184 100644 --- a/benchmark/url/url-searchparams-iteration.js +++ b/benchmark/url/url-searchparams-iteration.js @@ -32,11 +32,12 @@ function iterator(n) { const noDead = []; bench.start(); - for (var i = 0; i < n; i += 1) + for (var i = 0; i < n; i += 1) { for (var pair of params) { noDead[0] = pair[0]; noDead[1] = pair[1]; } + } bench.end(n); assert.strictEqual(noDead[0], 'three'); diff --git a/lib/_http_client.js b/lib/_http_client.js index e095051270d670..29ea688ecb1bf0 100644 --- a/lib/_http_client.js +++ b/lib/_http_client.js @@ -319,12 +319,13 @@ ClientRequest.prototype.abort = function abort() { this.aborted = Date.now(); // If we're aborting, we don't care about any more response data. - if (this.res) + if (this.res) { this.res._dump(); - else + } else { this.once('response', function(res) { res._dump(); }); + } // In the event that we don't have a socket, we will pop out of // the request queue through handling in onSocket. diff --git a/lib/_http_outgoing.js b/lib/_http_outgoing.js index 636afc4e46c56a..ccb66f742f91d0 100644 --- a/lib/_http_outgoing.js +++ b/lib/_http_outgoing.js @@ -219,12 +219,13 @@ OutgoingMessage.prototype.setTimeout = function setTimeout(msecs, callback) { // any messages, before ever calling this. In that case, just skip // it, since something else is destroying this connection anyway. OutgoingMessage.prototype.destroy = function destroy(error) { - if (this.socket) + if (this.socket) { this.socket.destroy(error); - else + } else { this.once('socket', function(socket) { socket.destroy(error); }); + } }; @@ -505,8 +506,7 @@ function matchHeader(self, state, field, value) { function validateHeader(msg, name, value) { if (typeof name !== 'string' || !name || !checkIsHttpToken(name)) - throw new TypeError( - 'Header name must be a valid HTTP Token ["' + name + '"]'); + throw new TypeError(`Header name must be a valid HTTP Token ["${name}"]`); if (value === undefined) throw new Error('"value" required in setHeader("' + name + '", value)'); if (msg._header) diff --git a/lib/_http_server.js b/lib/_http_server.js index 854ae56387e8cf..c3a10112ff2887 100644 --- a/lib/_http_server.js +++ b/lib/_http_server.js @@ -185,9 +185,10 @@ function writeHead(statusCode, reason, obj) { var originalStatusCode = statusCode; statusCode |= 0; - if (statusCode < 100 || statusCode > 999) + if (statusCode < 100 || statusCode > 999) { throw new errors.RangeError('ERR_HTTP_INVALID_STATUS_CODE', originalStatusCode); + } if (typeof reason === 'string') { @@ -224,9 +225,10 @@ function writeHead(statusCode, reason, obj) { headers = obj; } - if (common._checkInvalidHeaderChar(this.statusMessage)) + if (common._checkInvalidHeaderChar(this.statusMessage)) { throw new errors.Error('ERR_HTTP_INVALID_CHAR', 'Invalid character in statusMessage.'); + } var statusLine = 'HTTP/1.1 ' + statusCode + ' ' + this.statusMessage + CRLF; diff --git a/lib/buffer.js b/lib/buffer.js index bfa8656483f602..89bbfd2474dbe0 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -207,13 +207,14 @@ Object.setPrototypeOf(Buffer, Uint8Array); function assertSize(size) { let err = null; - if (typeof size !== 'number') + if (typeof size !== 'number') { err = new TypeError('"size" argument must be a number'); - else if (size < 0) + } else if (size < 0) { err = new RangeError('"size" argument must not be negative'); - else if (size > binding.kMaxLength) + } else if (size > binding.kMaxLength) { err = new RangeError('"size" argument must not be larger ' + 'than ' + binding.kMaxLength); + } if (err) { Error.captureStackTrace(err, assertSize); diff --git a/lib/child_process.js b/lib/child_process.js index c8f844d617c380..8f7d25206226fb 100644 --- a/lib/child_process.js +++ b/lib/child_process.js @@ -533,15 +533,16 @@ function spawnSync(/*file, args, options*/) { var input = options.stdio[i] && options.stdio[i].input; if (input != null) { var pipe = options.stdio[i] = util._extend({}, options.stdio[i]); - if (isUint8Array(input)) + if (isUint8Array(input)) { pipe.input = input; - else if (typeof input === 'string') + } else if (typeof input === 'string') { pipe.input = Buffer.from(input, options.encoding); - else + } else { throw new TypeError(util.format( 'stdio[%d] should be Buffer, Uint8Array or string not %s', i, typeof input)); + } } } diff --git a/lib/fs.js b/lib/fs.js index 61f227423b0f01..a2401acf31bc83 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -1951,10 +1951,11 @@ ReadStream.prototype.open = function() { }; ReadStream.prototype._read = function(n) { - if (typeof this.fd !== 'number') + if (typeof this.fd !== 'number') { return this.once('open', function() { this._read(n); }); + } if (this.destroyed) return; @@ -2116,10 +2117,11 @@ WriteStream.prototype._write = function(data, encoding, cb) { if (!(data instanceof Buffer)) return this.emit('error', new Error('Invalid data')); - if (typeof this.fd !== 'number') + if (typeof this.fd !== 'number') { return this.once('open', function() { this._write(data, encoding, cb); }); + } var self = this; fs.write(this.fd, data, 0, data.length, this.pos, function(er, bytes) { @@ -2151,10 +2153,11 @@ function writev(fd, chunks, position, callback) { WriteStream.prototype._writev = function(data, cb) { - if (typeof this.fd !== 'number') + if (typeof this.fd !== 'number') { return this.once('open', function() { this._writev(data, cb); }); + } const self = this; const len = data.length; diff --git a/lib/inspector.js b/lib/inspector.js index e4c401a9951da7..d73d8a23491854 100644 --- a/lib/inspector.js +++ b/lib/inspector.js @@ -41,22 +41,26 @@ class Session extends EventEmitter { } post(method, params, callback) { - if (typeof method !== 'string') + if (typeof method !== 'string') { throw new TypeError( `"method" must be a string, got ${typeof method} instead`); + } if (!callback && util.isFunction(params)) { callback = params; params = null; } - if (params && typeof params !== 'object') + if (params && typeof params !== 'object') { throw new TypeError( `"params" must be an object, got ${typeof params} instead`); - if (callback && typeof callback !== 'function') + } + if (callback && typeof callback !== 'function') { throw new TypeError( `"callback" must be a function, got ${typeof callback} instead`); + } - if (!this[connectionSymbol]) + if (!this[connectionSymbol]) { throw new Error('Session is not connected'); + } const id = this[nextIdSymbol]++; const message = {id, method}; if (params) { diff --git a/lib/internal/process.js b/lib/internal/process.js index 5254b5993bdea6..1636e73b7e009d 100644 --- a/lib/internal/process.js +++ b/lib/internal/process.js @@ -46,10 +46,12 @@ function setup_cpuUsage() { } // If a previous value was passed in, return diff of current from previous. - if (prevValue) return { - user: cpuValues[0] - prevValue.user, - system: cpuValues[1] - prevValue.system - }; + if (prevValue) { + return { + user: cpuValues[0] - prevValue.user, + system: cpuValues[1] - prevValue.system + }; + } // If no previous value passed in, return current value. return { diff --git a/lib/net.js b/lib/net.js index 121757aa108472..5f4eec89ec87b9 100644 --- a/lib/net.js +++ b/lib/net.js @@ -1005,19 +1005,23 @@ function lookupAndConnect(self, options) { var localAddress = options.localAddress; var localPort = options.localPort; - if (localAddress && !cares.isIP(localAddress)) + if (localAddress && !cares.isIP(localAddress)) { throw new TypeError('"localAddress" option must be a valid IP: ' + localAddress); + } - if (localPort && typeof localPort !== 'number') + if (localPort && typeof localPort !== 'number') { throw new TypeError('"localPort" option should be a number: ' + localPort); + } if (typeof port !== 'undefined') { - if (typeof port !== 'number' && typeof port !== 'string') + if (typeof port !== 'number' && typeof port !== 'string') { throw new TypeError('"port" option should be a number or string: ' + port); - if (!isLegalPort(port)) + } + if (!isLegalPort(port)) { throw new RangeError('"port" option should be >= 0 and < 65536: ' + port); + } } port |= 0; diff --git a/lib/readline.js b/lib/readline.js index 51d1da5770edb2..5e4318e44b6ccc 100644 --- a/lib/readline.js +++ b/lib/readline.js @@ -285,16 +285,18 @@ Interface.prototype._onLine = function(line) { }; Interface.prototype._writeToOutput = function _writeToOutput(stringToWrite) { - if (typeof stringToWrite !== 'string') + if (typeof stringToWrite !== 'string') { throw new errors.TypeError( 'ERR_INVALID_ARG_TYPE', 'stringToWrite', 'string', stringToWrite ); + } - if (this.output !== null && this.output !== undefined) + if (this.output !== null && this.output !== undefined) { this.output.write(stringToWrite); + } }; Interface.prototype._addHistory = function() { diff --git a/lib/tls.js b/lib/tls.js index 2247bb9ee56d18..5a03c3c30f8b24 100644 --- a/lib/tls.js +++ b/lib/tls.js @@ -122,9 +122,10 @@ function check(hostParts, pattern, wildcards) { return false; // Check host parts from right to left first. - for (var i = hostParts.length - 1; i > 0; i -= 1) + for (var i = hostParts.length - 1; i > 0; i -= 1) { if (hostParts[i] !== patternParts[i]) return false; + } const hostSubdomain = hostParts[0]; const patternSubdomain = patternParts[0]; diff --git a/lib/util.js b/lib/util.js index c75117b711a58f..fdc4a9b3b66f94 100644 --- a/lib/util.js +++ b/lib/util.js @@ -962,9 +962,10 @@ exports.inherits = function(ctor, superCtor) { if (superCtor === undefined || superCtor === null) throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'superCtor', 'function'); - if (superCtor.prototype === undefined) + if (superCtor.prototype === undefined) { throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'superCtor.prototype', 'function'); + } ctor.super_ = superCtor; Object.setPrototypeOf(ctor.prototype, superCtor.prototype); }; diff --git a/test/common/index.js b/test/common/index.js index 7f00820123711f..6e910881523e99 100644 --- a/test/common/index.js +++ b/test/common/index.js @@ -429,9 +429,11 @@ function leakedGlobals() { const leaked = []; // eslint-disable-next-line no-var - for (var val in global) - if (!knownGlobals.includes(global[val])) + for (var val in global) { + if (!knownGlobals.includes(global[val])) { leaked.push(val); + } + } if (global.__coverage__) { return leaked.filter((varname) => !/^(?:cov_|__cov)/.test(varname)); @@ -700,9 +702,10 @@ Object.defineProperty(exports, 'hasSmallICU', { exports.expectsError = function expectsError({code, type, message}) { return function(error) { assert.strictEqual(error.code, code); - if (type !== undefined) + if (type !== undefined) { assert(error instanceof type, `${error} is not the expected type ${type}`); + } if (message instanceof RegExp) { assert(message.test(error.message), `${error.message} does not match ${message}`); @@ -758,11 +761,13 @@ exports.getTTYfd = function getTTYfd() { if (!tty.isatty(tty_fd)) tty_fd++; else if (!tty.isatty(tty_fd)) tty_fd++; else if (!tty.isatty(tty_fd)) tty_fd++; - else try { - tty_fd = require('fs').openSync('/dev/tty'); - } catch (e) { - // There aren't any tty fd's available to use. - return -1; + else { + try { + tty_fd = require('fs').openSync('/dev/tty'); + } catch (e) { + // There aren't any tty fd's available to use. + return -1; + } } return tty_fd; }; diff --git a/test/parallel/test-domain-uncaught-exception.js b/test/parallel/test-domain-uncaught-exception.js index 2a157f9398b38e..0e5548df563803 100644 --- a/test/parallel/test-domain-uncaught-exception.js +++ b/test/parallel/test-domain-uncaught-exception.js @@ -182,10 +182,11 @@ if (process.argv[2] === 'child') { // Make sure that all expected messages were sent from the // child process test.expectedMessages.forEach(function(expectedMessage) { - if (test.messagesReceived === undefined || - test.messagesReceived.indexOf(expectedMessage) === -1) + const msgs = test.messagesReceived; + if (msgs === undefined || !msgs.includes(expectedMessage)) { assert.fail(`test ${test.fn.name} should have sent message: ${ expectedMessage} but didn't`); + } }); if (test.messagesReceived) { diff --git a/test/parallel/test-fs-null-bytes.js b/test/parallel/test-fs-null-bytes.js index a21fe516a09993..0552801b2deaa9 100644 --- a/test/parallel/test-fs-null-bytes.js +++ b/test/parallel/test-fs-null-bytes.js @@ -33,13 +33,15 @@ function check(async, sync) { assert.strictEqual(er.code, 'ENOENT'); }); - if (sync) + if (sync) { assert.throws(() => { sync.apply(null, argsSync); }, expected); + } - if (async) + if (async) { async.apply(null, argsAsync); + } } check(fs.access, fs.accessSync, 'foo\u0000bar'); diff --git a/test/pummel/test-net-write-callbacks.js b/test/pummel/test-net-write-callbacks.js index 7c3df30f582a18..10aff090583cea 100644 --- a/test/pummel/test-net-write-callbacks.js +++ b/test/pummel/test-net-write-callbacks.js @@ -46,9 +46,10 @@ function makeCallback(c) { if (called) throw new Error(`called callback #${c} more than once`); called = true; - if (c < lastCalled) + if (c < lastCalled) { throw new Error( `callbacks out of order. last=${lastCalled} current=${c}`); + } lastCalled = c; cbcount++; };