-
-
Notifications
You must be signed in to change notification settings - Fork 893
feat(organizeImports): add sortBareImports option
#9384
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
cadf73d
feat(organizeImports): add ignoreBareImports option to control bare i…
Conaclos 91f1118
[autofix.ci] apply automated fixes
autofix-ci[bot] a94d874
refactor: rename option to sortBareImports
Conaclos ab9f0bb
fix: regression that sorted bare import followed by another import
Conaclos b9060b0
[autofix.ci] apply automated fixes
autofix-ci[bot] 7316afb
refactor: apply suggestions
Conaclos 7b8e6a0
refactor: shorten changeset as sugested
Conaclos 2e5bc87
[autofix.ci] apply automated fixes
autofix-ci[bot] f03d531
fix: typo
Conaclos 1210dff
Improve docs
Conaclos 78ac541
Revert "refactor: shorten changeset as sugested"
Conaclos fa981cf
refactor: shorten changeset
Conaclos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| --- | ||
| "@biomejs/biome": minor | ||
| --- | ||
|
|
||
| Added the `sortBareImports` option to [`organizeImports`](https://biomejs.dev/assist/actions/organize-imports/), | ||
| which allows bare imports to be sorted within other imports when set to `false`. | ||
|
|
||
| ```json | ||
| { | ||
| "assist": { | ||
| "actions": { | ||
| "source": { | ||
| "organizeImports": { | ||
| "level": "on", | ||
| "options": { "sortBareImports": true } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ```diff | ||
| - import "b"; | ||
| import "a"; | ||
| + import "b"; | ||
| import { A } from "a"; | ||
| + import "./file"; | ||
| import { Local } from "./file"; | ||
| - import "./file"; | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
crates/biome_js_analyze/tests/specs/source/organizeImports/chunk-because-of-bare-imports.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| /* should not generate diagnostics */ | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
|
||
| import "b"; | ||
| import { A } from "a"; | ||
12 changes: 12 additions & 0 deletions
12
...biome_js_analyze/tests/specs/source/organizeImports/chunk-because-of-bare-imports.js.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| --- | ||
| source: crates/biome_js_analyze/tests/spec_tests.rs | ||
| expression: chunk-because-of-bare-imports.js | ||
| --- | ||
| # Input | ||
| ```js | ||
| /* should not generate diagnostics */ | ||
|
|
||
| import "b"; | ||
| import { A } from "a"; | ||
|
|
||
| ``` |
4 changes: 4 additions & 0 deletions
4
crates/biome_js_analyze/tests/specs/source/organizeImports/custom-order-with-bare-imports.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| import * as path from "node:path"; | ||
| import "node:path"; | ||
| import "./file.js"; | ||
| import { A } from "./file.js"; |
38 changes: 38 additions & 0 deletions
38
...iome_js_analyze/tests/specs/source/organizeImports/custom-order-with-bare-imports.js.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| --- | ||
| source: crates/biome_js_analyze/tests/spec_tests.rs | ||
| expression: custom-order-with-bare-imports.js | ||
| --- | ||
| # Input | ||
| ```js | ||
| import * as path from "node:path"; | ||
| import "node:path"; | ||
| import "./file.js"; | ||
| import { A } from "./file.js"; | ||
|
|
||
| ``` | ||
|
|
||
| # Diagnostics | ||
| ``` | ||
| custom-order-with-bare-imports.js:1:1 assist/source/organizeImports FIXABLE ━━━━━━━━━━━━━━━━━━━━━━ | ||
|
|
||
| i The imports and exports are not sorted. | ||
|
|
||
| > 1 │ import * as path from "node:path"; | ||
| │ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| 2 │ import "node:path"; | ||
| 3 │ import "./file.js"; | ||
|
|
||
| i Safe fix: Organize Imports (Biome) | ||
|
|
||
| 1 │ - import·*·as·path·from·"node:path"; | ||
| 2 │ - import·"node:path"; | ||
| 3 │ - import·"./file.js"; | ||
| 4 │ - import·{·A·}·from·"./file.js"; | ||
| 1 │ + import·"./file.js"; | ||
| 2 │ + import·{·A·}·from·"./file.js"; | ||
| 3 │ + import·"node:path"; | ||
| 4 │ + import·*·as·path·from·"node:path"; | ||
| 5 5 │ | ||
|
|
||
|
|
||
| ``` |
19 changes: 19 additions & 0 deletions
19
...js_analyze/tests/specs/source/organizeImports/custom-order-with-bare-imports.options.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| { | ||
| "$schema": "../../../../../../packages/@biomejs/biome/configuration_schema.json", | ||
| "assist": { | ||
| "actions": { | ||
| "source": { | ||
| "organizeImports": { | ||
| "level": "on", | ||
| "options": { | ||
| "groups": [ | ||
| ":PATH:", | ||
| ":NODE:" | ||
| ], | ||
| "sortBareImports": true | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
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_itemto returnNoneifsortBareImportsis false/unset and returns the info if it istrue. However, this led to some code duplication in theelsebranch: we have to check whether the attributes of bare imports are sorted.Thus, I decided to keep the current approach.