Skip to content

Commit fd1d1e8

Browse files
committed
feat: add prefer-flat-map and prefer-flat rules
1 parent 26372c1 commit fd1d1e8

11 files changed

+637
-772
lines changed

.editorconfig

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# EditorConfig is awesome: http://EditorConfig.org
2+
3+
root = true
4+
5+
[*]
6+
end_of_line = lf
7+
insert_final_newline = true
8+
trim_trailing_whitespace = true
9+
charset = utf-8
10+
indent_style = space
11+
indent_size = 4
12+
# C-style doc comments for eclint
13+
block_comment_start = /*
14+
block_comment = *
15+
block_comment_end = */
16+
17+
[{package.json,.travis.yml,package-lock.json,codecov.yml,.all-contributorsrc}]
18+
indent_style = space
19+
indent_size = 2

README.md

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,14 @@ Rules for Array functions and methods.
1717
- [Examples](#examples-2)
1818
- [`avoid-reverse`](#avoid-reverse)
1919
- [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)
2228
- [License](#license)
2329

2430
## Installation
@@ -189,7 +195,60 @@ const reverseArray = array.reverse();
189195
const reverseMap = array.reverse().map((r) => r + 1);
190196
```
191197

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
193252
The recommended configuration will set your parser ECMA Version to 2015, since that's when the Array functions and methods were added.
194253

195254
Rule | Error level | Fixable
@@ -199,7 +258,7 @@ Rule | Error level | Fixable
199258
`prefer-array-from` | Error | Yes
200259
`avoid-reverse` | Error | Yes
201260

202-
### Using the Configuration
261+
#### Using the Configuration
203262
To enable this configuration use the `extends` property in your `.eslintrc.json` config file (may look different for other config file styles):
204263
```json
205264
{
@@ -209,5 +268,19 @@ To enable this configuration use the `extends` property in your `.eslintrc.json`
209268
}
210269
```
211270

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+
212285
## License
213286
The `array-func` plugin is licensed under the [MIT License](LICENSE).

index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@ module.exports = {
2323
"array-func/prefer-array-from": "error",
2424
"array-func/avoid-reverse": "error"
2525
}
26+
},
27+
all: {
28+
parserOptions: {
29+
ecmaVersion: 2018
30+
},
31+
plugins: [ 'array-func' ],
32+
rules: {
33+
"array-func/prefer-flat-map": "error",
34+
"array-func/prefer-flat": "error"
35+
},
36+
extends: [ 'plugin:array-func/recommended' ]
2637
}
2738
}
2839
};

0 commit comments

Comments
 (0)