From a449af99e863f103de101058abc7bf4eccb6297a Mon Sep 17 00:00:00 2001 From: fisker Cheung Date: Sat, 25 May 2024 18:01:43 +0800 Subject: [PATCH] `prefer-array-find`: Change `checkFromLast` default value to `true` (#2367) --- docs/rules/prefer-array-find.md | 25 +++++++++++++------------ rules/prefer-array-find.js | 6 +++--- test/prefer-array-find.mjs | 15 ++++++--------- 3 files changed, 22 insertions(+), 24 deletions(-) diff --git a/docs/rules/prefer-array-find.md b/docs/rules/prefer-array-find.md index 5b7065e03c..4f4b1db54f 100644 --- a/docs/rules/prefer-array-find.md +++ b/docs/rules/prefer-array-find.md @@ -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)); ``` @@ -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)); -``` diff --git a/rules/prefer-array-find.js b/rules/prefer-array-find.js index 736ac4b8c5..85ff943cf1 100644 --- a/rules/prefer-array-find.js +++ b/rules/prefer-array-find.js @@ -180,7 +180,7 @@ const create = context => { const { checkFromLast, } = { - checkFromLast: false, + checkFromLast: true, ...context.options[0], }; @@ -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, }, }, }, diff --git a/test/prefer-array-find.mjs b/test/prefer-array-find.mjs index e886d293ee..a10a6fc7ee 100644 --- a/test/prefer-array-find.mjs +++ b/test/prefer-array-find.mjs @@ -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: [], }); @@ -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()', @@ -1005,7 +1002,7 @@ test({ `, errors: [{messageId: ERROR_POP}], }, - ].map(test => ({...test, options: checkFromLastOptions})), + ], }); // `.at(-1)` @@ -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)', @@ -1099,7 +1096,7 @@ test({ `, errors: [{messageId: ERROR_AT_MINUS_ONE}], }, - ].map(test => ({...test, options: checkFromLastOptions})), + ], }); // `.at(0)`