Skip to content

Commit

Permalink
Add prefer-array-flat rule (#1126)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker authored Feb 24, 2021
1 parent 8161b06 commit 2397d94
Show file tree
Hide file tree
Showing 11 changed files with 1,323 additions and 15 deletions.
84 changes: 84 additions & 0 deletions docs/rules/prefer-array-flat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# Prefer `Array#flat()` over legacy techniques to flatten arrays

ES2019 introduced a new method [`Array#flat()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat) that flatten arrays.

This rule is fixable.

## Fail

```js
const foo = bar.flatMap(x => x);
```

```js
const foo = bar.reduce((a, b) => a.concat(b), []);
```

```js
const foo = bar.reduce((a, b) => [...a, ...b], []);
```

```js
const foo = [].concat(bar);
```

```js
const foo = [].concat(...bar);
```

```js
const foo = [].concat.apply([], bar);
```

```js
const foo = Array.prototype.concat.apply([], bar);
```

```js
const foo = _.flatten(bar);
```

```js
const foo = lodash.flatten(bar);
```

```js
const foo = underscore.flatten(bar);
```

## Pass

```js
const foo = bar.flat();
```

## Options

Type: `object`

### functions

Type: `string[]`

You can also check custom functions that flatten arrays.

Example:

```js
{
'unicorn/prefer-array-flat': [
'error',
{
functions: [
'flatArray',
'utils.flat'
]
}
]
}
```

```js
// eslint unicorn/prefer-array-flat: ["error", {"functions": ["utils.flat"]}]
const foo = utils.flat(bar); // Fails
```
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ module.exports = {
'unicorn/prefer-add-event-listener': 'error',
'unicorn/prefer-array-find': 'error',
// TODO: Enable this by default when targeting Node.js 12.
'unicorn/prefer-array-flat': 'off',
// TODO: Enable this by default when targeting Node.js 12.
'unicorn/prefer-array-flat-map': 'off',
'unicorn/prefer-array-index-of': 'error',
'unicorn/prefer-array-some': 'error',
Expand Down
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ Configure it in `package.json`.
"unicorn/numeric-separators-style": "off",
"unicorn/prefer-add-event-listener": "error",
"unicorn/prefer-array-find": "error",
"unicorn/prefer-array-flat": "error",
"unicorn/prefer-array-flat-map": "error",
"unicorn/prefer-array-index-of": "error",
"unicorn/prefer-array-some": "error",
Expand Down Expand Up @@ -155,6 +156,7 @@ Configure it in `package.json`.
- [numeric-separators-style](docs/rules/numeric-separators-style.md) - Enforce the style of numeric separators by correctly grouping digits. *(fixable)*
- [prefer-add-event-listener](docs/rules/prefer-add-event-listener.md) - Prefer `.addEventListener()` and `.removeEventListener()` over `on`-functions. *(partly fixable)*
- [prefer-array-find](docs/rules/prefer-array-find.md) - Prefer `.find(…)` over the first element from `.filter(…)`. *(partly fixable)*
- [prefer-array-flat](docs/rules/prefer-array-flat.md) - Prefer `Array#flat()` over legacy techniques to flatten arrays. *(fixable)*
- [prefer-array-flat-map](docs/rules/prefer-array-flat-map.md) - Prefer `.flatMap(…)` over `.map(…).flat()`. *(fixable)*
- [prefer-array-index-of](docs/rules/prefer-array-index-of.md) - Prefer `Array#indexOf()` over `Array#findIndex()` when looking for the index of an item. *(partly fixable)*
- [prefer-array-some](docs/rules/prefer-array-some.md) - Prefer `.some(…)` over `.find(…)`.
Expand Down
2 changes: 1 addition & 1 deletion rules/no-array-callback-reference.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const {isParenthesized} = require('eslint-utils');
const getDocumentationUrl = require('./utils/get-documentation-url');
const methodSelector = require('./utils/method-selector');
const {notFunctionSelector} = require('./utils/not-function');
const isNodeMatches = require('./utils/is-node-matches');
const {isNodeMatches} = require('./utils/is-node-matches');

const ERROR_WITH_NAME_MESSAGE_ID = 'error-with-name';
const ERROR_WITHOUT_NAME_MESSAGE_ID = 'error-without-name';
Expand Down
5 changes: 3 additions & 2 deletions rules/no-array-for-each.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const shouldAddParenthesesToExpressionStatementExpression = require('./utils/sho
const getParenthesizedTimes = require('./utils/get-parenthesized-times');
const extendFixRange = require('./utils/extend-fix-range');
const isFunctionSelfUsedInside = require('./utils/is-function-self-used-inside');
const isNodeMatches = require('./utils/is-node-matches');
const {isNodeMatches} = require('./utils/is-node-matches');
const assertToken = require('./utils/assert-token');

const MESSAGE_ID = 'no-array-for-each';
Expand All @@ -24,7 +24,8 @@ const messages = {

const arrayForEachCallSelector = methodSelector({
name: 'forEach',
includeOptional: true
includeOptionalCall: true,
includeOptionalMember: true
});

const continueAbleNodeTypes = new Set([
Expand Down
Loading

0 comments on commit 2397d94

Please sign in to comment.