diff --git a/packages/eslint-plugin/docs/rules/require-array-sort-compare.md b/packages/eslint-plugin/docs/rules/require-array-sort-compare.md index d97e78e9aa0a..8bb7d3fe9e03 100644 --- a/packages/eslint-plugin/docs/rules/require-array-sort-compare.md +++ b/packages/eslint-plugin/docs/rules/require-array-sort-compare.md @@ -6,7 +6,7 @@ description: 'Require `Array#sort` calls to always provide a `compareFunction`.' > > See **https://typescript-eslint.io/rules/require-array-sort-compare** for documentation. -When called without a compare function, `Array#sort()` converts all non-undefined array elements into strings and then compares said strings based off their UTF-16 code units [[ECMA specification](https://www.ecma-international.org/ecma-262/9.0/#sec-sortcompare)]. +When called without a compare function, `Array#sort()` and `Array#toSorted()` converts all non-undefined array elements into strings and then compares said strings based off their UTF-16 code units [[ECMA specification](https://www.ecma-international.org/ecma-262/9.0/#sec-sortcompare)]. The result is that elements are sorted alphabetically, regardless of their type. For example, when sorting numbers, this results in a "10 before 2" order: diff --git a/packages/eslint-plugin/src/rules/require-array-sort-compare.ts b/packages/eslint-plugin/src/rules/require-array-sort-compare.ts index 1b2916ea0b48..fe246a6d8bf2 100644 --- a/packages/eslint-plugin/src/rules/require-array-sort-compare.ts +++ b/packages/eslint-plugin/src/rules/require-array-sort-compare.ts @@ -27,7 +27,7 @@ export default createRule({ type: 'problem', docs: { description: - 'Require `Array#sort` calls to always provide a `compareFunction`', + 'Require `Array#sort` and `Array#toSorted` calls to always provide a `compareFunction`', requiresTypeChecking: true, }, messages: { @@ -66,23 +66,26 @@ export default createRule({ return false; } - return { - "CallExpression[arguments.length=0] > MemberExpression[property.name='sort'][computed=false]"( - callee: TSESTree.MemberExpression, - ): void { - const calleeObjType = getConstrainedTypeAtLocation( - services, - callee.object, - ); + function checkSortArgument(callee: TSESTree.MemberExpression): void { + const calleeObjType = getConstrainedTypeAtLocation( + services, + callee.object, + ); - if (options.ignoreStringArrays && isStringArrayNode(callee.object)) { - return; - } + if (options.ignoreStringArrays && isStringArrayNode(callee.object)) { + return; + } - if (isTypeArrayTypeOrUnionOfArrayTypes(calleeObjType, checker)) { - context.report({ node: callee.parent, messageId: 'requireCompare' }); - } - }, + if (isTypeArrayTypeOrUnionOfArrayTypes(calleeObjType, checker)) { + context.report({ node: callee.parent, messageId: 'requireCompare' }); + } + } + + return { + "CallExpression[arguments.length=0] > MemberExpression[property.name='sort'][computed=false]": + checkSortArgument, + "CallExpression[arguments.length=0] > MemberExpression[property.name='toSorted'][computed=false]": + checkSortArgument, }; }, }); diff --git a/packages/eslint-plugin/tests/rules/require-array-sort-compare.test.ts b/packages/eslint-plugin/tests/rules/require-array-sort-compare.test.ts index 771adedb1418..3d544d7f2ce9 100644 --- a/packages/eslint-plugin/tests/rules/require-array-sort-compare.test.ts +++ b/packages/eslint-plugin/tests/rules/require-array-sort-compare.test.ts @@ -126,6 +126,13 @@ ruleTester.run('require-array-sort-compare', rule, { `, options: [{ ignoreStringArrays: true }], }, + { + code: ` + function f(a: number[]) { + a.toSorted((a, b) => a - b); + } + `, + }, ], invalid: [ { @@ -254,5 +261,13 @@ ruleTester.run('require-array-sort-compare', rule, { errors: [{ messageId: 'requireCompare' }], options: [{ ignoreStringArrays: true }], }, + { + code: ` + function f(a: number[]) { + a.toSorted(); + } + `, + errors: [{ messageId: 'requireCompare' }], + }, ], }); pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy