You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/rules/prefer-destructuring.md
+75-93Lines changed: 75 additions & 93 deletions
Original file line number
Diff line number
Diff line change
@@ -14,22 +14,30 @@ With JavaScript ES6, a new syntax was added for creating variables from an array
14
14
15
15
### Options
16
16
17
-
This rule takes two sets of configuration objects. The first object parameter determines what types of destructuring the rule applies to.
17
+
This rule takes two arguments, both of which are objects. The first object parameter determines what types of destructuring the rule applies to.
18
18
19
-
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.
19
+
In the first object, there are two properties, `array` and `object`, that can be used to turn on or off the destructuring requirement for each of those types independently. By default, both are true.
20
20
21
-
Alternatively, you can use separate configurations for different assignment types. It accepts 2 other keys instead of `array` and `object`.
22
-
23
-
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`.
24
-
25
-
The rule has a second object with a single key, `enforceForRenamedProperties`, which determines whether the `object` destructuring applies to renamed variables.
26
-
27
-
**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:
21
+
```json
22
+
{
23
+
"rules": {
24
+
"prefer-destructuring": ["error", {
25
+
"array": true,
26
+
"object": true
27
+
}]
28
+
}
29
+
}
30
+
```
28
31
29
-
* Accessing an object property whose key is an integer will fall under the category `array` destructuring.
30
-
* Accessing an array element through a computed index will fall under the category `object` destructuring.
32
+
For example, the following configuration enforces only object destructuring, but not array destructuring:
31
33
32
-
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 when `enforceForRenamedProperties` is enabled:
84
+
Alternatively, you can use separate configurations for different assignment types. The first argument accepts two other keys instead of `array` and `object`.
77
85
78
-
::: incorrect
86
+
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 is an object containing 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`.
An example configuration, with the defaults `array` and `object` filled in, looks like this:
127
+
#### enforceForRenamedProperties
128
+
129
+
The rule has a second object argument with a single key, `enforceForRenamedProperties`, which determines whether the `object` destructuring applies to renamed variables.
115
130
116
131
```json
117
132
{
118
133
"rules": {
119
-
"prefer-destructuring": ["error", {
120
-
"array": true,
134
+
"prefer-destructuring": ["error",
135
+
{
121
136
"object": true
122
-
}, {
123
-
"enforceForRenamedProperties": false
137
+
},
138
+
{
139
+
"enforceForRenamedProperties": true
124
140
}]
125
141
}
126
142
}
127
143
```
128
144
129
-
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.
130
-
131
-
For example, the following configuration enforces only object destructuring, but not array destructuring:
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.
163
-
164
-
For example, the following configuration enforces object destructuring in variable declarations and enforces array destructuring in assignment expressions.
165
-
166
-
```json
167
-
{
168
-
"rules": {
169
-
"prefer-destructuring": ["error", {
170
-
"VariableDeclarator": {
171
-
"array": false,
172
-
"object": true
173
-
},
174
-
"AssignmentExpression": {
175
-
"array": true,
176
-
"object": false
177
-
}
178
-
}, {
179
-
"enforceForRenamedProperties": false
180
-
}]
181
-
}
182
-
}
183
-
184
-
```
154
+
:::
185
155
186
-
Examples of **correct** code when object destructuring in `VariableDeclarator` is enforced:
156
+
Examples of **correct** code when `enforceForRenamedProperties` is enabled:
constbar=this.#x; // private identifiers are not allowed in destructuring
177
+
}
178
+
}
204
179
```
205
180
206
181
:::
207
182
183
+
**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:
184
+
185
+
* Accessing an object property whose key is an integer will fall under the category `array` destructuring.
186
+
* Accessing an array element through a computed index will fall under the category `object` destructuring.
187
+
188
+
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.
189
+
208
190
## When Not To Use It
209
191
210
192
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.
0 commit comments