Skip to content

Commit c63e698

Browse files
authored
Add no-unnecessary-array-flat-depth rule (#2618)
1 parent 1f6e172 commit c63e698

8 files changed

+214
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Disallow using `1` as the `depth` argument of `Array#flat()`
2+
3+
💼 This rule is enabled in the ✅ `recommended` [config](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config).
4+
5+
🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).
6+
7+
<!-- end auto-generated rule header -->
8+
<!-- Do not manually modify this header. Run: `npm run fix:eslint-docs` -->
9+
10+
Passing `1` as the `depth` argument to [`Array#flat(depth)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat) is unnecessary.
11+
12+
## Examples
13+
14+
```js
15+
//
16+
foo.flat(1);
17+
18+
//
19+
foo.flat();
20+
```
21+
22+
```js
23+
//
24+
foo?.flat(1);
25+
26+
//
27+
foo?.flat();
28+
```

docs/rules/no-unnecessary-array-splice-count.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<!-- Remove this comment, add more detailed description. -->
1111

12-
When calling `Array#splice(start, deleteCount)` and `Array#toSpliced(start, skipCount)`, omitting the `deleteCount` and `skipCount` argument will delete or skip all elements after `start`. Using `.length` or `Infinity` is unnecessary.
12+
When calling [`Array#splice(start, deleteCount)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice) and [`Array#toSpliced(start, skipCount)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toSpliced), omitting the `deleteCount` and `skipCount` argument will delete or skip all elements after `start`. Using `.length` or `Infinity` is unnecessary.
1313

1414
## Examples
1515

readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ export default [
108108
| [no-thenable](docs/rules/no-thenable.md) | Disallow `then` property. || | |
109109
| [no-this-assignment](docs/rules/no-this-assignment.md) | Disallow assigning `this` to a variable. || | |
110110
| [no-typeof-undefined](docs/rules/no-typeof-undefined.md) | Disallow comparing `undefined` using `typeof`. || 🔧 | 💡 |
111+
| [no-unnecessary-array-flat-depth](docs/rules/no-unnecessary-array-flat-depth.md) | Disallow using `1` as the `depth` argument of `Array#flat()`. || 🔧 | |
111112
| [no-unnecessary-array-splice-count](docs/rules/no-unnecessary-array-splice-count.md) | Disallow using `.length` or `Infinity` as the `deleteCount` or `skipCount` argument of `Array#{splice,toSpliced}()`. || 🔧 | |
112113
| [no-unnecessary-await](docs/rules/no-unnecessary-await.md) | Disallow awaiting non-promise values. || 🔧 | |
113114
| [no-unnecessary-polyfills](docs/rules/no-unnecessary-polyfills.md) | Enforce the use of built-in methods instead of unnecessary polyfills. || | |

rules/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import noStaticOnlyClass from './no-static-only-class.js';
5353
import noThenable from './no-thenable.js';
5454
import noThisAssignment from './no-this-assignment.js';
5555
import noTypeofUndefined from './no-typeof-undefined.js';
56+
import noUnnecessaryArrayFlatDepth from './no-unnecessary-array-flat-depth.js';
5657
import noUnnecessaryArraySpliceCount from './no-unnecessary-array-splice-count.js';
5758
import noUnnecessaryAwait from './no-unnecessary-await.js';
5859
import noUnnecessaryPolyfills from './no-unnecessary-polyfills.js';
@@ -184,6 +185,7 @@ const rules = {
184185
'no-thenable': createRule(noThenable, 'no-thenable'),
185186
'no-this-assignment': createRule(noThisAssignment, 'no-this-assignment'),
186187
'no-typeof-undefined': createRule(noTypeofUndefined, 'no-typeof-undefined'),
188+
'no-unnecessary-array-flat-depth': createRule(noUnnecessaryArrayFlatDepth, 'no-unnecessary-array-flat-depth'),
187189
'no-unnecessary-array-splice-count': createRule(noUnnecessaryArraySpliceCount, 'no-unnecessary-array-splice-count'),
188190
'no-unnecessary-await': createRule(noUnnecessaryAwait, 'no-unnecessary-await'),
189191
'no-unnecessary-polyfills': createRule(noUnnecessaryPolyfills, 'no-unnecessary-polyfills'),
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import {isMethodCall, isLiteral} from './ast/index.js';
2+
import {removeArgument} from './fix/index.js';
3+
import {} from './utils/index.js';
4+
5+
const MESSAGE_ID = 'no-unnecessary-array-flat-depth';
6+
const messages = {
7+
[MESSAGE_ID]: 'Passing `1` as the `depth` argument is unnecessary.',
8+
};
9+
10+
/** @param {import('eslint').Rule.RuleContext} context */
11+
const create = context => {
12+
context.on('CallExpression', callExpression => {
13+
if (!(
14+
isMethodCall(callExpression, {
15+
method: 'flat',
16+
argumentsLength: 1,
17+
optionalCall: false,
18+
})
19+
&& isLiteral(callExpression.arguments[0], 1)
20+
)) {
21+
return;
22+
}
23+
24+
const [numberOne] = callExpression.arguments;
25+
26+
return {
27+
node: numberOne,
28+
messageId: MESSAGE_ID,
29+
/** @param {import('eslint').Rule.RuleFixer} fixer */
30+
fix: fixer => removeArgument(fixer, numberOne, context.sourceCode),
31+
};
32+
});
33+
};
34+
35+
/** @type {import('eslint').Rule.RuleModule} */
36+
const config = {
37+
create,
38+
meta: {
39+
type: 'suggestion',
40+
docs: {
41+
description: 'Disallow using `1` as the `depth` argument of `Array#flat()`.',
42+
recommended: true,
43+
},
44+
fixable: 'code',
45+
46+
messages,
47+
},
48+
};
49+
50+
export default config;
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import {getTester} from './utils/test.js';
2+
3+
const {test} = getTester(import.meta);
4+
5+
test.snapshot({
6+
valid: [
7+
'foo.flat()',
8+
'foo.flat?.(1)',
9+
'foo?.flat()',
10+
'foo.flat(1, extra)',
11+
'flat(1)',
12+
'new foo.flat(1)',
13+
'const ONE = 1; foo.flat(ONE)',
14+
'foo.notFlat(1)',
15+
],
16+
invalid: [
17+
'foo.flat(1)',
18+
'foo.flat(1.0)',
19+
'foo.flat(0b01)',
20+
'foo?.flat(1)',
21+
],
22+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# Snapshot report for `test/no-unnecessary-array-flat-depth.js`
2+
3+
The actual snapshot is saved in `no-unnecessary-array-flat-depth.js.snap`.
4+
5+
Generated by [AVA](https://avajs.dev).
6+
7+
## invalid(1): foo.flat(1)
8+
9+
> Input
10+
11+
`␊
12+
1 | foo.flat(1)␊
13+
`
14+
15+
> Output
16+
17+
`␊
18+
1 | foo.flat()␊
19+
`
20+
21+
> Error 1/1
22+
23+
`␊
24+
> 1 | foo.flat(1)␊
25+
| ^ Passing \`1\` as the \`depth\` argument is unnecessary.␊
26+
`
27+
28+
## invalid(2): foo.flat(1.0)
29+
30+
> Input
31+
32+
`␊
33+
1 | foo.flat(1.0)␊
34+
`
35+
36+
> Output
37+
38+
`␊
39+
1 | foo.flat()␊
40+
`
41+
42+
> Error 1/1
43+
44+
`␊
45+
> 1 | foo.flat(1.0)␊
46+
| ^^^ Passing \`1\` as the \`depth\` argument is unnecessary.␊
47+
`
48+
49+
## invalid(3): foo.flat(0b01)
50+
51+
> Input
52+
53+
`␊
54+
1 | foo.flat(0b01)␊
55+
`
56+
57+
> Output
58+
59+
`␊
60+
1 | foo.flat()␊
61+
`
62+
63+
> Error 1/1
64+
65+
`␊
66+
> 1 | foo.flat(0b01)␊
67+
| ^^^^ Passing \`1\` as the \`depth\` argument is unnecessary.␊
68+
`
69+
70+
## invalid(4): foo?.flat(1)
71+
72+
> Input
73+
74+
`␊
75+
1 | foo?.flat(1)␊
76+
`
77+
78+
> Output
79+
80+
`␊
81+
1 | foo?.flat()␊
82+
`
83+
84+
> Error 1/1
85+
86+
`␊
87+
> 1 | foo?.flat(1)␊
88+
| ^ Passing \`1\` as the \`depth\` argument is unnecessary.␊
89+
`
90+
91+
## invalid(3): foo?.flat(1)
92+
93+
> Input
94+
95+
`␊
96+
1 | foo?.flat(1)␊
97+
`
98+
99+
> Output
100+
101+
`␊
102+
1 | foo?.flat()␊
103+
`
104+
105+
> Error 1/1
106+
107+
`␊
108+
> 1 | foo?.flat(1)␊
109+
| ^ Passing \`1\` as the \`depth\` argument is unnecessary.␊
110+
`
Binary file not shown.

0 commit comments

Comments
 (0)