Skip to content

Commit 3ad6da5

Browse files
authored
refactor(noUnusedImports): report empty named imports (#4478)
1 parent 7aaab7f commit 3ad6da5

File tree

9 files changed

+503
-516
lines changed

9 files changed

+503
-516
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,16 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b
103103

104104
Contributed by @Conaclos
105105

106+
- `noUnusedImports` now reports empty named imports and suggests its removal ([#3574](https://github.com/biomejs/biome/issues/3574)).
107+
108+
The rule now suggests the removal of empty named imports such as:
109+
110+
```diff
111+
- import {} from "mod";
112+
```
113+
114+
Contributed by @Conaclos
115+
106116
#### Bug fixes
107117

108118
- [useArrayLiterals](https://biomejs.dev/linter/rules/use-array-literals/) now reports all expressions using the `Array` constructors.

crates/biome_js_analyze/src/lint/correctness/no_unused_imports.rs

Lines changed: 278 additions & 126 deletions
Large diffs are not rendered by default.

crates/biome_js_analyze/src/lint/style/use_import_type.rs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,6 @@ impl Rule for UseImportType {
133133
}
134134
let import = ctx.query();
135135
let import_clause = import.import_clause().ok()?;
136-
if import_clause.assertion().is_some() {
137-
return None;
138-
}
139136
// Import attributes and type-only imports are not compatible.
140137
if import_clause.assertion().is_some() {
141138
return None;

crates/biome_js_analyze/tests/specs/correctness/noUnusedImports/invalid-import-namespace.ts.snap

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
source: crates/biome_js_analyze/tests/spec_tests.rs
33
expression: invalid-import-namespace.ts
4+
snapshot_kind: text
45
---
56
# Input
67
```ts
@@ -24,7 +25,7 @@ invalid-import-namespace.ts:1:13 lint/correctness/noUnusedImports FIXABLE ━
2425
2526
i Unused imports might be the result of an incomplete refactoring.
2627
27-
i Safe fix: Remove the unused import.
28+
i Safe fix: Remove the unused imports.
2829
2930
1 │ import·*·as·Ns1·from·""
3031
│ -----------------------
@@ -44,7 +45,7 @@ invalid-import-namespace.ts:4:18 lint/correctness/noUnusedImports FIXABLE ━
4445
4546
i Unused imports might be the result of an incomplete refactoring.
4647
47-
i Safe fix: Remove the unused import.
48+
i Safe fix: Remove the unused imports.
4849
4950
1 1 │ import * as Ns1 from ""
5051
2 2 │ export type T1 = Ns1;

crates/biome_js_analyze/tests/specs/correctness/noUnusedImports/invalid-unused-react.jsx.snap

Lines changed: 18 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
---
22
source: crates/biome_js_analyze/tests/spec_tests.rs
33
expression: invalid-unused-react.jsx
4+
snapshot_kind: text
45
---
56
# Input
67
```jsx
@@ -28,7 +29,7 @@ invalid-unused-react.jsx:1:8 lint/correctness/noUnusedImports FIXABLE ━━
2829
2930
i Unused imports might be the result of an incomplete refactoring.
3031
31-
i Safe fix: Remove the unused import.
32+
i Safe fix: Remove the unused imports.
3233
3334
1 │ import·X·from·"react"
3435
│ ---------------------
@@ -48,7 +49,7 @@ invalid-unused-react.jsx:2:13 lint/correctness/noUnusedImports FIXABLE ━━
4849
4950
i Unused imports might be the result of an incomplete refactoring.
5051
51-
i Safe fix: Remove the unused import.
52+
i Safe fix: Remove the unused imports.
5253
5354
1 1 │ import X from "react"
5455
2 │ - import·*·as·X·from·"react"
@@ -59,20 +60,20 @@ invalid-unused-react.jsx:2:13 lint/correctness/noUnusedImports FIXABLE ━━
5960
```
6061

6162
```
62-
invalid-unused-react.jsx:3:21 lint/correctness/noUnusedImports FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━
63+
invalid-unused-react.jsx:3:8 lint/correctness/noUnusedImports FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━
6364
6465
! This import is unused.
6566
6667
1 │ import X from "react"
6768
2 │ import * as X from "react"
6869
> 3 │ import { default as X } from "react"
69-
^
70+
^^^^^^^^^^^^^^^^
7071
4 │
7172
5 │ import React from "x"
7273
7374
i Unused imports might be the result of an incomplete refactoring.
7475
75-
i Safe fix: Remove the unused import.
76+
i Safe fix: Remove the unused imports.
7677
7778
1 1 │ import X from "react"
7879
2 2 │ import * as X from "react"
@@ -97,7 +98,7 @@ invalid-unused-react.jsx:5:8 lint/correctness/noUnusedImports FIXABLE ━━
9798
9899
i Unused imports might be the result of an incomplete refactoring.
99100
100-
i Safe fix: Remove the unused import.
101+
i Safe fix: Remove the unused imports.
101102
102103
2 2 │ import * as X from "react"
103104
3 3 │ import { default as X } from "react"
@@ -122,7 +123,7 @@ invalid-unused-react.jsx:6:13 lint/correctness/noUnusedImports FIXABLE ━━
122123
123124
i Unused imports might be the result of an incomplete refactoring.
124125
125-
i Safe fix: Remove the unused import.
126+
i Safe fix: Remove the unused imports.
126127
127128
4 4 │
128129
5 5 │ import React from "x"
@@ -134,20 +135,20 @@ invalid-unused-react.jsx:6:13 lint/correctness/noUnusedImports FIXABLE ━━
134135
```
135136

136137
```
137-
invalid-unused-react.jsx:7:21 lint/correctness/noUnusedImports FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━
138+
invalid-unused-react.jsx:7:8 lint/correctness/noUnusedImports FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━
138139
139140
! This import is unused.
140141
141142
5 │ import React from "x"
142143
6 │ import * as React from "x"
143144
> 7 │ import { default as React } from "x"
144-
^^^^^
145+
^^^^^^^^^^^^^^^^^^^^
145146
8 │ import React, { useEffect } from "x"
146147
9 │
147148
148149
i Unused imports might be the result of an incomplete refactoring.
149150
150-
i Safe fix: Remove the unused import.
151+
i Safe fix: Remove the unused imports.
151152
152153
5 5 │ import React from "x"
153154
6 6 │ import * as React from "x"
@@ -161,39 +162,22 @@ invalid-unused-react.jsx:7:21 lint/correctness/noUnusedImports FIXABLE ━━
161162
```
162163
invalid-unused-react.jsx:8:8 lint/correctness/noUnusedImports FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━
163164
164-
! This import is unused.
165+
! These imports are unused.
165166
166167
6 │ import * as React from "x"
167168
7 │ import { default as React } from "x"
168169
> 8 │ import React, { useEffect } from "x"
169-
│ ^^^^^
170+
│ ^^^^^^^^^^^^^^^^^^^^
170171
9 │
171172
172173
i Unused imports might be the result of an incomplete refactoring.
173174
174-
i Safe fix: Remove the unused import.
175-
176-
8 │ import·React,·{·useEffect·}·from·"x"
177-
│ -------
178-
179-
```
180-
181-
```
182-
invalid-unused-react.jsx:8:17 lint/correctness/noUnusedImports FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━
183-
184-
! This import is unused.
185-
186-
6 │ import * as React from "x"
187-
7 │ import { default as React } from "x"
188-
> 8 │ import React, { useEffect } from "x"
189-
│ ^^^^^^^^^
190-
9 │
175+
i Safe fix: Remove the unused imports.
191176
192-
i Unused imports might be the result of an incomplete refactoring.
193-
194-
i Safe fix: Remove the unused import.
177+
6 6 │ import * as React from "x"
178+
7 7 │ import { default as React } from "x"
179+
8 │ - import·React,·{·useEffect·}·from·"x"
180+
9 8 │
195181
196-
8 │ import·React,·{·useEffect·}·from·"x"
197-
│ ---------------
198182
199183
```

crates/biome_js_analyze/tests/specs/correctness/noUnusedImports/invalid.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,6 @@ import { L as M, } from "mod"; // Import comment
2727
import {a} from 'a'
2828
import {d} from 'd'
2929
import {b} from 'b'
30-
export const bb = a + b
30+
export const bb = a + b
31+
32+
import {} from "mod"

0 commit comments

Comments
 (0)