Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: switch to object spread where possible #25104

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/.eslintrc.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
rules:
prefer-object-spread: error
no-restricted-syntax:
# Config copied from .eslintrc.js
- error
Expand Down
2 changes: 1 addition & 1 deletion lib/_tls_wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ function initRead(tls, wrapped) {
*/

function TLSSocket(socket, opts) {
const tlsOptions = Object.assign({}, opts);
const tlsOptions = { ...opts };

if (tlsOptions.ALPNProtocols)
tls.convertALPNProtocols(tlsOptions.ALPNProtocols, tlsOptions);
Expand Down
4 changes: 2 additions & 2 deletions lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ function normalizeExecArgs(command, options, callback) {
}

// Make a shallow copy so we don't clobber the user's options object.
options = Object.assign({}, options);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i realize this isn't directly related but it would be good to avoid assigning to the argument.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do that pretty often everywhere throughout the code?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could be a separate pr i guess... i just figured as long as you're modifying a bunch of these you could change the argument name. non blocking.

options = { ...options };
options.shell = typeof options.shell === 'string' ? options.shell : true;

return {
Expand Down Expand Up @@ -470,7 +470,7 @@ function normalizeSpawnArguments(file, args, options) {
}

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

if (options.shell) {
const command = [file].concat(args).join(' ');
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ function setupChannel(target, channel) {
throw new ERR_INVALID_ARG_TYPE('options', 'Object', options);
}

options = Object.assign({ swallowErrors: false }, options);
options = { swallowErrors: false, ...options };

if (this.connected) {
return this._send(message, handle, options, callback);
Expand Down
9 changes: 5 additions & 4 deletions lib/internal/console/constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,11 @@ Console.prototype.warn = function warn(...args) {
Console.prototype.error = Console.prototype.warn;

Console.prototype.dir = function dir(object, options) {
options = Object.assign({
customInspect: false
}, this[kGetInspectOptions](this._stdout), options);
this[kWriteToConsole](kUseStdout, util.inspect(object, options));
this[kWriteToConsole](kUseStdout, util.inspect(object, {
customInspect: false,
...this[kGetInspectOptions](this._stdout),
...options
}));
};

Console.prototype.time = function time(label = 'default') {
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/http2/compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ class Http2ServerResponse extends Stream {
}

getHeaders() {
return Object.assign({}, this[kHeaders]);
return { ...this[kHeaders] };
}

hasHeader(name) {
Expand Down
20 changes: 10 additions & 10 deletions lib/internal/http2/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,7 @@ function pingCallback(cb) {
// 6. enablePush must be a boolean
// All settings are optional and may be left undefined
function validateSettings(settings) {
settings = Object.assign({}, settings);
settings = { ...settings };
assertWithinRange('headerTableSize',
settings.headerTableSize,
0, kMaxInt);
Expand Down Expand Up @@ -1443,7 +1443,7 @@ class ClientHttp2Session extends Http2Session {
assertIsObject(options, 'options');

headers = Object.assign(Object.create(null), headers);
options = Object.assign({}, options);
options = { ...options };

if (headers[HTTP2_HEADER_METHOD] === undefined)
headers[HTTP2_HEADER_METHOD] = HTTP2_METHOD_GET;
Expand Down Expand Up @@ -1848,7 +1848,7 @@ class Http2Stream extends Duplex {
throw new ERR_HTTP2_INVALID_STREAM();

assertIsObject(options, 'options');
options = Object.assign({}, options);
options = { ...options };
validatePriorityOptions(options);

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

assertIsObject(options, 'options');
options = Object.assign({}, options);
options = { ...options };
options.endStream = !!options.endStream;

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

assertIsObject(options, 'options');
options = Object.assign({}, options);
options = { ...options };

const session = this[kSession];
debug(`Http2Stream ${this[kID]} [Http2Session ` +
Expand Down Expand Up @@ -2378,7 +2378,7 @@ class ServerHttp2Stream extends Http2Stream {
const session = this[kSession];

assertIsObject(options, 'options');
options = Object.assign({}, options);
options = { ...options };

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

assertIsObject(options, 'options');
options = Object.assign({}, options);
options = { ...options };

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

function initializeOptions(options) {
assertIsObject(options, 'options');
options = Object.assign({}, options);
options = { ...options };
options.allowHalfOpen = true;
assertIsObject(options.settings, 'options.settings');
options.settings = Object.assign({}, options.settings);
options.settings = { ...options.settings };

// Used only with allowHTTP1
options.Http1IncomingMessage = options.Http1IncomingMessage ||
Expand Down Expand Up @@ -2775,7 +2775,7 @@ function connect(authority, options, listener) {
}

assertIsObject(options, 'options');
options = Object.assign({}, options);
options = { ...options };

if (typeof authority === 'string')
authority = new URL(authority);
Expand Down
5 changes: 2 additions & 3 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,7 @@ function hasOwnProperty(obj, prop) {
// and it can be overridden by custom print functions, such as `probe` or
// `eyes.js`.
const writer = exports.writer = (obj) => util.inspect(obj, writer.options);
writer.options =
Object.assign({}, util.inspect.defaultOptions, { showProxy: true });
writer.options = { ...util.inspect.defaultOptions, showProxy: true };

exports._builtinLibs = builtinLibs;

Expand Down Expand Up @@ -509,7 +508,7 @@ function REPLServer(prompt,
if (self.useColors && self.writer === writer) {
// Turn on ANSI coloring.
self.writer = (obj) => util.inspect(obj, self.writer.options);
self.writer.options = Object.assign({}, writer.options, { colors: true });
self.writer.options = { ...writer.options, colors: true };
}

function filterInternalStackFrames(structuredStack) {
Expand Down
10 changes: 7 additions & 3 deletions lib/tls.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,13 @@ class SecurePair extends EventEmitter {
this.credentials = secureContext;

this.encrypted = socket1;
this.cleartext = new exports.TLSSocket(socket2, Object.assign({
secureContext, isServer, requestCert, rejectUnauthorized
}, options));
this.cleartext = new exports.TLSSocket(socket2, {
secureContext,
isServer,
requestCert,
rejectUnauthorized,
...options
});
this.cleartext.once('secure', () => this.emit('secure'));
}

Expand Down
8 changes: 2 additions & 6 deletions lib/vm.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,9 +292,7 @@ function runInContext(code, contextifiedSandbox, options) {
[kParsingContext]: contextifiedSandbox
};
} else {
options = Object.assign({}, options, {
[kParsingContext]: contextifiedSandbox
});
options = { ...options, [kParsingContext]: contextifiedSandbox };
}
return createScript(code, options)
.runInContext(contextifiedSandbox, options);
Expand All @@ -305,9 +303,7 @@ function runInNewContext(code, sandbox, options) {
options = { filename: options };
}
sandbox = createContext(sandbox, getContextOptions(options));
options = Object.assign({}, options, {
[kParsingContext]: sandbox
});
options = { ...options, [kParsingContext]: sandbox };
return createScript(code, options).runInNewContext(sandbox, options);
}

Expand Down