Skip to content

Rule request: prefer-object-spread #7230

@ljharb

Description

@ljharb

Please describe what the rule should do:
When it finds an Object.assign call where the first argument is an object literal, the rule should error, and recommend using the object spread operator instead.

What category of rule is this? (place an "X" next to just one item)

[ ] Enforces code style
[ ] Warns about a potential error
[x] Suggests an alternate way of doing something
[ ] Other (please specify:)

Provide 2-3 code examples that this rule will warn about:

return Object.assign({}, foo, { bar: baz }, quux); // should be `return { ...foo, bar: baz, ...quux };`
return Object.assign({ foo }, bar, { baz }); // should be `return { foo, ...bar, baz };`

return Object.assign(foo, bar, { baz }); // should not warn

Why should this rule be included in ESLint (instead of a plugin)?
Just like http://eslint.org/docs/rules/prefer-spread, this recommends that users use a syntax feature (an operator) instead of API (Object.assign) for the cases where it makes sense. Both are core language features, and eslint core should be where core language feature rules live.

Metadata

Metadata

Assignees

Labels

acceptedThere is consensus among the team that this change meets the criteria for inclusionarchived due to ageThis issue has been archived; please open a new issue for any further discussionfeatureThis change adds a new feature to ESLintruleRelates to ESLint's core rules

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions