Skip to content

Commit a449af9

Browse files
authoredMay 25, 2024··
prefer-array-find: Change checkFromLast default value to true (#2367)
1 parent 8957a03 commit a449af9

File tree

3 files changed

+22
-24
lines changed

3 files changed

+22
-24
lines changed
 

‎docs/rules/prefer-array-find.md

+13-12
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,18 @@ This rule is fixable unless default values are used in declaration or assignment
1717
const item = array.filter(x => isUnicorn(x))[0];
1818
```
1919

20+
```js
21+
const item = array.filter(x => isUnicorn(x)).at(-1);
22+
```
23+
2024
```js
2125
const item = array.filter(x => isUnicorn(x)).shift();
2226
```
2327

28+
```js
29+
const item = array.filter(x => isUnicorn(x)).pop();
30+
```
31+
2432
```js
2533
const [item] = array.filter(x => isUnicorn(x));
2634
```
@@ -50,25 +58,18 @@ Type: `object`
5058
### checkFromLast
5159

5260
Type: `boolean`\
53-
Default: `false`
61+
Default: `true`
5462

55-
Pass `checkFromLast: true` to check cases searching from last.
63+
Pass `checkFromLast: false` to disable check cases searching from last.
5664

57-
#### Fail
65+
#### Pass
5866

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

6472
```js
65-
// eslint unicorn/prefer-array-find: ["error", {"checkFromLast": true}]
73+
// eslint unicorn/prefer-array-find: ["error", {"checkFromLast": false}]
6674
const item = array.filter(x => isUnicorn(x)).pop();
6775
```
68-
69-
#### Pass
70-
71-
```js
72-
// eslint unicorn/prefer-array-find: ["error", {"checkFromLast": true}]
73-
const item = array.findLast(x => isUnicorn(x));
74-
```

‎rules/prefer-array-find.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ const create = context => {
180180
const {
181181
checkFromLast,
182182
} = {
183-
checkFromLast: false,
183+
checkFromLast: true,
184184
...context.options[0],
185185
};
186186

@@ -428,8 +428,8 @@ const schema = [
428428
properties: {
429429
checkFromLast: {
430430
type: 'boolean',
431-
// TODO: Change default value to `true`, or remove the option when targeting Node.js 18.
432-
default: false,
431+
// TODO: Remove the option at some point.
432+
default: true,
433433
},
434434
},
435435
},

‎test/prefer-array-find.mjs

+6-9
Original file line numberDiff line numberDiff line change
@@ -923,15 +923,12 @@ test({
923923
],
924924
});
925925

926-
// Check from last
927-
const checkFromLastOptions = [{checkFromLast: true}];
928-
929-
// Default to false
926+
// `checkFromLast` default to true
930927
test({
931928
valid: [
932929
'array.filter(foo).pop()',
933930
'array.filter(foo).at(-1)',
934-
],
931+
].map(code => ({code, options: [{checkFromLast: false}]})),
935932
invalid: [],
936933
});
937934

@@ -968,7 +965,7 @@ test({
968965
'array.filter().pop()',
969966
'array.filter(foo, thisArgument, extraArgument).pop()',
970967
'array.filter(...foo).pop()',
971-
].map(code => ({code, options: checkFromLastOptions})),
968+
],
972969
invalid: [
973970
{
974971
code: 'array.filter(foo).pop()',
@@ -1005,7 +1002,7 @@ test({
10051002
`,
10061003
errors: [{messageId: ERROR_POP}],
10071004
},
1008-
].map(test => ({...test, options: checkFromLastOptions})),
1005+
],
10091006
});
10101007

10111008
// `.at(-1)`
@@ -1058,7 +1055,7 @@ test({
10581055
'array.filter().at(-1)',
10591056
'array.filter(foo, thisArgument, extraArgument).at(-1)',
10601057
'array.filter(...foo).at(-1)',
1061-
].map(code => ({code, options: checkFromLastOptions})),
1058+
],
10621059
invalid: [
10631060
{
10641061
code: 'array.filter(foo).at(-1)',
@@ -1099,7 +1096,7 @@ test({
10991096
`,
11001097
errors: [{messageId: ERROR_AT_MINUS_ONE}],
11011098
},
1102-
].map(test => ({...test, options: checkFromLastOptions})),
1099+
],
11031100
});
11041101

11051102
// `.at(0)`

0 commit comments

Comments
 (0)
Please sign in to comment.