@@ -17,8 +17,14 @@ Rules for Array functions and methods.
17
17
- [ Examples] ( #examples-2 )
18
18
- [ ` avoid-reverse ` ] ( #avoid-reverse )
19
19
- [ Examples] ( #examples-3 )
20
- - [ ` array-func/recommended ` Configuration] ( #array-funcrecommended-configuration )
21
- - [ Using the Configuration] ( #using-the-configuration )
20
+ - [ ` prefer-flat-map ` ] ( #prefer-flat-map )
21
+ - [ Examples] ( #examples-4 )
22
+ - [ ` prefer-flat ` ] ( #prefer-flat )
23
+ - [ Examples] ( #examples-55 )
24
+ - [ Configurations] ( #configurations )
25
+ - [ ` array-func/recommended ` Configuration] ( #array-funcrecommended-configuration )
26
+ - [ Using the Configuration] ( #using-the-configuration )
27
+ - [ ` array-func/all ` Configuration] ( #array-funcall-configuration )
22
28
- [ License] ( #license )
23
29
24
30
## Installation
@@ -189,7 +195,60 @@ const reverseArray = array.reverse();
189
195
const reverseMap = array .reverse ().map ((r ) => r + 1 );
190
196
```
191
197
192
- ## ` array-func/recommended ` Configuration
198
+ ### ` prefer-flat-map `
199
+ Use ` .flatMap() ` to flatten an array and map the values instead of using
200
+ ` .flat().map() ` .
201
+
202
+ This rule is auto fixable.
203
+
204
+ #### Examples
205
+ Code that triggers this rule:
206
+ ``` js
207
+ const flattenedAndMapped = array .flat ().map ((p ) => p);
208
+ ```
209
+
210
+ Code that doesn't trigger this rule:
211
+ ``` js
212
+ const oneAction = array .flatMap ((m ) => m);
213
+
214
+ const flattened = array .flat ();
215
+
216
+ const mapped = array .map ((r ) => r + 1 );
217
+
218
+ const mappedThenFlattened = array .map ((r ) => r + 1 ).flat ();
219
+
220
+ const flatMappedWithExtra = array .flat ().reverse ().map ((r ) => r + 1 );
221
+ ```
222
+
223
+ ### ` prefer-flat `
224
+ Use ` .flat() ` to flatten an array of arrays. This rule currently recognizes two
225
+ patterns and can replace them with a ` .flat() ` call:
226
+
227
+ - ` [].concat(...array) `
228
+ - ` array.reduce((p, n) => p.concat(n), []) `
229
+
230
+ This rule is auto fixable.
231
+
232
+ #### Examples
233
+ Code that triggers this rule:
234
+ ``` js
235
+ const concatFlat = [].concat (... array);
236
+
237
+ const reduceFlat = array .reduce ((p , n ) => p .concat (n), []);
238
+ ```
239
+
240
+ Code that doesn't trigger this rule:
241
+ ``` js
242
+ const flattened = array .flat ();
243
+
244
+ const reverseFlat = array .reduce ((p , n ) => n .concat (p), []);
245
+
246
+ const otherReduce = array .reduce ((p , n ) => n + p, 0 );
247
+ ```
248
+
249
+ ## Configurations
250
+
251
+ ### ` array-func/recommended ` Configuration
193
252
The recommended configuration will set your parser ECMA Version to 2015, since that's when the Array functions and methods were added.
194
253
195
254
Rule | Error level | Fixable
@@ -199,7 +258,7 @@ Rule | Error level | Fixable
199
258
` prefer-array-from ` | Error | Yes
200
259
` avoid-reverse ` | Error | Yes
201
260
202
- ### Using the Configuration
261
+ #### Using the Configuration
203
262
To enable this configuration use the ` extends ` property in your ` .eslintrc.json ` config file (may look different for other config file styles):
204
263
``` json
205
264
{
@@ -209,5 +268,19 @@ To enable this configuration use the `extends` property in your `.eslintrc.json`
209
268
}
210
269
```
211
270
271
+ ### ` array-func/all ` Configuration
272
+ The recommended configuration does not include all rules, since some Array methods
273
+ were added after ES2015. The all configuration enables all rules the plugin
274
+ contains and sets the ECMA version appropriately.
275
+
276
+ Rule | Error level | Fixable
277
+ ---- | ----------- | -------
278
+ ` from-map ` | Error | Yes
279
+ ` no-unnecessary-this-arg ` | Error | Sometimes
280
+ ` prefer-array-from ` | Error | Yes
281
+ ` avoid-reverse ` | Error | Yes
282
+ ` prefer-flat-map ` | Error | Yes
283
+ ` prefer-flat ` | Error | Yes
284
+
212
285
## License
213
286
The ` array-func ` plugin is licensed under the [ MIT License] ( LICENSE ) .
0 commit comments