mskelton / eslint-plugin-sort

Auto-fixable sort rules for ESLint.
https://www.npmjs.com/package/eslint-plugin-sort
ISC License
24 stars 1 forks source link

Add case sensitivity and natural order options for the object properties rule #44

Closed mauricekleine closed 1 year ago

mauricekleine commented 1 year ago

First of all, why is this repo not more hyped? 🤩

Eslint's sort-keys rule supports options for case sensitivity and natural order. I personally rely on the natural order a lot for sorting Tailwind properties in a classname object, for example:

const classNames = clsx({
  "gap-1": gap === 1,
  "gap-2": gap === 2,
  // ...etc
 "gap-10": gap === 10
})

However, with the current default config for the object properties rule, the result would be the following:

const classNames = clsx({
  "gap-1": gap === 1,
  "gap-10": gap === 10,
  "gap-2": gap === 2,
  // ...etc
})

Having the option to set whether or not the rule should use natural sorting, would be a great addition so that the above object would sort numeric string in a 'natural' way using natural-compare

To keep it aligned with sort-key, I also added the option to choose between asc and desc orders

mauricekleine commented 1 year ago

@mskelton curious to hear your thoughts!

mauricekleine commented 1 year ago

Btw I forgot to mention, but these changes are backwards compatible. Not adding any of the new options results in the same results as before - this is also covered in the tests

mauricekleine commented 1 year ago

@mskelton makes sense, I'll add it to the other rules as well. I wasn't sure about setting the natural order as a default because it does change how people currently use the rule. If you're okay with that though, I'll set the default to true.

I'm actually thinking, should it in that case just always be natural order without having the ability to change the behaviour? So the only additional new config would be the caseSensitive boolean for each rule:

{
  "sort/object-properties": "error" // asc, natural order, case insensitive
}

or

{
  "sort/object-properties": ["error", { "caseSensitive": true, }] // asc, natural order, case sensitive
}
mskelton commented 1 year ago

I still think that natural should be an option, just enabled by default. It's possible that in certain circumstances such as different languages, natural order might not work well, so I would at least like it an option, even if enabled by default.

mauricekleine commented 1 year ago

@mskelton I implemented the case-sensitivity and natural order options for the other rules as well. Tests and docs are updated too. What do you think?

mauricekleine commented 1 year ago

Awesome, thanks for polishing it @mskelton!