Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: require-array-sort-compare + toSorted #8052

Merged
merged 2 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
35 changes: 19 additions & 16 deletions packages/eslint-plugin/src/rules/require-array-sort-compare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default createRule<Options, MessageIds>({
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: {
Expand Down Expand Up @@ -66,23 +66,26 @@ export default createRule<Options, MessageIds>({
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]":
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Non-Actionable] Aside: I'm glad you didn't increase the complexity of this selector 😂 I think it's at the maximum reasonable level to still be readable as-is...

Copy link
Contributor Author

@Zamiell Zamiell Dec 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol, that was because i simply do not know the selector syntax well enough to even try

checkSortArgument,
};
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
{
Expand Down Expand Up @@ -254,5 +261,13 @@ ruleTester.run('require-array-sort-compare', rule, {
errors: [{ messageId: 'requireCompare' }],
options: [{ ignoreStringArrays: true }],
},
{
code: `
function f(a: number[]) {
a.toSorted();
}
Zamiell marked this conversation as resolved.
Show resolved Hide resolved
`,
errors: [{ messageId: 'requireCompare' }],
},
],
});