Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add no-magic-array-flat-depth rule #2335

Merged
merged 9 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions configs/recommended.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ module.exports = {
'unicorn/no-invalid-remove-event-listener': 'error',
'unicorn/no-keyword-prefix': 'off',
'unicorn/no-lonely-if': 'error',
'unicorn/no-magic-array-flat-depth': 'error',
'no-negated-condition': 'off',
'unicorn/no-negated-condition': 'error',
'no-nested-ternary': 'off',
Expand Down
40 changes: 40 additions & 0 deletions docs/rules/no-magic-array-flat-depth.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Disallow magic number as the `depth` argument in `Array#flat(…).`
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Disallow magic number as the `depth` argument in `Array#flat(…).`
# Disallow a magic number as the `depth` argument in `Array#flat(…).`


💼 This rule is enabled in the ✅ `recommended` [config](https://github.com/sindresorhus/eslint-plugin-unicorn#preset-configs-eslintconfigjs).

<!-- end auto-generated rule header -->
<!-- Do not manually modify this header. Run: `npm run fix:eslint-docs` -->

When calling [`Array#flat(depth)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat) with depth, normally the depth argument should be `1` or `Infinity`(`Number.POSITIVE_INFINITY`), otherwise it should be a meaningful variable name or explained with a comment.

## Fail

```js
const foo = array.flat(2);
```

```js
const foo = array.flat(99);
```

## Pass

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

```js
const foo = array.flat(Number.POSITIVE_INFINITY);
```

```js
const foo = array.flat(Infinity);
```

```js
const foo = array.flat(depth);
```

```js
const foo = array.flat(/* The depth is always 2 */ 2);
```
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ If you don't use the preset, ensure you use the same `env` and `parserOptions` c
| [no-invalid-remove-event-listener](docs/rules/no-invalid-remove-event-listener.md) | Prevent calling `EventTarget#removeEventListener()` with the result of an expression. | ✅ | | |
| [no-keyword-prefix](docs/rules/no-keyword-prefix.md) | Disallow identifiers starting with `new` or `class`. | | | |
| [no-lonely-if](docs/rules/no-lonely-if.md) | Disallow `if` statements as the only statement in `if` blocks without `else`. | ✅ | 🔧 | |
| [no-magic-array-flat-depth](docs/rules/no-magic-array-flat-depth.md) | Disallow magic number as the `depth` argument in `Array#flat(…).` | ✅ | | |
| [no-negated-condition](docs/rules/no-negated-condition.md) | Disallow negated conditions. | ✅ | 🔧 | |
| [no-nested-ternary](docs/rules/no-nested-ternary.md) | Disallow nested ternary expressions. | ✅ | 🔧 | |
| [no-new-array](docs/rules/no-new-array.md) | Disallow `new Array()`. | ✅ | 🔧 | 💡 |
Expand Down
51 changes: 51 additions & 0 deletions rules/no-magic-array-flat-depth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'use strict';
const {isOpeningParenToken} = require('@eslint-community/eslint-utils');
const {isMethodCall, isNumberLiteral} = require('./ast/index.js');

const MESSAGE_ID = 'no-magic-array-flat-depth';
const messages = {
[MESSAGE_ID]: 'Magic number as depth is not allowed.',
};

/** @param {import('eslint').Rule.RuleContext} context */
const create = context => ({
CallExpression(callExpression) {
if (!isMethodCall(callExpression, {
method: 'flat',
argumentsLength: 1,
optionalCall: false,
})) {
return;
}

const [depth] = callExpression.arguments;

if (!isNumberLiteral(depth) || depth.value === 1) {
return;
}

const {sourceCode} = context;
const openingParenthesisToken = sourceCode.getTokenAfter(callExpression.callee, isOpeningParenToken);
const closingParenthesisToken = sourceCode.getLastToken(callExpression);
if (sourceCode.commentsExistBetween(openingParenthesisToken, closingParenthesisToken)) {
return;
}

return {
node: depth,
messageId: MESSAGE_ID,
};
},
});

/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
create,
meta: {
type: 'suggestion',
docs: {
description: 'Disallow magic number as the `depth` argument in `Array#flat(…).`',
fisker marked this conversation as resolved.
Show resolved Hide resolved
},
messages,
},
};
28 changes: 28 additions & 0 deletions test/no-magic-array-flat-depth.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {getTester} from './utils/test.mjs';

const {test} = getTester(import.meta);

test.snapshot({
valid: [
'array.flat(1)',
'array.flat(1.0)',
'array.flat(0x01)',
'array.flat(unknown)',
'array.flat(Number.POSITIVE_INFINITY)',
'array.flat(Infinity)',
'array.flat(/* explanation */2)',
'array.flat(2/* explanation */)',
'array.flat()',
'array.flat(2, extraArgument)',
'new array.flat(2)',
'array.flat?.(2)',
'array.notFlat(2)',
'flat(2)',
],
invalid: [
'array.flat(2)',
'array?.flat(2)',
'array.flat(99,)',
'array.flat(0b10,)',
],
});
65 changes: 65 additions & 0 deletions test/snapshots/no-magic-array-flat-depth.mjs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Snapshot report for `test/no-magic-array-flat-depth.mjs`

The actual snapshot is saved in `no-magic-array-flat-depth.mjs.snap`.

Generated by [AVA](https://avajs.dev).

## invalid(1): array.flat(2)

> Input

`␊
1 | array.flat(2)␊
`

> Error 1/1

`␊
> 1 | array.flat(2)␊
| ^ Magic number as depth is not allowed.␊
`

## invalid(2): array?.flat(2)

> Input

`␊
1 | array?.flat(2)␊
`

> Error 1/1

`␊
> 1 | array?.flat(2)␊
| ^ Magic number as depth is not allowed.␊
`

## invalid(3): array.flat(99,)

> Input

`␊
1 | array.flat(99,)␊
`

> Error 1/1

`␊
> 1 | array.flat(99,)␊
| ^^ Magic number as depth is not allowed.␊
`

## invalid(4): array.flat(0b10,)

> Input

`␊
1 | array.flat(0b10,)␊
`

> Error 1/1

`␊
> 1 | array.flat(0b10,)␊
| ^^^^ Magic number as depth is not allowed.␊
`
Binary file added test/snapshots/no-magic-array-flat-depth.mjs.snap
Binary file not shown.
Loading