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 when not in strict mode (see When Not To Use It below). 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.
Rule Details
This rule aims to prevent unintended behavior caused by modification or reassignment of function parameters.
Examples of incorrect code for this rule:
::: incorrect
/*eslint no-param-reassign: "error"*/
functionfoo(bar){
bar =13;
}
functionfoo(bar){
bar++;
}
functionfoo(bar){
for(bar in baz){}
}
functionfoo(bar){
for(bar of baz){}
}
:::
Examples of correct code for this rule:
::: correct
/*eslint no-param-reassign: "error"*/
functionfoo(bar){
var baz = bar;
}
:::
Options
This rule takes one option, an object, with a boolean property "props", and arrays "ignorePropertyModificationsFor" and "ignorePropertyModificationsForRegex". "props" is false by default. If "props" is set to true, this rule warns against the modification of parameter properties unless they're included in "ignorePropertyModificationsFor" or "ignorePropertyModificationsForRegex", which is an empty array by default.
props
Examples of correct code for the default { "props": false } option:
If you want to allow assignment to function parameters, then you can safely disable this rule.
Strict mode code doesn't sync indices of the arguments object with each parameter binding. Therefore, this rule is not necessary to protect against arguments object mutation in ESM modules or other strict mode functions.
Source: http://eslint.org/docs/rules/
With JavaScript ES6, a new syntax was added for creating variables from an array index or object property, called destructuring. This rule enforces usage of destructuring instead of accessing a property through a member expression.
Rule Details
Options
This rule takes two sets of configuration objects. The first object parameter determines what types of destructuring the rule applies to.
The two properties, array and object, can be used to turn on or off the destructuring requirement for each of those types independently. By default, both are true.
Alternatively, you can use separate configurations for different assignment types. It accepts 2 other keys instead of array and object.
One key is VariableDeclarator and the other is AssignmentExpression, which can be used to control the destructuring requirement for each of those types independently. Each property accepts an object that accepts two properties, array and object, which can be used to control the destructuring requirement for each of array and object independently for variable declarations and assignment expressions. By default, array and object are set to true for both VariableDeclarator and AssignmentExpression.
The rule has a second object with a single key, enforceForRenamedProperties, which determines whether the object destructuring applies to renamed variables.
Note: It is not possible to determine if a variable will be referring to an object or an array at runtime. This rule therefore guesses the assignment type by checking whether the key being accessed is an integer. This can lead to the following possibly confusing situations:
Accessing an object property whose key is an integer will fall under the category array destructuring.
Accessing an array element through a computed index will fall under the category object destructuring.
The --fix option on the command line fixes only problems reported in variable declarations, and among them only those that fall under the category object destructuring. Furthermore, the name of the declared variable has to be the same as the name used for non-computed member access in the initializer. For example, var foo = object.foo can be automatically fixed by this rule. Problems that involve computed member access (e.g., var foo = object[foo]) or renamed properties (e.g., var foo = object.bar) are not automatically fixed.
Examples of incorrect code for this rule:
::: incorrect
// With `array` enabled
var foo = array[0];
bar.baz = array[0];
// With `object` enabled
var foo = object.foo;
var foo = object['foo'];
:::
Examples of correct code for this rule:
::: correct
// With `array` enabled
var[ foo ]= array;
var foo = array[someIndex];
[bar.baz]= array;
// With `object` enabled
var{ foo }= object;
var foo = object.bar;
let foo;
({ foo }= object);
:::
Examples of incorrect code when enforceForRenamedProperties is enabled:
::: incorrect
var foo = object.bar;
:::
Examples of correct code when enforceForRenamedProperties is enabled:
::: correct
var{bar: foo }= object;
:::
Examples of additional correct code when enforceForRenamedProperties is enabled:
::: correct
classC{
#x;
foo(){
const bar =this.#x;// private identifiers are not allowed in destructuring
}
}
:::
An example configuration, with the defaults array and object filled in, looks like this:
{
"rules": {
"prefer-destructuring": ["error", {
"array": true,
"object": true
}, {
"enforceForRenamedProperties": false
}]
}
}
The two properties, array and object, which can be used to turn on or off the destructuring requirement for each of those types independently. By default, both are true.
For example, the following configuration enforces only object destructuring, but not array destructuring:
An example configuration, with the defaults VariableDeclarator and AssignmentExpression filled in, looks like this:
{
"rules": {
"prefer-destructuring": ["error", {
"VariableDeclarator": {
"array": false,
"object": true
},
"AssignmentExpression": {
"array": true,
"object": true
}
}, {
"enforceForRenamedProperties": false
}]
}
}
The two properties, VariableDeclarator and AssignmentExpression, which can be used to turn on or off the destructuring requirement for array and object. By default, all values are true.
For example, the following configuration enforces object destructuring in variable declarations and enforces array destructuring in assignment expressions.
{
"rules": {
"prefer-destructuring": ["error", {
"VariableDeclarator": {
"array": false,
"object": true
},
"AssignmentExpression": {
"array": true,
"object": false
}
}, {
"enforceForRenamedProperties": false
}]
}
}
Examples of correct code when object destructuring in VariableDeclarator is enforced:
If you want to be able to access array indices or object properties directly, you can either configure the rule to your tastes or disable the rule entirely.
Additionally, if you intend to access large array indices directly, like:
var foo = array[100];
Then the array part of this rule is not recommended, as destructuring does not match this use case very well.