Skip to content

Commit 72623fa

Browse files
authored
fix(assist/organizeImports): don't duplicate header file comment (#6511)
1 parent 9a8fe0f commit 72623fa

File tree

5 files changed

+75
-4
lines changed

5 files changed

+75
-4
lines changed

.changeset/four-phones-shout.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@biomejs/biome": patch
3+
---
4+
5+
Fixed [#6492](https://github.com/biomejs/biome/issues/6492). The
6+
`organizeImports` assist action no longer duplicates a comment at the start of
7+
the file when `:BLANK_LINE:` precedes the first import group.

crates/biome_js_analyze/src/assist/source/organize_imports.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -945,9 +945,11 @@ impl Rule for OrganizeImports {
945945
};
946946
let mut new_item = new_item.into_syntax();
947947
let old_item = old_item.into_node()?;
948-
let blank_line_separated_groups = options
949-
.groups
950-
.separated_by_blank_line(prev_group, key.group);
948+
let blank_line_separated_groups = index != 0
949+
&& options
950+
.groups
951+
.separated_by_blank_line(prev_group, key.group);
952+
prev_group = key.group;
951953
// Don't make any change if it is the same node and no change have to be done
952954
if !blank_line_separated_groups && index == key.slot_index && !was_merged {
953955
continue;
@@ -1001,7 +1003,6 @@ impl Rule for OrganizeImports {
10011003
new_item = new_item.prepend_trivia_pieces(newline)?;
10021004
}
10031005
mutation.replace_element_discard_trivia(old_item.into(), new_item.into());
1004-
prev_group = key.group;
10051006
}
10061007
}
10071008
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// some comment
2+
import react from "@vitejs/plugin-react";
3+
import svgr from "vite-plugin-svgr";
4+
import { defineConfig } from "vite";
5+
import tsconfigPaths from "vite-tsconfig-paths";
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
source: crates/biome_js_analyze/tests/spec_tests.rs
3+
expression: issue6492.js
4+
---
5+
# Input
6+
```js
7+
// some comment
8+
import react from "@vitejs/plugin-react";
9+
import svgr from "vite-plugin-svgr";
10+
import { defineConfig } from "vite";
11+
import tsconfigPaths from "vite-tsconfig-paths";
12+
13+
```
14+
15+
# Diagnostics
16+
```
17+
issue6492.js:2:1 assist/source/organizeImports FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
18+
19+
i The imports and exports are not sorted.
20+
21+
1 │ // some comment
22+
> 2 │ import react from "@vitejs/plugin-react";
23+
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
24+
3 │ import svgr from "vite-plugin-svgr";
25+
4 │ import { defineConfig } from "vite";
26+
27+
i Safe fix: Organize Imports (Biome)
28+
29+
1 1 │ // some comment
30+
2 2 │ import react from "@vitejs/plugin-react";
31+
3 │ - import·svgr·from·"vite-plugin-svgr";
32+
4 │ - import·{·defineConfig·}·from·"vite";
33+
3 │ + import·{·defineConfig·}·from·"vite";
34+
4 │ + import·svgr·from·"vite-plugin-svgr";
35+
5 5 │ import tsconfigPaths from "vite-tsconfig-paths";
36+
6 6 │
37+
38+
39+
```
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"$schema": "https://biomejs.dev/schemas/2.0.4/schema.json",
3+
"assist": {
4+
"enabled": true,
5+
"actions": {
6+
"source": {
7+
"organizeImports": {
8+
"level": "on",
9+
"options": {
10+
"groups": [
11+
["react*", "react*/*"],
12+
":BLANK_LINE:"
13+
]
14+
}
15+
}
16+
}
17+
}
18+
}
19+
}

0 commit comments

Comments
 (0)