Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
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
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -27,7 +27,7 @@ export default createRule<Options, MessageIds>({ | ||
type: 'problem', | ||
docs: { | ||
description: | ||
'Require `Array#sort`and `Array#toSorted`calls to always provide a `compareFunction`', | ||
requiresTypeChecking: true, | ||
}, | ||
messages: { | ||
@@ -66,23 +66,26 @@ export default createRule<Options, MessageIds>({ | ||
return false; | ||
} | ||
function checkSortArgument(callee: TSESTree.MemberExpression): void { | ||
const calleeObjType = getConstrainedTypeAtLocation( | ||
services, | ||
callee.object, | ||
); | ||
if (options.ignoreStringArrays && isStringArrayNode(callee.object)) { | ||
return; | ||
} | ||
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]": | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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... ContributorAuthor There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
---|---|---|
@@ -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(); | ||
} | ||
Zamiell marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
`, | ||
errors: [{ messageId: 'requireCompare' }], | ||
}, | ||
], | ||
}); |