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
Usage with eslint no-param-reassign #189
Comments
@brummelte I don't think your example is a param reassign and the rule shouldn't trigger on it |
You are correct, I just copied it from your example. But the question stays the same: const nextState = produce(baseState, (draftState) => {
/* eslint-disable no-param-reassign */
draftState.test = "New value";
/* eslint-enable no-param-reassign */
}); |
That isn't a param reassign either, please test before reporting issues
Op vr 14 sep. 2018 20:17 schreef Jan Brummelte <notifications@github.com>:
… You are correct, I just copied it from your example. But the question
stays the same:
const nextState = produce(baseState, (draftState) => {
/* eslint-disable no-param-reassign */
draftState.test = "New value";
/* eslint-enable no-param-reassign */
});
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#189 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABvGhLyxkysvti5JdWlWYMvl6VlEFCjDks5ua_LSgaJpZM4Wppfh>
.
|
It is with the option Which the airbnb preset uses: https://github.com/airbnb/javascript/blob/master/packages/eslint-config-airbnb-base/rules/best-practices.js |
Wow that's an insane rule disabling half the language. But not going to
argue it, that is too tiring. you might try to set option
ignorePropertyModificationsFor: "draft".
Otherwise; drop either that rule or immer.
Op vr 14 sep. 2018 20:27 schreef Jan Brummelte <notifications@github.com>:
… It is with the option { "props": false }.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#189 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABvGhGhUuzs6ChESdpV98O5NiPxjqvZ7ks5ua_T6gaJpZM4Wppfh>
.
|
For future readers: did that option solve the issue?
Op vr 14 sep. 2018 om 21:13 schreef Jan Brummelte <notifications@github.com
…:
Closed #189 <#189>.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#189 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/ABvGhMcz4mI1XoQ3aFsqDJZ66lGU-ys9ks5ua__vgaJpZM4Wppfh>
.
|
I also don't like disabling it within its file or function scope. I think |
The issue was, that that is not the perfect solution. But yes, that setting would work. |
Ok, to summarize:
|
Just to make it quicker for ya'll,
|
Also in case you have multiple producers in the same scope, like this:
This rule would work:
|
For people coming from future google searches, extending on @jaimefps comment above To enable more semantic variable naming as well as avoiding problem in case of multiple producers in the same scope, we can do module.exports = {
extends: [
'airbnb', // this means you have "props" = "true" by default
],
rules: {
'no-param-reassign': ['error', { props: true, ignorePropertyModificationsForRegex: ["^draft"] }],
},
}; This should make it work for |
Another trick that works:
|
So the rule in .eslintrc from you guys helped me to fix this problem in my immer code:
But if I want to reassign the state completely, like in this reducer (working with redux toolkit in this project):
Giving me the infamous linter warning Do you guys know why it works with reassigning nested state but not the complete state or what I can do different? |
@Crismon96 in some JS engines, reassigning to a named argument heavily deoptimizes the function. Always make a new variable name instead. |
That rule triggered for me in immer (that's how I landed on this issue), but also on code like this: export function groupBy(data, fnc) {
return data.reduce((acc, curr) => {
const res = fnc(curr);
if (!acc[res]) acc[res] = [curr];
else acc[res].push(curr);
return acc;
}, {});
} 75:20 error Assignment to property of function parameter 'acc' no-param-reassign So now I can't even use reduce anymore? This rule (with |
I don't want to disable the eslint rule "no-param-reassign" globally, because it prevents errors.
So this seems to be the best way to use immer with that rule enabled:
There is a way to specify "ignorePropertyModificationsFor" for that eslint rule (https://eslint.org/docs/rules/no-param-reassign). So draftState could be ignored. But that would only exclude that name.
Is there a better way without disabling that rule?
The text was updated successfully, but these errors were encountered: