Skip to content

Commit f02bcd9

Browse files
authored
feat: array-callback-return support findLast and findLastIndex (#16314)
* feat: support `Array.prototype.find(Last)Index`. Fixes #16313 * docs: update `array-callback-return` docs
1 parent 8cc0bbe commit f02bcd9

3 files changed

Lines changed: 11 additions & 1 deletion

File tree

docs/src/rules/array-callback-return.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ This rule finds callback functions of the following methods, then checks usage o
2727
* [`Array.prototype.filter`](https://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.filter)
2828
* [`Array.prototype.find`](https://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.find)
2929
* [`Array.prototype.findIndex`](https://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.findindex)
30+
* [`Array.prototype.findLast`](https://tc39.es/ecma262/#sec-array.prototype.findlast)
31+
* [`Array.prototype.findLastIndex`](https://tc39.es/ecma262/#sec-array.prototype.findlastindex)
3032
* [`Array.prototype.flatMap`](https://www.ecma-international.org/ecma-262/10.0/#sec-array.prototype.flatmap)
3133
* [`Array.prototype.forEach`](https://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.foreach) (optional, based on `checkForEach` parameter)
3234
* [`Array.prototype.map`](https://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.map)

lib/rules/array-callback-return.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ const astUtils = require("./utils/ast-utils");
1616
//------------------------------------------------------------------------------
1717

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

2121
/**
2222
* Checks a given code path segment is reachable.

tests/lib/rules/array-callback-return.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ ruleTester.run("array-callback-return", rule, {
5757
"foo.filter(function() { return true; })",
5858
"foo.find(function() { return true; })",
5959
"foo.findIndex(function() { return true; })",
60+
"foo.findLast(function() { return true; })",
61+
"foo.findLastIndex(function() { return true; })",
6062
"foo.flatMap(function() { return true; })",
6163
"foo.forEach(function() { return; })",
6264
"foo.map(function() { return true; })",
@@ -77,6 +79,8 @@ ruleTester.run("array-callback-return", rule, {
7779
{ code: "foo.filter(function() { return; })", options: allowImplicitOptions },
7880
{ code: "foo.find(function() { return; })", options: allowImplicitOptions },
7981
{ code: "foo.findIndex(function() { return; })", options: allowImplicitOptions },
82+
{ code: "foo.findLast(function() { return; })", options: allowImplicitOptions },
83+
{ code: "foo.findLastIndex(function() { return; })", options: allowImplicitOptions },
8084
{ code: "foo.flatMap(function() { return; })", options: allowImplicitOptions },
8185
{ code: "foo.forEach(function() { return; })", options: allowImplicitOptions },
8286
{ code: "foo.map(function() { return; })", options: allowImplicitOptions },
@@ -129,8 +133,12 @@ ruleTester.run("array-callback-return", rule, {
129133
{ code: "foo.filter(function foo() {})", errors: [{ messageId: "expectedInside", data: { name: "function 'foo'", arrayMethodName: "Array.prototype.filter" } }] },
130134
{ code: "foo.find(function() {})", errors: [{ messageId: "expectedInside", data: { name: "function", arrayMethodName: "Array.prototype.find" } }] },
131135
{ code: "foo.find(function foo() {})", errors: [{ messageId: "expectedInside", data: { name: "function 'foo'", arrayMethodName: "Array.prototype.find" } }] },
136+
{ code: "foo.findLast(function() {})", errors: [{ messageId: "expectedInside", data: { name: "function", arrayMethodName: "Array.prototype.findLast" } }] },
137+
{ code: "foo.findLast(function foo() {})", errors: [{ messageId: "expectedInside", data: { name: "function 'foo'", arrayMethodName: "Array.prototype.findLast" } }] },
132138
{ code: "foo.findIndex(function() {})", errors: [{ messageId: "expectedInside", data: { name: "function", arrayMethodName: "Array.prototype.findIndex" } }] },
133139
{ code: "foo.findIndex(function foo() {})", errors: [{ messageId: "expectedInside", data: { name: "function 'foo'", arrayMethodName: "Array.prototype.findIndex" } }] },
140+
{ code: "foo.findLastIndex(function() {})", errors: [{ messageId: "expectedInside", data: { name: "function", arrayMethodName: "Array.prototype.findLastIndex" } }] },
141+
{ code: "foo.findLastIndex(function foo() {})", errors: [{ messageId: "expectedInside", data: { name: "function 'foo'", arrayMethodName: "Array.prototype.findLastIndex" } }] },
134142
{ code: "foo.flatMap(function() {})", errors: [{ messageId: "expectedInside", data: { name: "function", arrayMethodName: "Array.prototype.flatMap" } }] },
135143
{ code: "foo.flatMap(function foo() {})", errors: [{ messageId: "expectedInside", data: { name: "function 'foo'", arrayMethodName: "Array.prototype.flatMap" } }] },
136144
{ code: "foo.map(function() {})", errors: [{ messageId: "expectedInside", data: { name: "function", arrayMethodName: "Array.prototype.map" } }] },

0 commit comments

Comments
 (0)