Eslint-plugin-react: react/jsx-sort-props + {ignoreCase: false} now ignores case when sorting props

Created on 21 Dec 2019  Â·  10Comments  Â·  Source: yannickcr/eslint-plugin-react

I've found an unexpected(?) change that stems from #2391.

<Hello name="John" Number="2" />

With {ignoreCase: false}, which according to the docs should take case into account when sorting the props, the above example now expects name to be sorted before Number.

I know _why_ it sorts the way it does: #2391 switched to using String#localeCompare, which does…

> 'name'.localeCompare('Number')
-1

Looking at the lib/rules/jsx-sort-props.js code, I'm not sure what changes would be required to accommodate both this issue and #2381, unfortunately.

So, the crux of this issue is: is this an unexpected change, or should I alter my expectations and update the documentation to reflect this behavior?

bug help wanted

All 10 comments

This definitely seems like an unexpected change.

Specifically, we should probably only use localeCompare when ignoreCase is true?

@ljharb any news on this one?

No, "help wanted" means it's awaiting a PR.

We can't depend on localeCompare as it is not reliable.
Example:

Screenshot 2020-01-28 at 12 30 03 PM

Therefore, we need to replace localeCompare from line and line.

Hi @hawkrives ,
I have added a PR, Can you check the test cases?

@tanmoyopenroot we must depend on localeCompare whenever possible since english isn't the only language in the world.

@ljharb But each character in a string in any language will be compared in Unicode order for comparison operator.
In Unicode everything is ordered alphabetically.

If that were true, there'd be no point in the localeCompare method existing.

I will try to fix it using localeCompare.

Was this page helpful?
0 / 5 - 0 ratings