Skip to content

feat(eslint-plugin): add require-array-sort-compare rule (fixes #247) #261

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

Merged
merged 4 commits into from
Feb 14, 2019

Conversation

mysticatea
Copy link
Member

Fixes #247.

This PR adds require-array-sort-compare rule that enforces giving compare argument to Array#sort() method.

@codecov
Copy link

codecov bot commented Feb 12, 2019

Codecov Report

Merging #261 into master will increase coverage by 0.03%.
The diff coverage is 100%.

@@            Coverage Diff             @@
##           master     #261      +/-   ##
==========================================
+ Coverage    94.6%   94.63%   +0.03%     
==========================================
  Files          65       66       +1     
  Lines        2778     2794      +16     
  Branches      720      723       +3     
==========================================
+ Hits         2628     2644      +16     
  Misses         57       57              
  Partials       93       93
Impacted Files Coverage Δ
...int-plugin/src/rules/require-array-sort-compare.ts 100% <100%> (ø)

Copy link
Member

@bradzacher bradzacher left a comment

Choose a reason for hiding this comment

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

requesting changes specifically for the vscode setting.

code otherwise LGTM

@bradzacher bradzacher added the enhancement: new plugin rule New rule request for eslint-plugin label Feb 12, 2019
@mysticatea
Copy link
Member Author

Thank you for the review!

I updated this PR.

Copy link
Member

@bradzacher bradzacher left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Collaborator

@armano2 armano2 left a comment

Choose a reason for hiding this comment

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

LGTM

@bradzacher
Copy link
Member

bradzacher commented Feb 13, 2019

Do we want to add an option ignoreStringArray (which is turned off by default), which allows people to ignore the string[] case?

I'm just thinking - I can see some people getting annoyed at this rule for two reasons:

  1. they like using the default function on string[], and don't want have to create a function which does the same thing as default (a,b) => a < b.
  2. String#localeCompare can sometimes be dangerous because it uses the javascript context's current locale - which can mean that depending on the user's settings you might produce an unintended sort order.

@mysticatea
Copy link
Member Author

  1. they like using the default function on string[], and don't want have to create a function which does the same thing as default (a,b) => a < b.

People can use undefined instead of a function as well.

  1. String#localeCompare can sometimes be dangerous because it uses the javascript context's current locale - which can mean that depending on the user's settings you might produce an unintended sort order.

String#localCompare has some options.
People can specify arbitary locale and options such as sensitivity, numeric, caseFirst, and etc.

I don't oppose the option, but I'm not sure if it's needed. I think, maybe, we can discuss it on another issue.

@bradzacher
Copy link
Member

bradzacher commented Feb 14, 2019

Figured it was worth raising just so we all thought about it.

I'm more happy to punt on it till someone asks for it :)

@bradzacher bradzacher merged commit 2a4aaaa into master Feb 14, 2019
@bradzacher bradzacher deleted the require-array-sort-compare/new branch February 14, 2019 01:17
@sindresorhus
Copy link

@mysticatea You forgot to add the rule to https://github.com/typescript-eslint/typescript-eslint/tree/master/packages/eslint-plugin#supported-rules

@bradzacher
Copy link
Member

weird... that should have been caught by the CI.

@armano2
Copy link
Collaborator

armano2 commented Feb 19, 2019

we are not running eslint-docs check anymore, we have to update it first (support for scoped packages) and add it to CI

@j-f1
Copy link
Contributor

j-f1 commented Feb 21, 2019

@armano2 I pushed the update that adds support for scoped packages (v0.4.0).

kaicataldo pushed a commit to kaicataldo/typescript-eslint that referenced this pull request Aug 27, 2019
Fixes typescript-eslint#144
Requires ~~typescript-eslint#259~~, ~~typescript-eslint#260~~.

- added a util to make it standardised and easier to add default config for a rule
- configured recommended based on typescript-eslint#144
  - purposely switched `recommended` prop to be `"error" | "warning" | false`
    - inside the eslint repo, it should be `true`. otherwise it's just a property that isn't used officially by eslint. It's truthy so `eslint-docs` still work.
  - changed recommended generator to accept `"error"`/`"warning"` for more configurability.
- adjusted default config of certain rules that didn't match our recommendations.
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rule Proposal: require-array-sort-compare
5 participants