Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
feat: array-callback-return support findLast and findLastIndex (#…
…16314)

* feat: support `Array.prototype.find(Last)Index`.

Fixes #16313

* docs: update `array-callback-return` docs
  • Loading branch information
sosukesuzuki committed Sep 16, 2022
1 parent 8cc0bbe commit f02bcd9
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 1 deletion.
2 changes: 2 additions & 0 deletions docs/src/rules/array-callback-return.md
Expand Up @@ -27,6 +27,8 @@ This rule finds callback functions of the following methods, then checks usage o
* [`Array.prototype.filter`](https://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.filter)
* [`Array.prototype.find`](https://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.find)
* [`Array.prototype.findIndex`](https://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.findindex)
* [`Array.prototype.findLast`](https://tc39.es/ecma262/#sec-array.prototype.findlast)
* [`Array.prototype.findLastIndex`](https://tc39.es/ecma262/#sec-array.prototype.findlastindex)
* [`Array.prototype.flatMap`](https://www.ecma-international.org/ecma-262/10.0/#sec-array.prototype.flatmap)
* [`Array.prototype.forEach`](https://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.foreach) (optional, based on `checkForEach` parameter)
* [`Array.prototype.map`](https://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.map)
Expand Down
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 f02bcd9

Please sign in to comment.