Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
JoshuaKGoldberg merged 2 commits intotypescript-eslint:mainfromZamiell:to-sorted
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionspackages/eslint-plugin/src/rules/require-array-sort-compare.ts
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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]":

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
ContributorAuthor

@ZamiellZamiellDec 12, 2023
edited
Loading

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

JoshuaKGoldberg reacted with laugh emoji
checkSortArgument,
};
},
});
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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();
}
`,
errors: [{ messageId: 'requireCompare' }],
},
],
});

[8]ページ先頭

©2009-2025 Movatter.jp