Skip to content

Commit

Permalink
prefer-array-find: Change checkFromLast default value to true (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker authored May 25, 2024
1 parent 8957a03 commit a449af9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 24 deletions.
25 changes: 13 additions & 12 deletions docs/rules/prefer-array-find.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,18 @@ This rule is fixable unless default values are used in declaration or assignment
const item = array.filter(x => isUnicorn(x))[0];
```

```js
const item = array.filter(x => isUnicorn(x)).at(-1);
```

```js
const item = array.filter(x => isUnicorn(x)).shift();
```

```js
const item = array.filter(x => isUnicorn(x)).pop();
```

```js
const [item] = array.filter(x => isUnicorn(x));
```
Expand Down Expand Up @@ -50,25 +58,18 @@ Type: `object`
### checkFromLast

Type: `boolean`\
Default: `false`
Default: `true`

Pass `checkFromLast: true` to check cases searching from last.
Pass `checkFromLast: false` to disable check cases searching from last.

#### Fail
#### Pass

```js
// eslint unicorn/prefer-array-find: ["error", {"checkFromLast": true}]
// eslint unicorn/prefer-array-find: ["error", {"checkFromLast": false}]
const item = array.filter(x => isUnicorn(x)).at(-1);
```

```js
// eslint unicorn/prefer-array-find: ["error", {"checkFromLast": true}]
// eslint unicorn/prefer-array-find: ["error", {"checkFromLast": false}]
const item = array.filter(x => isUnicorn(x)).pop();
```

#### Pass

```js
// eslint unicorn/prefer-array-find: ["error", {"checkFromLast": true}]
const item = array.findLast(x => isUnicorn(x));
```
6 changes: 3 additions & 3 deletions rules/prefer-array-find.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ const create = context => {
const {
checkFromLast,
} = {
checkFromLast: false,
checkFromLast: true,
...context.options[0],
};

Expand Down Expand Up @@ -428,8 +428,8 @@ const schema = [
properties: {
checkFromLast: {
type: 'boolean',
// TODO: Change default value to `true`, or remove the option when targeting Node.js 18.
default: false,
// TODO: Remove the option at some point.
default: true,
},
},
},
Expand Down
15 changes: 6 additions & 9 deletions test/prefer-array-find.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -923,15 +923,12 @@ test({
],
});

// Check from last
const checkFromLastOptions = [{checkFromLast: true}];

// Default to false
// `checkFromLast` default to true
test({
valid: [
'array.filter(foo).pop()',
'array.filter(foo).at(-1)',
],
].map(code => ({code, options: [{checkFromLast: false}]})),
invalid: [],
});

Expand Down Expand Up @@ -968,7 +965,7 @@ test({
'array.filter().pop()',
'array.filter(foo, thisArgument, extraArgument).pop()',
'array.filter(...foo).pop()',
].map(code => ({code, options: checkFromLastOptions})),
],
invalid: [
{
code: 'array.filter(foo).pop()',
Expand Down Expand Up @@ -1005,7 +1002,7 @@ test({
`,
errors: [{messageId: ERROR_POP}],
},
].map(test => ({...test, options: checkFromLastOptions})),
],
});

// `.at(-1)`
Expand Down Expand Up @@ -1058,7 +1055,7 @@ test({
'array.filter().at(-1)',
'array.filter(foo, thisArgument, extraArgument).at(-1)',
'array.filter(...foo).at(-1)',
].map(code => ({code, options: checkFromLastOptions})),
],
invalid: [
{
code: 'array.filter(foo).at(-1)',
Expand Down Expand Up @@ -1099,7 +1096,7 @@ test({
`,
errors: [{messageId: ERROR_AT_MINUS_ONE}],
},
].map(test => ({...test, options: checkFromLastOptions})),
],
});

// `.at(0)`
Expand Down

0 comments on commit a449af9

Please sign in to comment.