Skip to content

Commit

Permalink
feat: support Array.prototype.findIndex and `Array.prototype.findIn…
Browse files Browse the repository at this point in the history
…dexLast`.

Fixes #16313
  • Loading branch information
sosukesuzuki committed Sep 15, 2022
1 parent 8cc0bbe commit 329ed97
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/rules/array-callback-return.js
Expand Up @@ -16,7 +16,7 @@ const astUtils = require("./utils/ast-utils");
//------------------------------------------------------------------------------

const TARGET_NODE_TYPE = /^(?:Arrow)?FunctionExpression$/u;
const TARGET_METHODS = /^(?:every|filter|find(?:Index)?|flatMap|forEach|map|reduce(?:Right)?|some|sort)$/u;
const TARGET_METHODS = /^(?:every|filter|find(?:Last)?(?:Index)?|flatMap|forEach|map|reduce(?:Right)?|some|sort)$/u;

/**
* Checks a given code path segment is reachable.
Expand Down
8 changes: 8 additions & 0 deletions tests/lib/rules/array-callback-return.js
Expand Up @@ -57,6 +57,8 @@ ruleTester.run("array-callback-return", rule, {
"foo.filter(function() { return true; })",
"foo.find(function() { return true; })",
"foo.findIndex(function() { return true; })",
"foo.findLast(function() { return true; })",
"foo.findLastIndex(function() { return true; })",
"foo.flatMap(function() { return true; })",
"foo.forEach(function() { return; })",
"foo.map(function() { return true; })",
Expand All @@ -77,6 +79,8 @@ ruleTester.run("array-callback-return", rule, {
{ code: "foo.filter(function() { return; })", options: allowImplicitOptions },
{ code: "foo.find(function() { return; })", options: allowImplicitOptions },
{ code: "foo.findIndex(function() { return; })", options: allowImplicitOptions },
{ code: "foo.findLast(function() { return; })", options: allowImplicitOptions },
{ code: "foo.findLastIndex(function() { return; })", options: allowImplicitOptions },
{ code: "foo.flatMap(function() { return; })", options: allowImplicitOptions },
{ code: "foo.forEach(function() { return; })", options: allowImplicitOptions },
{ code: "foo.map(function() { return; })", options: allowImplicitOptions },
Expand Down Expand Up @@ -129,8 +133,12 @@ ruleTester.run("array-callback-return", rule, {
{ code: "foo.filter(function foo() {})", errors: [{ messageId: "expectedInside", data: { name: "function 'foo'", arrayMethodName: "Array.prototype.filter" } }] },
{ code: "foo.find(function() {})", errors: [{ messageId: "expectedInside", data: { name: "function", arrayMethodName: "Array.prototype.find" } }] },
{ code: "foo.find(function foo() {})", errors: [{ messageId: "expectedInside", data: { name: "function 'foo'", arrayMethodName: "Array.prototype.find" } }] },
{ code: "foo.findLast(function() {})", errors: [{ messageId: "expectedInside", data: { name: "function", arrayMethodName: "Array.prototype.findLast" } }] },
{ code: "foo.findLast(function foo() {})", errors: [{ messageId: "expectedInside", data: { name: "function 'foo'", arrayMethodName: "Array.prototype.findLast" } }] },
{ code: "foo.findIndex(function() {})", errors: [{ messageId: "expectedInside", data: { name: "function", arrayMethodName: "Array.prototype.findIndex" } }] },
{ code: "foo.findIndex(function foo() {})", errors: [{ messageId: "expectedInside", data: { name: "function 'foo'", arrayMethodName: "Array.prototype.findIndex" } }] },
{ code: "foo.findLastIndex(function() {})", errors: [{ messageId: "expectedInside", data: { name: "function", arrayMethodName: "Array.prototype.findLastIndex" } }] },
{ code: "foo.findLastIndex(function foo() {})", errors: [{ messageId: "expectedInside", data: { name: "function 'foo'", arrayMethodName: "Array.prototype.findLastIndex" } }] },
{ code: "foo.flatMap(function() {})", errors: [{ messageId: "expectedInside", data: { name: "function", arrayMethodName: "Array.prototype.flatMap" } }] },
{ code: "foo.flatMap(function foo() {})", errors: [{ messageId: "expectedInside", data: { name: "function 'foo'", arrayMethodName: "Array.prototype.flatMap" } }] },
{ code: "foo.map(function() {})", errors: [{ messageId: "expectedInside", data: { name: "function", arrayMethodName: "Array.prototype.map" } }] },
Expand Down

0 comments on commit 329ed97

Please sign in to comment.