Skip to content
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

refactor(noUnusedImports): report empty named imports #4478

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this new? Which version of cargo-insta are you using?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The latest one: cargo-insta 1.41.1

---
# 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