question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rule request: `prefer-object-spread`

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:12
  • Comments:32 (31 by maintainers)

github_iconTop GitHub Comments

3reactions
ljharbcommented, Feb 5, 2018

@mysticatea we already have an implementation of this rule internally at @airbnb, mind if we submit a PR?

3reactions
mysticateacommented, Feb 5, 2018

I’ll champion this proposal, new prefer-object-spread rule. The advisory rule for new syntax would be helpful. TSLint looks to have the rule.

Read more comments on GitHub >

github_iconTop Results From Across the Web

prefer-object-spread - ESLint - Pluggable JavaScript Linter
A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.
Read more >
prefer-object-spread - Rule
Rule : prefer-object-spread. Enforces the use of the ES2018 object spread operator over Object.assign() where appropriate.
Read more >
eslint-plugin-prefer-object-spread - npm
This rule suggests that object spread properties be used instead of Object.assign() . The rule is only applied when Object.assign() is used for ......
Read more >
Disable the prefer-object-spread rule for airbnb-base/legacy?
Hello,. We are in the process of revamping a large volume of code using ESLint + airbnb. We are using the airbnbn-base/legacy version, ......
Read more >
Prefer-object-spread - ESLint - W3cubDocs
When Object.assign is called using an object literal as the first argument, this rule requires using the object spread syntax instead. This rule...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found