5
5
Rules for Array functions and methods.
6
6
7
7
## Contents
8
+
8
9
- [ Installation] ( #installation )
9
10
- [ Rules] ( #rules )
10
11
- [ ` from-map ` ] ( #from-map )
11
12
- [ Examples] ( #examples )
13
+ - [ Using the rule] ( #using-the-rule )
12
14
- [ ` no-unnecessary-this-arg ` ] ( #no-unnecessary-this-arg )
13
15
- [ Checked Functions] ( #checked-functions )
14
16
- [ Checked Methods] ( #checked-methods )
15
17
- [ Examples] ( #examples-1 )
18
+ - [ Using the rule] ( #using-the-rule-1 )
16
19
- [ ` prefer-array-from ` ] ( #prefer-array-from )
17
20
- [ Examples] ( #examples-2 )
21
+ - [ Using the rule] ( #using-the-rule-2 )
18
22
- [ ` avoid-reverse ` ] ( #avoid-reverse )
19
23
- [ Examples] ( #examples-3 )
24
+ - [ Using the rule] ( #using-the-rule-3 )
20
25
- [ ` prefer-flat-map ` ] ( #prefer-flat-map )
21
26
- [ Examples] ( #examples-4 )
27
+ - [ Using the rule] ( #using-the-rule-4 )
22
28
- [ ` prefer-flat ` ] ( #prefer-flat )
23
29
- [ Examples] ( #examples-5 )
30
+ - [ Using the rule] ( #using-the-rule-5 )
24
31
- [ Configurations] ( #configurations )
25
32
- [ ` array-func/recommended ` Configuration] ( #array-funcrecommended-configuration )
26
33
- [ Using the Configuration] ( #using-the-configuration )
27
34
- [ ` array-func/all ` Configuration] ( #array-funcall-configuration )
35
+ - [ Using the Configuration] ( #using-the-configuration-1 )
28
36
- [ License] ( #license )
29
37
30
38
## Installation
@@ -44,21 +52,25 @@ $ npm install -D eslint-plugin-array-func
44
52
## Rules
45
53
46
54
### ` from-map `
55
+
47
56
Prefer using the ` mapFn ` callback of ` Array.from ` over an immediate ` .map() ` call on the ` Array.from ` result.
48
57
49
58
` Array.from ` has a ` mapFn ` callback that lets you map the items of the iterable to an array like you would with ` .map() ` except that values have not yet been truncated to fit types allowed in an array. Some iterables can't be directly converted to an array and thus have to be iterated either way. In that case using the mapping callback of ` Array.from ` avoids an iteration. See also [ MDN] ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from#Description ) for an explanation of the potential benefits of using the mapping callback of ` Array.from ` directly.
50
59
51
60
This rule is auto fixable. It will produce nested function calls if you use the ` Array.from ` map callback and have a ` .map() ` call following it.
52
61
53
62
#### Examples
63
+
54
64
Code that triggers this rule:
65
+
55
66
``` js
56
67
Array .from (iterable).map ((t ) => t .id );
57
68
58
69
Array .from (iterable, (t ) => t .id ).map ((id ) => id[0 ]);
59
70
```
60
71
61
72
Code that doesn't trigger this rule:
73
+
62
74
``` js
63
75
Array .from (iterable, (t ) => t .id );
64
76
@@ -68,17 +80,37 @@ const arr = Array.from(iterable);
68
80
const mappedArray = arr .map ((t ) => t .id );
69
81
```
70
82
83
+ #### Using the rule
84
+
85
+ To use this rule, your ` .eslintrc.json ` should at least contain the following (may look different for other config file styles):
86
+
87
+ ``` json
88
+ {
89
+ "plugin" : [
90
+ " array-func"
91
+ ],
92
+ "rules" : {
93
+ "array-func/array-from" : " error"
94
+ }
95
+ }
96
+ ```
97
+
98
+ Alternatively you can use a [ configuration] ( #configurations ) included with this plugin.
99
+
71
100
### ` no-unnecessary-this-arg `
101
+
72
102
Avoid the ` this ` parameter when providing arrow function as callback in array functions.
73
103
74
104
The ` this ` parameter is useless when providing arrow functions, since the ` this ` of arrow functions can not be rebound, thus the parameter has no effect.
75
105
76
106
The fix is usually to omit the parameter. The Array methods can't be auto-fixed, since the detection of array methods is not confident enough to know that the method is being called on an array.
77
107
78
108
#### Checked Functions
109
+
79
110
- ` from ` (fixable)
80
111
81
112
#### Checked Methods
113
+
82
114
- ` every `
83
115
- ` filter `
84
116
- ` find `
@@ -88,7 +120,9 @@ The fix is usually to omit the parameter. The Array methods can't be auto-fixed,
88
120
- ` some `
89
121
90
122
#### Examples
123
+
91
124
Code that triggers this rule:
125
+
92
126
``` js
93
127
const array = Array .from (" example" , (char ) => char .charCodeAt (0 ), this );
94
128
@@ -108,6 +142,7 @@ array.forEach((char) => console.log(char), this);
108
142
```
109
143
110
144
Code that doesn't trigger this rule:
145
+
111
146
``` js
112
147
const array = Array .from (" example" , (char ) => char .charCodeAt (0 ));
113
148
const alternateArray = Array .from (" example" , function (char ) {
@@ -135,21 +170,42 @@ array.forEach(function(char) {
135
170
array .filter (this .isGood , this );
136
171
```
137
172
173
+ #### Using the rule
174
+
175
+ To use this rule, your ` .eslintrc.json ` should at least contain the following (may look different for other config file styles):
176
+
177
+ ``` json
178
+ {
179
+ "plugin" : [
180
+ " array-func"
181
+ ],
182
+ "rules" : {
183
+ "array-func/no-unnecessary-this-arg" : " error"
184
+ }
185
+ }
186
+ ```
187
+
188
+ Alternatively you can use a [ configuration] ( #configurations ) included with this plugin.
189
+
138
190
### ` prefer-array-from `
191
+
139
192
Use ` Array.from ` instead of ` [...iterable] ` .
140
193
See [ ` from-map ` ] ( #from-map ) for additional benefits ` Array.from ` can provide over the spread syntax.
141
194
142
195
This rule is auto fixable.
143
196
144
197
#### Examples
198
+
145
199
Code that triggers this rule:
200
+
146
201
``` js
147
202
const iterable = [... " string" ];
148
203
149
204
const arrayCopy = [... iterable];
150
205
```
151
206
152
207
Code that doesn't trigger this rule:
208
+
153
209
``` js
154
210
const array = [1 , 2 , 3 ];
155
211
@@ -160,7 +216,25 @@ const arrayCopy = Array.from(array);
160
216
const characterArray = Array .from (" string" );
161
217
```
162
218
219
+ #### Using the rule
220
+
221
+ To use this rule, your ` .eslintrc.json ` should at least contain the following (may look different for other config file styles):
222
+
223
+ ``` json
224
+ {
225
+ "plugin" : [
226
+ " array-func"
227
+ ],
228
+ "rules" : {
229
+ "array-func/prefer-array-from" : " error"
230
+ }
231
+ }
232
+ ```
233
+
234
+ Alternatively you can use a [ configuration] ( #configurations ) included with this plugin.
235
+
163
236
### ` avoid-reverse `
237
+
164
238
Avoid reversing the array and running a method on it if there is an equivalent
165
239
of the method operating on the array from the other end.
166
240
@@ -169,14 +243,17 @@ There are two operations with such equivalents: `reduce` with `reduceRight`.
169
243
This rule is auto fixable.
170
244
171
245
#### Examples
246
+
172
247
Code that triggers this rule:
248
+
173
249
``` js
174
250
const string = array .reverse ().reduce ((p , c ) => p + c, ' ' );
175
251
176
252
const reverseString = array .reverse ().reduceRight ((p , c ) => p + c, ' ' );
177
253
```
178
254
179
255
Code that doesn't trigger this rule:
256
+
180
257
``` js
181
258
const reverseString = array .reduce ((p , c ) => p + c, ' ' );
182
259
@@ -187,21 +264,42 @@ const reverseArray = array.reverse();
187
264
const reverseMap = array .reverse ().map ((r ) => r + 1 );
188
265
```
189
266
267
+ #### Using the rule
268
+
269
+ To use this rule, your ` .eslintrc.json ` should at least contain the following (may look different for other config file styles):
270
+
271
+ ``` json
272
+ {
273
+ "plugin" : [
274
+ " array-func"
275
+ ],
276
+ "rules" : {
277
+ "array-func/avoid-reverse" : " error"
278
+ }
279
+ }
280
+ ```
281
+
282
+ Alternatively you can use a [ configuration] ( #configurations ) included with this plugin.
283
+
190
284
### ` prefer-flat-map `
285
+
191
286
Use ` .flatMap() ` to flatten an array and map the values instead of using
192
287
` .flat().map() ` .
193
288
194
289
This rule is auto fixable.
195
290
196
291
#### Examples
292
+
197
293
Code that triggers this rule:
294
+
198
295
``` js
199
296
const flattenedAndMapped = array .map ((p ) => p).flat ();
200
297
201
298
const flatWithDefaultDepth = array .map ((r ) => r).flat (1 );
202
299
```
203
300
204
301
Code that doesn't trigger this rule:
302
+
205
303
``` js
206
304
const oneAction = array .flatMap ((m ) => m);
207
305
@@ -216,7 +314,25 @@ const flatMappedWithExtra = array.map((r) => r + 1).reverse().flat();
216
314
const flatWithDepth = array .map ((p ) => p).flat (99 );
217
315
```
218
316
317
+ #### Using the rule
318
+
319
+ To use this rule, your ` .eslintrc.json ` should at least contain the following (may look different for other config file styles):
320
+
321
+ ``` json
322
+ {
323
+ "plugin" : [
324
+ " array-func"
325
+ ],
326
+ "rules" : {
327
+ "array-func/prefer-flat-map" : " error"
328
+ }
329
+ }
330
+ ```
331
+
332
+ Alternatively you can use a [ configuration] ( #configurations ) included with this plugin.
333
+
219
334
### ` prefer-flat `
335
+
220
336
Use ` .flat() ` to flatten an array of arrays. This rule currently recognizes two
221
337
patterns and can replace them with a ` .flat() ` call:
222
338
@@ -226,14 +342,17 @@ patterns and can replace them with a `.flat()` call:
226
342
This rule is auto fixable.
227
343
228
344
#### Examples
345
+
229
346
Code that triggers this rule:
347
+
230
348
``` js
231
349
const concatFlat = [].concat (... array);
232
350
233
351
const reduceFlat = array .reduce ((p , n ) => p .concat (n), []);
234
352
```
235
353
236
354
Code that doesn't trigger this rule:
355
+
237
356
``` js
238
357
const flattened = array .flat ();
239
358
@@ -242,9 +361,27 @@ const reverseFlat = array.reduce((p, n) => n.concat(p), []);
242
361
const otherReduce = array .reduce ((p , n ) => n + p, 0 );
243
362
```
244
363
364
+ #### Using the rule
365
+
366
+ To use this rule, your ` .eslintrc.json ` should at least contain the following (may look different for other config file styles):
367
+
368
+ ``` json
369
+ {
370
+ "plugin" : [
371
+ " array-func"
372
+ ],
373
+ "rules" : {
374
+ "array-func/prefer-flat" : " error"
375
+ }
376
+ }
377
+ ```
378
+
379
+ Alternatively you can use a [ configuration] ( #configurations ) included with this plugin.
380
+
245
381
## Configurations
246
382
247
383
### ` array-func/recommended ` Configuration
384
+
248
385
The recommended configuration will set your parser ECMA Version to 2015, since that's when the Array functions and methods were added.
249
386
250
387
Rule | Error level | Fixable
@@ -255,7 +392,9 @@ Rule | Error level | Fixable
255
392
` array-func/avoid-reverse ` | Error | Yes
256
393
257
394
#### Using the Configuration
395
+
258
396
To enable this configuration use the ` extends ` property in your ` .eslintrc.json ` config file (may look different for other config file styles):
397
+
259
398
``` json
260
399
{
261
400
"extends" : [
@@ -265,6 +404,7 @@ To enable this configuration use the `extends` property in your `.eslintrc.json`
265
404
```
266
405
267
406
### ` array-func/all ` Configuration
407
+
268
408
The recommended configuration does not include all rules, since some Array methods
269
409
were added after ES2015. The all configuration enables all rules the plugin
270
410
contains and sets the ECMA version appropriately.
@@ -278,5 +418,18 @@ Rule | Error level | Fixable
278
418
` array-func/prefer-flat-map ` | Error | Yes
279
419
` array-func/prefer-flat ` | Error | Yes
280
420
421
+ #### Using the Configuration
422
+
423
+ To enable this configuration use the ` extends ` property in your ` .eslintrc.json ` config file (may look different for other config file styles):
424
+
425
+ ``` json
426
+ {
427
+ "extends" : [
428
+ " plugin:array-func/all"
429
+ ]
430
+ }
431
+ ```
432
+
281
433
## License
434
+
282
435
The ` array-func ` plugin is licensed under the [ MIT License] ( LICENSE ) .
0 commit comments