Skip to content

Commit 2fc43fb

Browse files
BridgeARMylesBorins
authored andcommitted
lib: switch to object spread where possible
Use the object spread notation instead of using Object.assign. It is not only easier to read it is also faster as of V8 6.8. PR-URL: #25104 Reviewed-By: Gus Caplan <me@gus.host> Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Yuta Hiroto <hello@hiroppy.me> Reviewed-By: Minwoo Jung <minwoo@nodesource.com> Reviewed-By: Denys Otrishko <shishugi@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent 54e42f0 commit 2fc43fb

12 files changed

+41
-37
lines changed

lib/.eslintrc.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
rules:
2+
prefer-object-spread: error
23
no-restricted-syntax:
34
# Config copied from .eslintrc.js
45
- error

lib/_tls_wrap.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ function initRead(tls, wrapped) {
289289
*/
290290

291291
function TLSSocket(socket, opts) {
292-
const tlsOptions = Object.assign({}, opts);
292+
const tlsOptions = { ...opts };
293293

294294
if (tlsOptions.ALPNProtocols)
295295
tls.convertALPNProtocols(tlsOptions.ALPNProtocols, tlsOptions);

lib/child_process.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ function normalizeExecArgs(command, options, callback) {
136136
}
137137

138138
// Make a shallow copy so we don't clobber the user's options object.
139-
options = Object.assign({}, options);
139+
options = { ...options };
140140
options.shell = typeof options.shell === 'string' ? options.shell : true;
141141

142142
return {
@@ -470,7 +470,7 @@ function normalizeSpawnArguments(file, args, options) {
470470
}
471471

472472
// Make a shallow copy so we don't clobber the user's options object.
473-
options = Object.assign({}, options);
473+
options = { ...options };
474474

475475
if (options.shell) {
476476
const command = [file].concat(args).join(' ');

lib/console.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,11 @@ Console.prototype.warn = function warn(...args) {
225225
Console.prototype.error = Console.prototype.warn;
226226

227227
Console.prototype.dir = function dir(object, options) {
228-
options = Object.assign({
229-
customInspect: false
230-
}, this[kGetInspectOptions](this._stdout), options);
228+
options = {
229+
customInspect: false,
230+
...this[kGetInspectOptions](this._stdout),
231+
...options
232+
};
231233
write(this._ignoreErrors,
232234
this._stdout,
233235
util.inspect(object, options),

lib/internal/child_process.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -636,7 +636,7 @@ function setupChannel(target, channel) {
636636
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
637637
}
638638

639-
options = Object.assign({ swallowErrors: false }, options);
639+
options = { swallowErrors: false, ...options };
640640

641641
if (this.connected) {
642642
return this._send(message, handle, options, callback);

lib/internal/console/constructor.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -271,10 +271,11 @@ Console.prototype.warn = function warn(...args) {
271271
Console.prototype.error = Console.prototype.warn;
272272

273273
Console.prototype.dir = function dir(object, options) {
274-
options = Object.assign({
275-
customInspect: false
276-
}, this[kGetInspectOptions](this._stdout), options);
277-
this[kWriteToConsole](kUseStdout, util.inspect(object, options));
274+
this[kWriteToConsole](kUseStdout, util.inspect(object, {
275+
customInspect: false,
276+
...this[kGetInspectOptions](this._stdout),
277+
...options
278+
}));
278279
};
279280

280281
Console.prototype.time = function time(label = 'default') {

lib/internal/http2/compat.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ class Http2ServerResponse extends Stream {
512512
}
513513

514514
getHeaders() {
515-
return Object.assign({}, this[kHeaders]);
515+
return { ...this[kHeaders] };
516516
}
517517

518518
hasHeader(name) {

lib/internal/http2/core.js

+10-10
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,7 @@ function pingCallback(cb) {
790790
// 6. enablePush must be a boolean
791791
// All settings are optional and may be left undefined
792792
function validateSettings(settings) {
793-
settings = Object.assign({}, settings);
793+
settings = { ...settings };
794794
assertWithinRange('headerTableSize',
795795
settings.headerTableSize,
796796
0, kMaxInt);
@@ -1443,7 +1443,7 @@ class ClientHttp2Session extends Http2Session {
14431443
assertIsObject(options, 'options');
14441444

14451445
headers = Object.assign(Object.create(null), headers);
1446-
options = Object.assign({}, options);
1446+
options = { ...options };
14471447

14481448
if (headers[HTTP2_HEADER_METHOD] === undefined)
14491449
headers[HTTP2_HEADER_METHOD] = HTTP2_METHOD_GET;
@@ -1848,7 +1848,7 @@ class Http2Stream extends Duplex {
18481848
throw new ERR_HTTP2_INVALID_STREAM();
18491849

18501850
assertIsObject(options, 'options');
1851-
options = Object.assign({}, options);
1851+
options = { ...options };
18521852
validatePriorityOptions(options);
18531853

18541854
const priorityFn = submitPriority.bind(this, options);
@@ -2257,7 +2257,7 @@ class ServerHttp2Stream extends Http2Stream {
22572257
throw new ERR_INVALID_CALLBACK();
22582258

22592259
assertIsObject(options, 'options');
2260-
options = Object.assign({}, options);
2260+
options = { ...options };
22612261
options.endStream = !!options.endStream;
22622262

22632263
assertIsObject(headers, 'headers');
@@ -2322,7 +2322,7 @@ class ServerHttp2Stream extends Http2Stream {
23222322
const state = this[kState];
23232323

23242324
assertIsObject(options, 'options');
2325-
options = Object.assign({}, options);
2325+
options = { ...options };
23262326

23272327
const session = this[kSession];
23282328
debug(`Http2Stream ${this[kID]} [Http2Session ` +
@@ -2378,7 +2378,7 @@ class ServerHttp2Stream extends Http2Stream {
23782378
const session = this[kSession];
23792379

23802380
assertIsObject(options, 'options');
2381-
options = Object.assign({}, options);
2381+
options = { ...options };
23822382

23832383
if (options.offset !== undefined && typeof options.offset !== 'number')
23842384
throw new ERR_INVALID_OPT_VALUE('offset', options.offset);
@@ -2441,7 +2441,7 @@ class ServerHttp2Stream extends Http2Stream {
24412441
throw new ERR_HTTP2_HEADERS_SENT();
24422442

24432443
assertIsObject(options, 'options');
2444-
options = Object.assign({}, options);
2444+
options = { ...options };
24452445

24462446
if (options.offset !== undefined && typeof options.offset !== 'number')
24472447
throw new ERR_INVALID_OPT_VALUE('offset', options.offset);
@@ -2667,10 +2667,10 @@ function connectionListener(socket) {
26672667

26682668
function initializeOptions(options) {
26692669
assertIsObject(options, 'options');
2670-
options = Object.assign({}, options);
2670+
options = { ...options };
26712671
options.allowHalfOpen = true;
26722672
assertIsObject(options.settings, 'options.settings');
2673-
options.settings = Object.assign({}, options.settings);
2673+
options.settings = { ...options.settings };
26742674

26752675
// Used only with allowHTTP1
26762676
options.Http1IncomingMessage = options.Http1IncomingMessage ||
@@ -2775,7 +2775,7 @@ function connect(authority, options, listener) {
27752775
}
27762776

27772777
assertIsObject(options, 'options');
2778-
options = Object.assign({}, options);
2778+
options = { ...options };
27792779

27802780
if (typeof authority === 'string')
27812781
authority = new URL(authority);

lib/repl.js

+2-3
Original file line numberDiff line numberDiff line change
@@ -130,8 +130,7 @@ function hasOwnProperty(obj, prop) {
130130
// and it can be overridden by custom print functions, such as `probe` or
131131
// `eyes.js`.
132132
const writer = exports.writer = (obj) => util.inspect(obj, writer.options);
133-
writer.options =
134-
Object.assign({}, util.inspect.defaultOptions, { showProxy: true });
133+
writer.options = { ...util.inspect.defaultOptions, showProxy: true };
135134

136135
exports._builtinLibs = builtinLibs;
137136

@@ -510,7 +509,7 @@ function REPLServer(prompt,
510509
if (self.useColors && self.writer === writer) {
511510
// Turn on ANSI coloring.
512511
self.writer = (obj) => util.inspect(obj, self.writer.options);
513-
self.writer.options = Object.assign({}, writer.options, { colors: true });
512+
self.writer.options = { ...writer.options, colors: true };
514513
}
515514

516515
function filterInternalStackFrames(structuredStack) {

lib/tls.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -264,9 +264,13 @@ class SecurePair extends EventEmitter {
264264
this.credentials = secureContext;
265265

266266
this.encrypted = socket1;
267-
this.cleartext = new exports.TLSSocket(socket2, Object.assign({
268-
secureContext, isServer, requestCert, rejectUnauthorized
269-
}, options));
267+
this.cleartext = new exports.TLSSocket(socket2, {
268+
secureContext,
269+
isServer,
270+
requestCert,
271+
rejectUnauthorized,
272+
...options
273+
});
270274
this.cleartext.once('secure', () => this.emit('secure'));
271275
}
272276

lib/util.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -123,11 +123,12 @@ function formatWithOptions(inspectOptions, f) {
123123
break;
124124
case 111: // 'o'
125125
{
126-
const opts = Object.assign({}, inspectOptions, {
126+
const opts = {
127127
showHidden: true,
128128
showProxy: true,
129-
depth: 4
130-
});
129+
depth: 4,
130+
...inspectOptions
131+
};
131132
tempStr = inspect(arguments[a++], opts);
132133
break;
133134
}

lib/vm.js

+2-6
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,7 @@ function runInContext(code, contextifiedSandbox, options) {
290290
[kParsingContext]: contextifiedSandbox
291291
};
292292
} else {
293-
options = Object.assign({}, options, {
294-
[kParsingContext]: contextifiedSandbox
295-
});
293+
options = { ...options, [kParsingContext]: contextifiedSandbox };
296294
}
297295
return createScript(code, options)
298296
.runInContext(contextifiedSandbox, options);
@@ -303,9 +301,7 @@ function runInNewContext(code, sandbox, options) {
303301
options = { filename: options };
304302
}
305303
sandbox = createContext(sandbox, getContextOptions(options));
306-
options = Object.assign({}, options, {
307-
[kParsingContext]: sandbox
308-
});
304+
options = { ...options, [kParsingContext]: sandbox };
309305
return createScript(code, options).runInNewContext(sandbox, options);
310306
}
311307

0 commit comments

Comments
 (0)