Skip to content

feat(organizeImports): add sortBareImports option#9384

Merged
Conaclos merged 12 commits intonextfrom
conaclos/organizeImports-bare-import-sorting
Mar 8, 2026
Merged

feat(organizeImports): add sortBareImports option#9384
Conaclos merged 12 commits intonextfrom
conaclos/organizeImports-bare-import-sorting

Conversation

@Conaclos
Copy link
Member

@Conaclos Conaclos commented Mar 7, 2026

Summary

This PR adds a new option ignoreBareImports sortBareImports that allows bare imports to be sorted within other imports when set to false true.
Example:

{
  "assist": {
    "actions": {
      "source": {
        "organizeImports": {
          "level": "on",
          "options": { "sortBareImports": true }
        }
      }
    }
  }
}
- import "b";
  import "a";
+ import "b";
  import { A } from "a";
+ import "./file";
  import { Local } from "./file";
- import "./file";

As you noticed, I chose to not merge bare imports with other imports because a bare import signals a side effect. Merging them could remove this signal.

I am a bit hesitant about the option name.
I chose ignoreBareImports. However, bare imports are not completely ignored because we sort their attributes.
An alternative name could be sortBareImports. Any opinion?

EDIT: I switched to sortBareImports. This looks better to me.

Note that this PR doesn't address https://github.com/biomejs/bioze/discussions/7811 yet.
I plan to submit a future PR that allows matching against bare imports.
This will require to set ignoreBareImports to false.

The PR fixed also a small bug: import attributes of bare imports were not sorted.

Test Plan

I added several tests to cover the new option.

Docs

I updated the rule docs and I added a changeset.
Also I added doc-comment to the options.

@changeset-bot
Copy link

changeset-bot bot commented Mar 7, 2026

🦋 Changeset detected

Latest commit: fa981cf

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 14 packages
Name Type
@biomejs/biome Minor
@biomejs/cli-win32-x64 Minor
@biomejs/cli-win32-arm64 Minor
@biomejs/cli-darwin-x64 Minor
@biomejs/cli-darwin-arm64 Minor
@biomejs/cli-linux-x64 Minor
@biomejs/cli-linux-arm64 Minor
@biomejs/cli-linux-x64-musl Minor
@biomejs/cli-linux-arm64-musl Minor
@biomejs/wasm-web Minor
@biomejs/wasm-bundler Minor
@biomejs/wasm-nodejs Minor
@biomejs/backend-jsonrpc Patch
@biomejs/js-api Major

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions github-actions bot added A-Linter Area: linter L-JavaScript Language: JavaScript and super languages labels Mar 7, 2026
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 7, 2026

Caution

Review failed

Pull request was closed or merged during review

Walkthrough

Adds a new organizeImports option sortBareImports and implements sorting and chunking behaviour for bare (side‑effect) imports. Introduces ImportStatementKind::Bare, shifts bitflag positions for other import kinds, and exposes public modules import_key and specifiers_attributes while making util private. Updates chunk boundary, sorting and merge logic to account for bare imports, updates documentation, and adds tests and options covering mixed and unsorted bare‑import scenarios. No existing public function signatures were removed.

Possibly related PRs

Suggested reviewers

  • ematipico
  • dyc3
🚥 Pre-merge checks | ✅ 2
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately summarises the main change: adding a new sortBareImports option to organizeImports, which is the central feature of this PR.
Description check ✅ Passed The description is directly related to the changeset, explaining the new option, its behaviour, configuration examples, and the reasoning behind design decisions.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch conaclos/organizeImports-bare-import-sorting

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot]

This comment was marked as resolved.

coderabbitai[bot]

This comment was marked as resolved.

@Conaclos Conaclos changed the base branch from main to next March 7, 2026 12:08
@Conaclos Conaclos force-pushed the conaclos/organizeImports-bare-import-sorting branch from 05f6776 to 887b702 Compare March 7, 2026 12:13
coderabbitai[bot]

This comment was marked as resolved.

@Conaclos Conaclos force-pushed the conaclos/organizeImports-bare-import-sorting branch 2 times, most recently from 8a3a783 to cadf73d Compare March 7, 2026 12:15
coderabbitai[bot]

