Ban Object.assign mutation
See original GitHub issueCould this library have an option to ban Object.assign
?
It’d probably need to only apply to non-empty objects. i.e. this should be banned because it mutates a
:
const a = { message: 'hello' }
const b = Object.assign(a, { message: 'bye' })
a.message // 'bye'
b.message // 'bye'
But this could be considered ok:
const a = { message: 'hello' }
const b = Object.assign({}, a, { message: 'bye' })
a.message // 'hello'
b.message // 'bye'
Issue Analytics
- State:
- Created 5 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Changes to object made with Object.assign mutates source ...
Object.assign will copy properties to another object. Some of those properties are copied "by value" and others are copied "by reference".
Read more >Redux: Avoiding Object Mutations with Object.assign() and ...
Learn how to use Object.assign() and the spread operator proposed for ES7 to avoid mutating objects.
Read more >Airbnb JavaScript Style Guide()
3.8 Prefer the object spread operator over Object.assign to shallow-copy objects ... Mutation should be avoided in general, but in particular when exporting ......
Read more >Object.assign() - JavaScript - MDN Web Docs
The Object.assign() method copies all enumerable own properties from one or more source objects to a target object.
Read more >How to Avoid Object Mutation in JavaScript | by Jake Mills
To avoid the mutation of our object assigned to the character variable, and using the Object.assign() method, we do the following:
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I feel this should be added to the
no-object-mutation
rule.I think this sound like a good idea. The case of assigning to the empty object might be OK but perhaps we should also have the option to ban
Object.assign
altogether for cases where you want to promote object spread instead of empty object assign (const c = {...a, ...b}
).