refactor: improve sorting override framework #617
Draft
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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?