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.

Ban Object.assign mutation

See original GitHub issue

Could 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:closed
  • Created 5 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
RebeccaStevenscommented, Jan 25, 2019

I feel this should be added to the no-object-mutation rule.

1reaction
jonaskellocommented, Jan 25, 2019

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}).

Read more comments on GitHub >

github_iconTop 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 >

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