This comment was marked as resolved.

@codspeed-hq
Copy link

codspeed-hq bot commented Mar 7, 2026

Merging this PR will not alter performance

✅ 58 untouched benchmarks
⏩ 156 skipped benchmarks1


Comparing conaclos/organizeImports-bare-import-sorting (fa981cf) with next (271a437)

Open in CodSpeed

Footnotes

  1. 156 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@ematipico
Copy link
Member

An alternative name could be sortBareImports. Any opinion?

I like it!

@Conaclos Conaclos changed the title feat(organizeImports): add ignoreBareImports to control bare import sort feat(organizeImports): add sortBareImports to control bare import sort Mar 8, 2026
let sort_bare_imports = options.sort_bare_imports.unwrap_or_default();
let mut chunk: Option<ChunkBuilder> = None;
let mut prev_kind: Option<JsSyntaxKind> = None;
let mut prev_is_bare_import = false;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not fond of this new variable. However, the alternative approach I have in mind is not necessarily better: The alternative implementation is to modify ImportInfo::from_module_item to return None if sortBareImports is false/unset and returns the info if it is true. However, this led to some code duplication in the else branch: we have to check whether the attributes of bare imports are sorted.

Thus, I decided to keep the current approach.

coderabbitai[bot]

This comment was marked as resolved.

@Conaclos Conaclos requested a review from a team March 8, 2026 11:11
@@ -0,0 +1,4 @@
/* should not generate diagnostics */
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a non-regression test: While I was implementing the feature I introduced a bug that was not covered by the test suite. This test covers it.

@Conaclos
Copy link
Member Author

Conaclos commented Mar 8, 2026

The lint rule docs CI task fails. However, this seems to come from other rules. Thus, I think we can ignore this failure.

@Conaclos Conaclos changed the title feat(organizeImports): add sortBareImports to control bare import sort feat(organizeImports): add sortBareImports option Mar 8, 2026
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@crates/biome_js_analyze/src/assist/source/organize_imports.rs`:
- Line 67: Update the doc comment that documents sortBareImports so the sentence
starts with a capital letter: change "when `sortBareImports` is unset or
`false`, every bare import forms an independent chunk." to "When
`sortBareImports` is unset or `false`, every bare import forms an independent
chunk." in the organize_imports.rs documentation for sortBareImports.
- Line 674: Fix the typo in the documentation comment in organize_imports.rs
where the string "Yo ucan opt for a `lexicographic` sort (sometimes referred as
_binary_ sort) by" contains a stray space; change "Yo ucan" to "You can" in that
doc comment (search for the exact phrase "Yo ucan opt for a `lexicographic`
sort" to locate the comment).

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 21306b67-39f2-42ed-98af-479c5a1d0bfd

📥 Commits

Reviewing files that changed from the base of the PR and between ab9f0bb and 2e5bc87.

⛔ Files ignored due to path filters (2)
  • packages/@biomejs/backend-jsonrpc/src/workspace.ts is excluded by !**/backend-jsonrpc/src/workspace.ts and included by **
  • packages/@biomejs/biome/configuration_schema.json is excluded by !**/configuration_schema.json and included by **
📒 Files selected for processing (4)
  • .changeset/flat-beers-battle.md
  • crates/biome_js_analyze/src/assist/source/organize_imports.rs
  • crates/biome_js_analyze/tests/specs/source/organizeImports/custom-order-with-bare-imports.options.json
  • crates/biome_rule_options/src/organize_imports.rs
🚧 Files skipped from review as they are similar to previous changes (2)
  • .changeset/flat-beers-battle.md
  • crates/biome_js_analyze/tests/specs/source/organizeImports/custom-order-with-bare-imports.options.json

@Conaclos Conaclos merged commit f4c9edc into next Mar 8, 2026
14 of 16 checks passed
@Conaclos Conaclos deleted the conaclos/organizeImports-bare-import-sorting branch March 8, 2026 16:58
Hideart added a commit to Hideart/biome that referenced this pull request Mar 9, 2026
)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Hideart added a commit to Hideart/biome that referenced this pull request Mar 9, 2026
)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-Linter Area: linter L-JavaScript Language: JavaScript and super languages

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants