prefer-object-spread

Require object spread instead of Object.assign() into a new object

fixable

Object.assign({}, defaults, options) builds a new object, but you have to notice the {} to know that nothing is changed in place. { ...defaults, ...options } is shorter and can only make a new object.

A call is only reported if its first argument is an object literal. Object.assign(target, options) changes target, which spread can't do. A call is also left alone if it spreads its arguments, or if one of its object literals has a getter or a setter.

--fix rewrites the call as an object literal.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"prefer-object-spread" = "error" # or "warn"

Examples#

example.js3 problems
const settings = Object.assign({}, defaults, options);
const admin = Object.assign({ role: "admin" }, user);
const point = Object.assign({ x: 0, y: 0 });
example.jsNo problems
const settings = { ...defaults, ...options };
const admin = { role: "admin", ...user };
const point = { x: 0, y: 0 };

Object.assign(target, options);
const merged = Object.assign({}, ...sources);
const list = Object.assign({ get size() { return items.length; } }, base);