Allow regular expressions in no-param-reassign ignorePropertyModificationsFor option #11621
Labels
archived due to age
This issue has been archived; please open a new issue for any further discussion
auto closed
The bot closed this issue
enhancement
This change enhances an existing feature of ESLint
rule
Relates to ESLint's core rules
triage
An ESLint team member will look at this issue soon
Background
immer
is a widely used library (10k GitHub stars, 1.5m weekly npm downloads) for working with immutable state.immer
works by taking in an object, creating a temporary version of the object wrapped in a proxy, recording mutations to the temporary object, and returning a new object with the mutations applied (leaving the original unmodified). For example:Since
immer
operates by mutating a "draft" version of the object passed in as a parameter, it falls afoul ofno-param-reassign
withprops: true
. The author of the library recommends settingignorePropertyModificationsFor: "draft"
(and always usingdraft
as the parameter name) or disabling theprops
option altogether.Proposal
In the codebase I work on, I'd like the option to use a more descriptive variable name than
draft
. The convention I've used is to always prefix the name of the parameter withdraft
. For example,produce(x, (draftX) => { draftX.y = 'z'; })
. In general, I'd like to forbid assignment to properties of parameters, except when the parameter name matches/^draft/
. However,ignorePropertyModificationsFor
only accepts an array of string literals.Many other rules accept arrays of regex patterns, and it seems that regular expressions were considered in the proposal for
ignorePropertyModificationsFor
but were not implemented because the originally proposed options schema was deemed to be too complex.I would like to propose one of two changes:
Option 1: Change
ignorePropertyModificationsFor
to take an array of regular expressions instead of an array of strings. This would be a breaking change.Option 2: Add a new option (
ignorePropertyModificationsForPatterns
? or something less unwieldy if anyone has a better idea) that takes an array of regular expressions and optionally deprecateignorePropertyModificationsFor
in the next major release.What rule do you want to change?
no-param-reassign
Does this change cause the rule to produce more or fewer warnings?
Fewer, assuming the same option name is reused, since a literal string would be interpreted as a regex and thus matched against substrings of parameter names, so potentially more parameters would have their property modifications ignored.
How will the change be implemented? (New option, new default behavior, etc.)?
Either a change to the behaviour of
ignorePropertyModificationsFor
(breaking) or a new option (non-breaking) and potential deprecation ofignorePropertyModificationsFor
.Please provide some example code that this change will affect:
What does the rule currently do for this code?
Error:
Assignment to property of function parameter 'draftFoo'
What will the rule do after it's changed?
No error because
draftFoo
matches regex/^draft/
.Are you willing to submit a pull request to implement this change?
Yes.
The text was updated successfully, but these errors were encountered: