Skip to content

Should we allow mutating function parameters? #53

@aljones15

Description

@aljones15

Eslint contains a rule that throws if you try to mutate function parameters:

https://eslint.org/docs/rules/no-param-reassign

Disallow Reassignment of Function Parameters (no-param-reassign)
Assignment to variables declared as function parameters can be misleading and lead to confusing behavior, as modifying function parameters will also mutate the arguments object. Often, assignment to function parameters is unintended and indicative of a mistake or programmer error.

This rule can be also configured to fail when function parameters are modified. Side effects on parameters can cause counter-intuitive execution flow and make errors difficult to track down.

The major reason for this rule is here:

https://spin.atomicobject.com/2011/04/10/javascript-don-t-reassign-your-function-arguments/

The rule also contains the option {props: true} if {props: true} then the re-assignment also applies to properties of parameters. This is relevant to us as we often use an object called options as the parameter to a function and then destructure and use those properties. Setting {props: true} means than this will cause a linting error:

// note: this is not a good example, but it does
// get across the point
const foo = ({bar, milo}) => {
  if(Array.isArray(milo)) {
    // this will cause an eslint error if `{props: true}`
    milo = milo.join(',')
  }
  return `${bar}${milo}`;
}

Thumbs up if you support adding this rule with {props: truie} to the rule set.
Thumbs down if not, leave a comment if you want something else.

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions