New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Object.assign instead of manipulating input objects #98
Comments
Slightly relevant issue: https://github.com/sindresorhus/eslint-config-xo/issues/18 |
Seems like it should be possible to detect the arguments and to find the cases where something is assigning to properties of those arguments. Could maybe use code path analysis. Should use object spread and not |
Seems like the better fix is to make sure that objects in parameters are treated as immutable. The implementation for immutable/no-mutation might work as a starting point. // objects
function foo(bar) {
delete bar.zaz; // fail
bar.zaz = bar.zaz || 'zaz'; // fail
bar = {
zaz: 'zaz',
...bar
}; // pass
}
// arrays
function foo(bar) {
bar.push('zaz'); // fail
bar.pop() // fail
bar.unshift('zaz'); // fail
bar.shift(); // fail
bar.length = 0; // fail
delete bar[0]; // fail
bar = {
...bar,
'zaz'
}; // pass
} I'd argue that overshadowing parameter is also bad but should be a different rule. |
Turns out what I was mentioning is already a rule: https://eslint.org/docs/rules/no-param-reassign There is also a functional difference between |
I've been using |
It's not really a problem. It's just a slight functional difference. In my opinion, I think the In any case, I was just pondering whether I wanted to help out with this feature -- but I'd rather have immutable parameters and/or use destructuring directly in the function signature. I'll go dig up something else to poke at. |
I myself did it wrong in the early days, and I definitely come across this issue quite a lot.
While it should've been
Not sure how easy it would be to detect this though.
The text was updated successfully, but these errors were encountered: