Skip to content

Commit

Permalink
refactor(noUnusedImports): report empty named imports (#4478)
Browse files Browse the repository at this point in the history
  • Loading branch information
Conaclos authored Nov 6, 2024
1 parent 7aaab7f commit 3ad6da5
Show file tree
Hide file tree
Showing 9 changed files with 503 additions and 516 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,16 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b

Contributed by @Conaclos

- `noUnusedImports` now reports empty named imports and suggests its removal ([#3574](https://github.com/biomejs/biome/issues/3574)).

The rule now suggests the removal of empty named imports such as:

```diff
- import {} from "mod";
```

Contributed by @Conaclos

#### Bug fixes

- [useArrayLiterals](https://biomejs.dev/linter/rules/use-array-literals/) now reports all expressions using the `Array` constructors.
Expand Down
404 changes: 278 additions & 126 deletions crates/biome_js_analyze/src/lint/correctness/no_unused_imports.rs

Large diffs are not rendered by default.

3 changes: 0 additions & 3 deletions crates/biome_js_analyze/src/lint/style/use_import_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,6 @@ impl Rule for UseImportType {
}
let import = ctx.query();
let import_clause = import.import_clause().ok()?;
if import_clause.assertion().is_some() {
return None;
}
// Import attributes and type-only imports are not compatible.
if import_clause.assertion().is_some() {
return None;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
source: crates/biome_js_analyze/tests/spec_tests.rs
expression: invalid-import-namespace.ts
snapshot_kind: text
---
# Input
```ts
Expand All @@ -24,7 +25,7 @@ invalid-import-namespace.ts:1:13 lint/correctness/noUnusedImports FIXABLE ━
i Unused imports might be the result of an incomplete refactoring.
i Safe fix: Remove the unused import.
i Safe fix: Remove the unused imports.
1 │ import·*·as·Ns1·from·""
│ -----------------------
Expand All @@ -44,7 +45,7 @@ invalid-import-namespace.ts:4:18 lint/correctness/noUnusedImports FIXABLE ━
i Unused imports might be the result of an incomplete refactoring.
i Safe fix: Remove the unused import.
i Safe fix: Remove the unused imports.
1 1 │ import * as Ns1 from ""
2 2 │ export type T1 = Ns1;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
---
source: crates/biome_js_analyze/tests/spec_tests.rs
expression: invalid-unused-react.jsx
snapshot_kind: text
---
# Input
```jsx
Expand Down Expand Up @@ -28,7 +29,7 @@ invalid-unused-react.jsx:1:8 lint/correctness/noUnusedImports FIXABLE ━━
i Unused imports might be the result of an incomplete refactoring.
i Safe fix: Remove the unused import.
i Safe fix: Remove the unused imports.
1 │ import·X·from·"react"
│ ---------------------
Expand All @@ -48,7 +49,7 @@ invalid-unused-react.jsx:2:13 lint/correctness/noUnusedImports FIXABLE ━━
i Unused imports might be the result of an incomplete refactoring.
i Safe fix: Remove the unused import.
i Safe fix: Remove the unused imports.
1 1 │ import X from "react"
2 │ - import·*·as·X·from·"react"
Expand All @@ -59,20 +60,20 @@ invalid-unused-react.jsx:2:13 lint/correctness/noUnusedImports FIXABLE ━━
```

```
invalid-unused-react.jsx:3:21 lint/correctness/noUnusedImports FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━
invalid-unused-react.jsx:3:8 lint/correctness/noUnusedImports FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━
! This import is unused.
1 │ import X from "react"
2 │ import * as X from "react"
> 3 │ import { default as X } from "react"
^
^^^^^^^^^^^^^^^^
4 │
5 │ import React from "x"
i Unused imports might be the result of an incomplete refactoring.
i Safe fix: Remove the unused import.
i Safe fix: Remove the unused imports.
1 1 │ import X from "react"
2 2 │ import * as X from "react"
Expand All @@ -97,7 +98,7 @@ invalid-unused-react.jsx:5:8 lint/correctness/noUnusedImports FIXABLE ━━
i Unused imports might be the result of an incomplete refactoring.
i Safe fix: Remove the unused import.
i Safe fix: Remove the unused imports.
2 2 │ import * as X from "react"
3 3 │ import { default as X } from "react"
Expand All @@ -122,7 +123,7 @@ invalid-unused-react.jsx:6:13 lint/correctness/noUnusedImports FIXABLE ━━
i Unused imports might be the result of an incomplete refactoring.
i Safe fix: Remove the unused import.
i Safe fix: Remove the unused imports.
4 4 │
5 5 │ import React from "x"
Expand All @@ -134,20 +135,20 @@ invalid-unused-react.jsx:6:13 lint/correctness/noUnusedImports FIXABLE ━━
```

```
invalid-unused-react.jsx:7:21 lint/correctness/noUnusedImports FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━
invalid-unused-react.jsx:7:8 lint/correctness/noUnusedImports FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━
! This import is unused.
5 │ import React from "x"
6 │ import * as React from "x"
> 7 │ import { default as React } from "x"
^^^^^
^^^^^^^^^^^^^^^^^^^^
8 │ import React, { useEffect } from "x"
9 │
i Unused imports might be the result of an incomplete refactoring.
i Safe fix: Remove the unused import.
i Safe fix: Remove the unused imports.
5 5 │ import React from "x"
6 6 │ import * as React from "x"
Expand All @@ -161,39 +162,22 @@ invalid-unused-react.jsx:7:21 lint/correctness/noUnusedImports FIXABLE ━━
```
invalid-unused-react.jsx:8:8 lint/correctness/noUnusedImports FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! This import is unused.
! These imports are unused.
6 │ import * as React from "x"
7 │ import { default as React } from "x"
> 8 │ import React, { useEffect } from "x"
│ ^^^^^
│ ^^^^^^^^^^^^^^^^^^^^
9 │
i Unused imports might be the result of an incomplete refactoring.
i Safe fix: Remove the unused import.
8 │ import·React,·{·useEffect·}·from·"x"
│ -------
```

```
invalid-unused-react.jsx:8:17 lint/correctness/noUnusedImports FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━
! This import is unused.
6 │ import * as React from "x"
7 │ import { default as React } from "x"
> 8 │ import React, { useEffect } from "x"
│ ^^^^^^^^^
9 │
i Safe fix: Remove the unused imports.
i Unused imports might be the result of an incomplete refactoring.
i Safe fix: Remove the unused import.
6 6 │ import * as React from "x"
7 7 │ import { default as React } from "x"
8 │ - import·React,·{·useEffect·}·from·"x"
9 8 │
8 │ import·React,·{·useEffect·}·from·"x"
│ ---------------
```
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ import { L as M, } from "mod"; // Import comment
import {a} from 'a'
import {d} from 'd'
import {b} from 'b'
export const bb = a + b
export const bb = a + b

import {} from "mod"
Loading

0 comments on commit 3ad6da5

Please sign in to comment.