diff --git a/crates/biome_js_analyze/src/lint/correctness/no_unused_imports.rs b/crates/biome_js_analyze/src/lint/correctness/no_unused_imports.rs index babe4c0b1e33..5e48a3a434e6 100644 --- a/crates/biome_js_analyze/src/lint/correctness/no_unused_imports.rs +++ b/crates/biome_js_analyze/src/lint/correctness/no_unused_imports.rs @@ -224,15 +224,7 @@ impl Rule for NoUnusedImports { if let Some(blank_line_pos) = blank_line_pos { // keep all leading trivia until the last blank line. leading_trivia_pieces.truncate(blank_line_pos + 1); - if let Some(next_sibling) = parent.next_sibling() { - let new_next_sibling = next_sibling - .clone() - .prepend_trivia_pieces(leading_trivia_pieces)?; - mutation.replace_element_discard_trivia( - next_sibling.into(), - new_next_sibling.into(), - ); - } else if let Some(prev_sibling) = parent.prev_sibling() { + if let Some(prev_sibling) = parent.prev_sibling() { let new_prev_sibling = prev_sibling .clone() .append_trivia_pieces(leading_trivia_pieces)?; @@ -240,6 +232,14 @@ impl Rule for NoUnusedImports { prev_sibling.into(), new_prev_sibling.into(), ); + } else if let Some(next_sibling) = parent.next_sibling() { + let new_next_sibling = next_sibling + .clone() + .prepend_trivia_pieces(leading_trivia_pieces)?; + mutation.replace_element_discard_trivia( + next_sibling.into(), + new_next_sibling.into(), + ); } } mutation.remove_element(parent.into()); diff --git a/crates/biome_js_analyze/tests/specs/correctness/noUnusedImports/invalid-import-namespace.ts.snap b/crates/biome_js_analyze/tests/specs/correctness/noUnusedImports/invalid-import-namespace.ts.snap index 3030019191cd..9ef5e2a2b14f 100644 --- a/crates/biome_js_analyze/tests/specs/correctness/noUnusedImports/invalid-import-namespace.ts.snap +++ b/crates/biome_js_analyze/tests/specs/correctness/noUnusedImports/invalid-import-namespace.ts.snap @@ -48,10 +48,10 @@ invalid-import-namespace.ts:4:18 lint/correctness/noUnusedImports FIXABLE ━ i Safe fix: Remove the unused imports. 1 1 │ import * as Ns1 from "" - 2 2 │ export type T1 = Ns1; - 3 │ - + 2 │ - export·type·T1·=·Ns1; + 2 │ + export·type·T1·=·Ns1; + 3 3 │ 4 │ - import·type·*·as·Ns2·from·"" - 3 │ + 5 4 │ export type T2 = Ns2; diff --git a/crates/biome_js_analyze/tests/specs/correctness/noUnusedImports/invalid-unused-react.jsx.snap b/crates/biome_js_analyze/tests/specs/correctness/noUnusedImports/invalid-unused-react.jsx.snap index 87ad273bc4e0..d3039dd0601d 100644 --- a/crates/biome_js_analyze/tests/specs/correctness/noUnusedImports/invalid-unused-react.jsx.snap +++ b/crates/biome_js_analyze/tests/specs/correctness/noUnusedImports/invalid-unused-react.jsx.snap @@ -100,11 +100,12 @@ invalid-unused-react.jsx:5:8 lint/correctness/noUnusedImports FIXABLE ━━ i Safe fix: Remove the unused imports. + 1 1 │ import X from "react" 2 2 │ import * as X from "react" - 3 3 │ import { default as X } from "react" - 4 │ - + 3 │ - import·{·default·as·X·}·from·"react" + 3 │ + import·{·default·as·X·}·from·"react" + 4 4 │ 5 │ - import·React·from·"x" - 4 │ + 6 5 │ import * as React from "x" 7 6 │ import { default as React } from "x" diff --git a/crates/biome_js_analyze/tests/specs/correctness/noUnusedImports/invalid.js.snap b/crates/biome_js_analyze/tests/specs/correctness/noUnusedImports/invalid.js.snap index e11be7bfe071..62fe09d98f3c 100644 --- a/crates/biome_js_analyze/tests/specs/correctness/noUnusedImports/invalid.js.snap +++ b/crates/biome_js_analyze/tests/specs/correctness/noUnusedImports/invalid.js.snap @@ -81,14 +81,13 @@ invalid.js:5:13 lint/correctness/noUnusedImports FIXABLE ━━━━━━━ i Safe fix: Remove the unused imports. 1 1 │ // Header comment - 2 2 │ import A from "mod"; - 3 │ - + 2 │ - import·A·from·"mod"; + 2 │ + import·A·from·"mod"; + 3 3 │ 4 │ - //·Header·comment 5 │ - import·*·as·B·from·"mod";·//·Import·comment - 6 3 │ - 4 │ + + 6 4 │ 7 5 │ // Header comment - 8 6 │ import { C } from "mod"; // Import comment ``` @@ -108,15 +107,15 @@ invalid.js:8:8 lint/correctness/noUnusedImports FIXABLE ━━━━━━━ i Safe fix: Remove the unused imports. + 3 3 │ 4 4 │ // Header comment - 5 5 │ import * as B from "mod"; // Import comment - 6 │ - + 5 │ - import·*·as·B·from·"mod";·//·Import·comment + 5 │ + import·*·as·B·from·"mod";·//·Import·comment + 6 6 │ 7 │ - //·Header·comment 8 │ - import·{·C·}·from·"mod";·//·Import·comment - 9 6 │ - 7 │ + + 9 7 │ 10 8 │ // Header comment - 11 9 │ import /*a*/ D /*b*/, /*c*/{ E }/*d*/ from "mod"; // Import comment ``` @@ -136,15 +135,15 @@ invalid.js:11:14 lint/correctness/noUnusedImports FIXABLE ━━━━━━ i Safe fix: Remove the unused imports. + 6 6 │ 7 7 │ // Header comment - 8 8 │ import { C } from "mod"; // Import comment - 9 │ - + 8 │ - import·{·C·}·from·"mod";·//·Import·comment + 8 │ + import·{·C·}·from·"mod";·//·Import·comment + 9 9 │ 10 │ - //·Header·comment 11 │ - import·/*a*/·D·/*b*/,·/*c*/{·E·}/*d*/·from·"mod";·//·Import·comment - 12 9 │ - 10 │ + + 12 10 │ 13 11 │ import /*a*/ F /*b*/, /*c*/ * as G /*d*/ from "mod"; - 14 12 │ ``` @@ -165,14 +164,14 @@ invalid.js:13:14 lint/correctness/noUnusedImports FIXABLE ━━━━━━ i Safe fix: Remove the unused imports. + 9 9 │ 10 10 │ // Header comment - 11 11 │ import /*a*/ D /*b*/, /*c*/{ E }/*d*/ from "mod"; // Import comment - 12 │ - + 11 │ - import·/*a*/·D·/*b*/,·/*c*/{·E·}/*d*/·from·"mod";·//·Import·comment + 11 │ + import·/*a*/·D·/*b*/,·/*c*/{·E·}/*d*/·from·"mod";·//·Import·comment + 12 12 │ 13 │ - import·/*a*/·F·/*b*/,·/*c*/·*·as·G·/*d*/·from·"mod"; - 14 12 │ - 13 │ + + 14 13 │ 15 14 │ import { - 16 15 │ // Comment ``` @@ -198,18 +197,18 @@ invalid.js:15:8 lint/correctness/noUnusedImports FIXABLE ━━━━━━━ i Safe fix: Remove the unused imports. + 11 11 │ import /*a*/ D /*b*/, /*c*/{ E }/*d*/ from "mod"; // Import comment 12 12 │ - 13 13 │ import /*a*/ F /*b*/, /*c*/ * as G /*d*/ from "mod"; - 14 │ - + 13 │ - import·/*a*/·F·/*b*/,·/*c*/·*·as·G·/*d*/·from·"mod"; + 13 │ + import·/*a*/·F·/*b*/,·/*c*/·*·as·G·/*d*/·from·"mod"; + 14 14 │ 15 │ - import·{ 16 │ - ····//·Comment 17 │ - ····H, 18 │ - ····I, 19 │ - }·from·"mod"; - 20 14 │ - 15 │ + + 20 15 │ 21 16 │ import {/*a*/J/*b*/, /*c*/K/*d*/} from "mod"; - 22 17 │ ``` @@ -230,14 +229,14 @@ invalid.js:21:8 lint/correctness/noUnusedImports FIXABLE ━━━━━━━ i Safe fix: Remove the unused imports. + 17 17 │ H, 18 18 │ I, - 19 19 │ } from "mod"; - 20 │ - + 19 │ - }·from·"mod"; + 19 │ + }·from·"mod"; + 20 20 │ 21 │ - import·{/*a*/J/*b*/,·/*c*/K/*d*/}·from·"mod"; - 22 20 │ - 21 │ + + 22 21 │ 23 22 │ // Header comment - 24 23 │ import { L as M, } from "mod"; // Import comment ``` @@ -257,15 +256,15 @@ invalid.js:24:8 lint/correctness/noUnusedImports FIXABLE ━━━━━━━ i Safe fix: Remove the unused imports. + 19 19 │ } from "mod"; 20 20 │ - 21 21 │ import {/*a*/J/*b*/, /*c*/K/*d*/} from "mod"; - 22 │ - + 21 │ - import·{/*a*/J/*b*/,·/*c*/K/*d*/}·from·"mod"; + 21 │ + import·{/*a*/J/*b*/,·/*c*/K/*d*/}·from·"mod"; + 22 22 │ 23 │ - //·Header·comment 24 │ - import·{·L·as·M,·}·from·"mod";·//·Import·comment - 25 22 │ - 23 │ + + 25 23 │ 26 24 │ // See https://github.com/biomejs/biome/issues/653 - 27 25 │ import {a} from 'a' ``` diff --git a/crates/biome_js_analyze/tests/specs/correctness/noUnusedImports/invalid.jsx.snap b/crates/biome_js_analyze/tests/specs/correctness/noUnusedImports/invalid.jsx.snap index eceb0aed0eb8..22a0da18ede9 100644 --- a/crates/biome_js_analyze/tests/specs/correctness/noUnusedImports/invalid.jsx.snap +++ b/crates/biome_js_analyze/tests/specs/correctness/noUnusedImports/invalid.jsx.snap @@ -110,11 +110,12 @@ invalid.jsx:5:8 lint/correctness/noUnusedImports FIXABLE ━━━━━━━ i Safe fix: Remove the unused imports. + 1 1 │ import X from "react" 2 2 │ import * as X from "react" - 3 3 │ import { default as X } from "react" - 4 │ - + 3 │ - import·{·default·as·X·}·from·"react" + 3 │ + import·{·default·as·X·}·from·"react" + 4 4 │ 5 │ - import·React·from·"x" - 4 │ + 6 5 │ import * as React from "x" 7 6 │ import { default as React } from "x" @@ -210,12 +211,13 @@ invalid.jsx:11:8 lint/correctness/noUnusedImports FIXABLE ━━━━━━ i Safe fix: Remove the unused imports. + 6 6 │ import * as React from "x" 7 7 │ import { default as React } from "x" - 8 8 │ import React, { useEffect } from "x" - 9 │ - + 8 │ - import·React,·{·useEffect·}·from·"x" + 8 │ + import·React,·{·useEffect·}·from·"x" + 9 9 │ 10 │ - //·unsupported·patterns 11 │ - import·X,·{·default·as·React·}·from·"react" - 9 │ + 12 10 │ import X, * as React from "react" 13 11 │ @@ -262,12 +264,13 @@ invalid.jsx:15:8 lint/correctness/noUnusedImports FIXABLE ━━━━━━ i Safe fix: Remove the unused imports. + 10 10 │ // unsupported patterns 11 11 │ import X, { default as React } from "react" - 12 12 │ import X, * as React from "react" - 13 │ - + 12 │ - import·X,·*·as·React·from·"react" + 12 │ + import·X,·*·as·React·from·"react" + 13 13 │ 14 │ - //·React·import·(no·exception) 15 │ - import·React·from·"react" - 13 │ + 16 14 │ import * as React from "react" 17 15 │ import { default as React } from "react" diff --git a/crates/biome_js_analyze/tests/specs/correctness/noUnusedImports/invalid.ts.snap b/crates/biome_js_analyze/tests/specs/correctness/noUnusedImports/invalid.ts.snap index d72a65aa7d86..81cfcc6d883f 100644 --- a/crates/biome_js_analyze/tests/specs/correctness/noUnusedImports/invalid.ts.snap +++ b/crates/biome_js_analyze/tests/specs/correctness/noUnusedImports/invalid.ts.snap @@ -76,14 +76,13 @@ invalid.ts:5:18 lint/correctness/noUnusedImports FIXABLE ━━━━━━━ i Safe fix: Remove the unused imports. 1 1 │ // Header comment - 2 2 │ import type A from "mod"; - 3 │ - + 2 │ - import·type·A·from·"mod"; + 2 │ + import·type·A·from·"mod"; + 3 3 │ 4 │ - //·Header·comment 5 │ - import·type·*·as·B·from·"mod";·//·Import·comment - 6 3 │ - 4 │ + + 6 4 │ 7 5 │ // Header comment - 8 6 │ import type { C } from "mod"; // Import comment ``` @@ -103,15 +102,15 @@ invalid.ts:8:13 lint/correctness/noUnusedImports FIXABLE ━━━━━━━ i Safe fix: Remove the unused imports. + 3 3 │ 4 4 │ // Header comment - 5 5 │ import type * as B from "mod"; // Import comment - 6 │ - + 5 │ - import·type·*·as·B·from·"mod";·//·Import·comment + 5 │ + import·type·*·as·B·from·"mod";·//·Import·comment + 6 6 │ 7 │ - //·Header·comment 8 │ - import·type·{·C·}·from·"mod";·//·Import·comment - 9 6 │ - 7 │ + + 9 7 │ 10 8 │ // Orphan comment - 11 9 │ ``` @@ -131,19 +130,19 @@ invalid.ts:13:14 lint/correctness/noUnusedImports FIXABLE ━━━━━━ i Safe fix: Remove the unused imports. + 6 6 │ 7 7 │ // Header comment - 8 8 │ import type { C } from "mod"; // Import comment - 9 │ - + 8 │ - import·type·{·C·}·from·"mod";·//·Import·comment + 8 │ + import·type·{·C·}·from·"mod";·//·Import·comment + 9 │ + + 10 │ + //·Orphan·comment + 9 11 │ 10 │ - //·Orphan·comment 11 │ - 12 │ - //·Header·comment 13 │ - import·/*a*/·D·/*b*/,·/*c*/{·type·E·}/*d*/·from·"mod";·//·Import·comment - 14 9 │ - 10 │ + //·Orphan·comment - 11 │ + - 12 │ + + 14 12 │ 15 13 │ import /*a*/ F /*b*/, /*c*/ * as G /*d*/ from "mod"; - 16 14 │ ``` @@ -164,14 +163,14 @@ invalid.ts:15:14 lint/correctness/noUnusedImports FIXABLE ━━━━━━ i Safe fix: Remove the unused imports. + 11 11 │ 12 12 │ // Header comment - 13 13 │ import /*a*/ D /*b*/, /*c*/{ type E }/*d*/ from "mod"; // Import comment - 14 │ - + 13 │ - import·/*a*/·D·/*b*/,·/*c*/{·type·E·}/*d*/·from·"mod";·//·Import·comment + 13 │ + import·/*a*/·D·/*b*/,·/*c*/{·type·E·}/*d*/·from·"mod";·//·Import·comment + 14 14 │ 15 │ - import·/*a*/·F·/*b*/,·/*c*/·*·as·G·/*d*/·from·"mod"; - 16 14 │ - 15 │ + + 16 15 │ 17 16 │ import { - 18 17 │ // Comment ``` @@ -197,18 +196,18 @@ invalid.ts:17:8 lint/correctness/noUnusedImports FIXABLE ━━━━━━━ i Safe fix: Remove the unused imports. + 13 13 │ import /*a*/ D /*b*/, /*c*/{ type E }/*d*/ from "mod"; // Import comment 14 14 │ - 15 15 │ import /*a*/ F /*b*/, /*c*/ * as G /*d*/ from "mod"; - 16 │ - + 15 │ - import·/*a*/·F·/*b*/,·/*c*/·*·as·G·/*d*/·from·"mod"; + 15 │ + import·/*a*/·F·/*b*/,·/*c*/·*·as·G·/*d*/·from·"mod"; + 16 16 │ 17 │ - import·{ 18 │ - ····//·Comment 19 │ - ····type·H, 20 │ - ····type·I, 21 │ - }·from·"mod"; - 22 16 │ - 17 │ + + 22 17 │ 23 18 │ import {/*a*/type J/*b*/, /*c*/type K/*d*/} from "mod"; - 24 19 │ ``` @@ -229,14 +228,14 @@ invalid.ts:23:8 lint/correctness/noUnusedImports FIXABLE ━━━━━━━ i Safe fix: Remove the unused imports. + 19 19 │ type H, 20 20 │ type I, - 21 21 │ } from "mod"; - 22 │ - + 21 │ - }·from·"mod"; + 21 │ + }·from·"mod"; + 22 22 │ 23 │ - import·{/*a*/type·J/*b*/,·/*c*/type·K/*d*/}·from·"mod"; - 24 22 │ - 23 │ + + 24 23 │ 25 24 │ // Header comment - 26 25 │ import type { L as M, } from "mod"; // Import comment ```