Skip to content

Commit 9b2fcf7

Browse files
author
SUZUKI Sosuke
authored
feat: array-callback-return supports Array.prototype.toSorted (#16845)
* feat: support `toSorted` * test: add tests for `toSorted` * docs: update docs for `toSorted` * docs: update the spec URL
1 parent 71349a1 commit 9b2fcf7

3 files changed

Lines changed: 7 additions & 1 deletion

File tree

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ This rule finds callback functions of the following methods, then checks usage o
3535
* [`Array.prototype.reduceRight`](https://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.reduceright)
3636
* [`Array.prototype.some`](https://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.some)
3737
* [`Array.prototype.sort`](https://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.sort)
38+
* [`Array.prototype.toSorted`](https://tc39.es/proposal-change-array-by-copy/#sec-array.prototype.toSorted)
3839
* And above of typed arrays.
3940

4041
Examples of **incorrect** code for this rule:

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(?:Last)?(?: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|toSorted)$/u;
2020

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

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ ruleTester.run("array-callback-return", rule, {
6666
"foo.reduceRight(function() { return true; })",
6767
"foo.some(function() { return true; })",
6868
"foo.sort(function() { return 0; })",
69+
"foo.toSorted(function() { return 0; })",
6970
{ code: "foo.every(() => { return true; })", parserOptions: { ecmaVersion: 6 } },
7071
"foo.every(function() { if (a) return true; else return false; })",
7172
"foo.every(function() { switch (a) { case 0: bar(); default: return true; } })",
@@ -88,6 +89,7 @@ ruleTester.run("array-callback-return", rule, {
8889
{ code: "foo.reduceRight(function() { return; })", options: allowImplicitOptions },
8990
{ code: "foo.some(function() { return; })", options: allowImplicitOptions },
9091
{ code: "foo.sort(function() { return; })", options: allowImplicitOptions },
92+
{ code: "foo.toSorted(function() { return; })", options: allowImplicitOptions },
9193
{ code: "foo.every(() => { return; })", options: allowImplicitOptions, parserOptions: { ecmaVersion: 6 } },
9294
{ code: "foo.every(function() { if (a) return; else return a; })", options: allowImplicitOptions },
9395
{ code: "foo.every(function() { switch (a) { case 0: bar(); default: return; } })", options: allowImplicitOptions },
@@ -151,6 +153,8 @@ ruleTester.run("array-callback-return", rule, {
151153
{ code: "foo.some(function foo() {})", errors: [{ messageId: "expectedInside", data: { name: "function 'foo'", arrayMethodName: "Array.prototype.some" } }] },
152154
{ code: "foo.sort(function() {})", errors: [{ messageId: "expectedInside", data: { name: "function", arrayMethodName: "Array.prototype.sort" } }] },
153155
{ code: "foo.sort(function foo() {})", errors: [{ messageId: "expectedInside", data: { name: "function 'foo'", arrayMethodName: "Array.prototype.sort" } }] },
156+
{ code: "foo.toSorted(function() {})", errors: [{ messageId: "expectedInside", data: { name: "function", arrayMethodName: "Array.prototype.toSorted" } }] },
157+
{ code: "foo.toSorted(function foo() {})", errors: [{ messageId: "expectedInside", data: { name: "function 'foo'", arrayMethodName: "Array.prototype.toSorted" } }] },
154158
{ code: "foo.bar.baz.every(function() {})", errors: [{ messageId: "expectedInside", data: { name: "function", arrayMethodName: "Array.prototype.every" } }] },
155159
{ code: "foo.bar.baz.every(function foo() {})", errors: [{ messageId: "expectedInside", data: { name: "function 'foo'", arrayMethodName: "Array.prototype.every" } }] },
156160
{ code: "foo[\"every\"](function() {})", errors: [{ messageId: "expectedInside", data: { name: "function", arrayMethodName: "Array.prototype.every" } }] },
@@ -190,6 +194,7 @@ ruleTester.run("array-callback-return", rule, {
190194
{ code: "foo.bar.baz.every(function foo() {})", options: allowImplicitOptions, errors: [{ messageId: "expectedInside", data: { name: "function 'foo'", arrayMethodName: "Array.prototype.every" } }] },
191195
{ code: "foo.every(cb || function() {})", options: allowImplicitOptions, errors: ["Array.prototype.every() expects a return value from function."] },
192196
{ code: "[\"foo\",\"bar\"].sort(function foo() {})", options: allowImplicitOptions, errors: [{ messageId: "expectedInside", data: { name: "function 'foo'", arrayMethodName: "Array.prototype.sort" } }] },
197+
{ code: "[\"foo\",\"bar\"].toSorted(function foo() {})", options: allowImplicitOptions, errors: [{ messageId: "expectedInside", data: { name: "function 'foo'", arrayMethodName: "Array.prototype.toSorted" } }] },
193198
{ code: "foo.forEach(x => x)", options: allowImplicitCheckForEach, parserOptions: { ecmaVersion: 6 }, errors: [{ messageId: "expectedNoReturnValue", data: { name: "arrow function", arrayMethodName: "Array.prototype.forEach" } }] },
194199
{ code: "foo.forEach(function(x) { if (a == b) {return x;}})", options: allowImplicitCheckForEach, errors: [{ messageId: "expectedNoReturnValue", data: { name: "function", arrayMethodName: "Array.prototype.forEach" } }] },
195200
{ code: "foo.forEach(function bar(x) { return x;})", options: allowImplicitCheckForEach, errors: [{ messageId: "expectedNoReturnValue", data: { name: "function 'bar'", arrayMethodName: "Array.prototype.forEach" } }] },

0 commit comments

Comments
 (0)