Skip to content

Rule Proposal: require-array-sort-compare #247

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

Closed
mysticatea opened this issue Feb 11, 2019 · 2 comments · Fixed by #261
Closed

Rule Proposal: require-array-sort-compare #247

mysticatea opened this issue Feb 11, 2019 · 2 comments · Fixed by #261
Assignees
Labels
enhancement: new plugin rule New rule request for eslint-plugin package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@mysticatea
Copy link
Member

Please describe what the rule should do:

From eslint/eslint#10363

This rule reports xs.sort() calls with no arguments if xs is an array.

let xs: any[]

//✘ BAD
xs.sort()

//✔ GOOD
xs.sort((a, b) => a - b)
xs.sort((a, b) => a.localeCompare(b))
xs.sort(cmp)
xs.sort(undefined) //← explicit default behavior

Because the default compare function does compare the array elements as strings then it can cause unexpected behavior. For example:

[1, 2, 10].sort() //→ it becomes [1, 10, 2]

The language spec describes this matter.

NOTE 2: Method calls performed by the ToString abstract operations in steps 5 and 7 have the potential to cause SortCompare to not behave as a consistent comparison function.
https://www.ecma-international.org/ecma-262/8.0/#sec-sortcompare

Because the rule doesn't want to warn the sort() method of non-array class, I think that this rule fits to this plugin.

What category of rule is this? (place an "X" next to just one item)

[ ] Enforces code style
[X] Warns about a potential error
[ ] Suggests an alternate way of doing something
[ ] Other (please specify:)

Provide 2-3 code examples that this rule will warn about:

let xs: any[]
let ys: ReadonlyArray<any>
let zs: { sort(): void }

//✘ BAD
xs.sort()
ys.sort()

//✔ GOOD
xs.sort((a, b) => a - b)
ys.sort((a, b) => a - b)
zs.sort() //← this is not an array.
@mysticatea mysticatea added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Feb 11, 2019
@bradzacher bradzacher added enhancement: new plugin rule New rule request for eslint-plugin and removed triage Waiting for team members to take a look labels Feb 11, 2019
@mysticatea mysticatea self-assigned this Feb 12, 2019
@aboyton
Copy link
Contributor

aboyton commented Feb 13, 2019

If you have an array of strings is it fine to call .sort() with no arguments?

@mysticatea
Copy link
Member Author

mysticatea commented Feb 13, 2019

In the the discussion in the original issue eslint/eslint#10363, there was a consensus: it should report string array as well in favor of String#localeCompare method.

kaicataldo pushed a commit to kaicataldo/typescript-eslint that referenced this issue Aug 27, 2019
This PR enables `eslint-plugin/require-meta-type` rule, and adds type field to all existing rules.

fixes: typescript-eslint#216
@typescript-eslint typescript-eslint locked as resolved and limited conversation to collaborators Feb 21, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement: new plugin rule New rule request for eslint-plugin package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
3 participants