-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
[New] order
: add caseInsensitive: 'invert'
option
#1740
base: main
Are you sure you want to change the base?
Conversation
My current workaround which works for ~80% of the instances in the target codebase is const rules = {
...
'import/order': [
'warn',
{
groups: ['builtin', 'external', 'internal', 'parent', 'sibling', 'index'],
alphabetize: {
order: 'asc',
caseInsensitive: true,
},
pathGroups: [
{ pattern: './[A-Z]*{,/**}', group: 'sibling', position: 'after' },
],
},
],
...
} |
2 similar comments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code seems fine to me. Can you elaborate on the use case?
6013f1b
to
bc8390f
Compare
Sure: the use case is that in the frontend code at $job, data/core modules start with a lower case, and the Components start with UpperCase, and so, when i'm sorting imports, i want the business logic imports to be first, and then the imports of Presentation Logic. (sorry for the late reply, got lost in my notifications) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM overall, just a few comments
@@ -211,12 +211,12 @@ import index from './'; | |||
import sibling from './foo'; | |||
``` | |||
|
|||
### `alphabetize: {order: asc|desc|ignore, caseInsensitive: true|false}`: | |||
### `alphabetize: {order: asc|desc|ignore, caseInsensitive: true|false|'invert'}`: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
bikeshed: maybe in addition to true/false, we could support (and recommend) a three-option string enum? iow, this can be 'ignore' | 'upperFirst' | 'lowerFirst'
, with false
being an alias for ignore
and true
being an alias for upperFirst
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, perhaps the "caseInsensitive" should be deprecated and make the 3 option string enum under an option called "caseSensitive" or "caseSensitivity"?
i wonder what other things call their options?
- GNU coreutils
sort
calls its option--ignore-case
/-f
(the f is for "fold lower case to upper case" - maybe too obscure of a term to use here)- although python has
str.casefold()
, algorithm defined in unicode spec)
- although python has
- JS
Intl.locale.caseFirst
andsensitivity
(under options) - JS
regExp.ignoreCase
- @typescript-eslint/eslint-plugin has
sort-type-union-intersection-members
(non-configurable) - eslint core has
sort-vars
hasignoreCase
- eslint
sort-vars
andsort-imports
haveignoreCase
- eslint
sort-keys
hascaseSensitive
andnatural
With that all said, i'd like to suggest keeping caseInsensitive
as a boolean, and for this option, introduce caseFirst: 'upper' | 'lower'
, based on the Intl.Collator
options
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would probably also be a good idea to just use Intl.Collator
like what typescript-eslint does instead of doing the case-munging manually
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
alternative diff, using caseFirst
is here: master...forivall:feat/case-first
src/rules/order.js
Outdated
const sorterFn = getSorter(alphabetizeOptions.order === 'asc') | ||
const comparator = alphabetizeOptions.caseInsensitive ? (a, b) => sorterFn(String(a).toLowerCase(), String(b).toLowerCase()) : (a, b) => sorterFn(a, b) | ||
const comparator = | ||
alphabetizeOptions.caseInsensitive === 'invert' ? (a, b) => sorterFn(swapCase(String(a)), swapCase(String(b))) | ||
: alphabetizeOptions.caseInsensitive ? (a, b) => sorterFn(String(a).toLowerCase(), String(b).toLowerCase()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seems like this should be handled inside getSorter
rather than here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resolved with my new refactor commit.
5b68e4f
to
3e1d204
Compare
3e1d204
to
f8eabbd
Compare
Adds the "invert" option to "caseInsensitive"
caseInsensitive
: usetrue
to ignore case,false
to consider case, and'invert'
to sort lowercase before uppercase (default:false
).bikeshed: I'd rename
caseInsensitive
tocaseSensitive
with defaulttrue
. other naming options are viable.