oxc icon indicating copy to clipboard operation
oxc copied to clipboard

formatter: sort-imports

Open leaysgur opened this issue 2 months ago • 6 comments

Part of #13610, tracking issue for sorting ImportDeclaration.

Phase 0: Foundation parts

  • [x] Research prior arts
    • Use @perfectionist/sort-imports as a reference
    • https://perfectionist.dev/rules/sort-imports
  • [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.type default alphabetical, not properly implemented localeCompare
  • [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 groups with prettier-plugin-sort-imports default?

Phase 2: More advanced options, features

  • [ ] Perf: Skip sorting if ImportDeclaration not found
  • [ ] Handle ignore comments
  • [ ] options.customGroups
  • [ ] Consider to support or not
    • options.internalPattern
    • options.fallbackSort
    • options.environment
    • options.specialCharacters
    • options.type: natural, unsorted
    • options.locales
    • options.partitionByComments(non-boolean)
    • Support ImportEquals, require()?

leaysgur avatar Sep 30 '25 06:09 leaysgur

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?

bc-m avatar Sep 30 '25 14:09 bc-m

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.

leaysgur avatar Oct 01 '25 06:10 leaysgur

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 avatar Oct 01 '25 13:10 binarybaba

@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.

bc-m avatar Oct 01 '25 13:10 bc-m

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

Image

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
  }
}

ghiscoding avatar Oct 17 '25 01:10 ghiscoding

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.

leaysgur avatar Oct 17 '25 01:10 leaysgur