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

prefer-array-find: Change checkFromLast default value to true #2367

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
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