-
Notifications
You must be signed in to change notification settings - Fork 55
refactor: improve sorting override framework #617
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
base: next
Are you sure you want to change the base?
Conversation
|
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
92c97c3 to
9130c26
Compare
9130c26 to
6ecf3ff
Compare
e182a95 to
8fc1966
Compare
- This is really just `compare` functions, split in different files.
…files - This file exports 2 functions. - `buildDefaultOptionsByGroupIndexComputer`. - `getCustomGroupsCompareOptions` ➡️ `computeOverriddenOptionsByGroupIndex`.
- Replace `nodeValueGetter` and `fallbackSortNodeValueGetter` with `comparatorByOptionsComputer`.
- Same thing as previous commit. - Replace `nodeValueGetter` and `fallbackSortNodeValueGetter` with `comparatorByOptionsComputer`.
8fc1966 to
6a4d984
Compare
Description
Our current sorting system relies on the following:
alphabetical,natural,line-length,custom).SortingNode'sname.nodeValueGetterandfallbackSortNodeValueGetter.The problem
nodeValueGetterandfallbackSortNodeValueGetterare now lacking for future use cases.With #593, we would like to be able to add a fallback sort which logic is not convenient to represent with just those two properties:
The two following imports are equal with the
alphabeticalandnaturalsorts in this case:import { a } from "a".import type { Type } from "a".In order to be able to place the
typeimport first with the current options we have, we would need to make sure that the first import'snamecomes before the second one.This can be achieved with a
nodeValueGetterthat transforms the import's name toaaand the second toab, but this isn't really clean.Solution
Rather than allowing rules to pass
nodeValueGetterandfallbackSortNodeValueGetter, allow them to directly pass comparison functions. Those comparison functions can be computed from options.For most rules, the comparison functions to pass are the default ones (alphabetical sorting, natural sorting, etc).
For rules that allow sorting by value (
sort-enumsfor example), we simply compute an alphabetical/natural sorting function that takes the node's value field instead of name, and pass those tosort-nodes/sort-nodes-by-groups.For #593, we simply need to create and pass a comparison function that takes two imports, and gives priority to the one with
type.How it looks
🧑💻 The default comparison functions passed
🧑💻 The comparison functions for `sort-enums` (which allows sorting by value)
What is the purpose of this pull request?