-
-
Notifications
You must be signed in to change notification settings - Fork 367
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
no-magic-array-flat-depth
rule (#2335)
- Loading branch information
Showing
7 changed files
with
187 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# 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), the depth argument should normally be `1` or `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); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
'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 a magic number as the `depth` argument in `Array#flat(…).`', | ||
recommended: true, | ||
}, | ||
messages, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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,)', | ||
], | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.