formatter: sort-imports
Part of #13610, tracking issue for sorting ImportDeclaration.
Phase 0: Foundation parts
- [x] Research prior arts
- Use
@perfectionist/sort-importsas a reference - https://perfectionist.dev/rules/sort-imports
- Use
- [x] Decide how to integrate it to current formatter workflow
- Reorder IR, after formatting
- [x] Implement very basic sorting
- [x] Setup tests and verify current architecture
Phase 1: Features behind default options
- [ ] Fix
options.typedefault alphabetical, not properly implementedlocaleCompare - [x]
options.partitionByComments(boolean only) - [x]
options.partitionByNewline - [x]
options.sortSideEffects - [x]
options.order - [x]
options.ignoreCase - [x]
options.newlinesBetween - [ ]
options.groups - [ ] Clean up comments, credits, and past trials
- [ ] Investigate perf improvement with this benchmark result
- [ ] Align default
groupswithprettier-plugin-sort-importsdefault?
Phase 2: More advanced options, features
- [ ] Perf: Skip sorting if
ImportDeclarationnot found - [ ] Handle ignore comments
- [ ]
options.customGroups - [ ] Consider to support or not
options.internalPatternoptions.fallbackSortoptions.environmentoptions.specialCharactersoptions.type: natural, unsortedoptions.localesoptions.partitionByComments(non-boolean)- Support
ImportEquals,require()?
It would be great if oxc could automatically merge imports from the same source. For example:
import { A } from 'package';
import { B } from 'package';
could be merged into:
import { A, B } from 'package';
Maybe this could be added to the roadmap?
Thank you for your feedback!
I don't think it's impossible, but please let us to consider it first. š In this issue, I will focus solely on sorting.
It would be great if oxc could automatically merge imports from the same source. For example:
import { A } from 'package'; import { B } from 'package'; could be merged into:
import { A, B } from 'package'; Maybe this could be added to the roadmap?
Shouldn't this be part of linter? I see they're actively doing support for eslint-plugin-import in the linter. It does it out of the box.
@binarybaba Iād prefer having this in the formatter, since (i think) the linter should focus on actual code problems ā or things that could potentially become problems ā and separate imports are still valid code.
Maybe we should move this discussion to a separate issue or a GitHub discussion, so we can keep this thread focused on import sorting.
I'm giving a try to the new [email protected] and I'm not sure if I'm suppose to open a new issue since this is not even Beta (if so, let me know). I see this sort imports seems incorrect since I see it moved all my local imports to the top even though I expected them to be at the bottom (and also expected a new line to separate external/local imports)
Below is the diff with @ianvs/prettier-plugin-sort-imports on the left and oxfmt on the right
Here's my .oxfmtrc.json
{
"$schema": "./node_modules/oxfmt/configuration_schema.json",
"indentStyle": "space",
"indentWidth": 2,
"lineWidth": 130,
"lineEnding": "lf",
"quoteStyle": "single",
"trailingCommas": "es5",
"semicolons": "always",
"expand": "auto",
"experimentalSortImports": {
"partitionByNewline": true,
"partitionByComment": false,
"sortSideEffects": false,
"order": "asc",
"ignoreCase": true
}
}
Thank you for trying and reporting!
this is not even Beta
Yes, it's simply not implemented yet. š
options.groups
So please bear with us a little longer.