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

fix(lint/noUnusedImports): remove entire line of an unused import #659

Merged
merged 1 commit into from
Nov 2, 2023
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
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ Read our [guidelines for writing a good changelog entry](https://github.com/biom

- Fix [#639](https://github.com/biomejs/biome/issues/639) by ignoring unused TypeScript's mapped key. Contributed by @Conaclos

- Fix [#565](https://github.com/biomejs/biome/issues/565) by handling several `infer` with the same name in extends clauses of TypeScript's conditional types.
- Fix [#565](https://github.com/biomejs/biome/issues/565) by handling several `infer` with the same name in extends clauses of TypeScript's conditional types. Contributed by @Conaclos

- Fix [#653](https://github.com/biomejs/biome/issues/653). [noUnusedImports](https://biomejs.dev/linter/rules/no-unused-imports) now correctly removes the entire line where the unused `import` is. Contributed by @Conaclos

### Parser

Expand Down
32 changes: 17 additions & 15 deletions crates/biome_js_analyze/src/utils/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use biome_js_syntax::{
JsSyntaxNode, JsVariableDeclaration, JsVariableDeclarator, JsVariableDeclaratorList,
JsVariableStatement, JsxChildList, T,
};
use biome_rowan::{AstNode, AstSeparatedList, BatchMutation};
use biome_rowan::{chain_trivia_pieces, AstNode, AstSeparatedList, BatchMutation};

pub trait JsBatchMutation {
/// Removes the declarator, and:
Expand Down Expand Up @@ -312,20 +312,22 @@ impl JsBatchMutation for BatchMutation<JsLanguage> {
let Some(pieces) = node.first_leading_trivia().map(|trivia| trivia.pieces()) else {
return;
};
let (sibling, new_sibling) =
if let Some(next_sibling) = node.last_token().and_then(|x| x.next_token()) {
(
next_sibling.clone(),
next_sibling.prepend_trivia_pieces(pieces),
)
} else if let Some(prev_sibling) = node.first_token().and_then(|x| x.prev_token()) {
(
prev_sibling.clone(),
prev_sibling.append_trivia_pieces(pieces),
)
} else {
return;
};
let (sibling, new_sibling) = if let Some(next_sibling) =
node.last_token().and_then(|x| x.next_token())
{
let mut next_sibling_leading_trivia = next_sibling.leading_trivia().pieces().peekable();
// Remove next newline
next_sibling_leading_trivia.next_if(|piece| piece.is_newline());
(
next_sibling.clone(),
next_sibling.with_leading_trivia_pieces(chain_trivia_pieces(
pieces,
next_sibling_leading_trivia,
)),
)
} else {
return;
};
self.replace_token_discard_trivia(sibling, new_sibling);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,9 @@ import {/*a*/J/*b*/, /*c*/K/*d*/} from "mod";

// Header comment
import { L as M, } from "mod"; // Import comment

// See https://github.com/biomejs/biome/issues/653
import {a} from 'a'
import {d} from 'd'
import {b} from 'b'
export const bb = a + b
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ import {/*a*/J/*b*/, /*c*/K/*d*/} from "mod";
// Header comment
import { L as M, } from "mod"; // Import comment

// See https://github.com/biomejs/biome/issues/653
import {a} from 'a'
import {d} from 'd'
import {b} from 'b'
export const bb = a + b
```

# Diagnostics
Expand All @@ -47,8 +52,11 @@ invalid.js:2:8 lint/nursery/noUnusedImports FIXABLE ━━━━━━━━

i Safe fix: Remove the unused import.

2 │ import·A·from·"mod";
│ --------------------
1 1 │ // Header comment
2 │ - import·A·from·"mod";
3 2 │
4 3 │ // Header comment


```

Expand All @@ -67,8 +75,12 @@ invalid.js:5:13 lint/nursery/noUnusedImports FIXABLE ━━━━━━━━

i Safe fix: Remove the unused import.

5 │ import·*·as·B·from·"mod";·//·Import·comment
│ -------------------------------------------
3 3 │
4 4 │ // Header comment
5 │ - import·*·as·B·from·"mod";·//·Import·comment
6 5 │
7 6 │ // Header comment


```

Expand All @@ -87,8 +99,12 @@ invalid.js:8:10 lint/nursery/noUnusedImports FIXABLE ━━━━━━━━

i Safe fix: Remove the unused import.

8 │ import·{·C·}·from·"mod";·//·Import·comment
│ ------------------------------------------
6 6 │
7 7 │ // Header comment
8 │ - import·{·C·}·from·"mod";·//·Import·comment
9 8 │
10 9 │ // Header comment


```

Expand Down Expand Up @@ -278,13 +294,44 @@ invalid.js:24:15 lint/nursery/noUnusedImports FIXABLE ━━━━━━━━
> 24 │ import { L as M, } from "mod"; // Import comment
│ ^
25 │
26 │ // See https://github.com/biomejs/biome/issues/653

i Unused imports might be the result of an incomplete refactoring.

i Safe fix: Remove the unused import.

24 │ import·{·L·as·M,·}·from·"mod";·//·Import·comment
│ ------------------------------------------------
22 22 │
23 23 │ // Header comment
24 │ - import·{·L·as·M,·}·from·"mod";·//·Import·comment
25 24 │
26 25 │ // See https://github.com/biomejs/biome/issues/653


```

```
invalid.js:28:9 lint/nursery/noUnusedImports FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

! This import is unused.

26 │ // See https://github.com/biomejs/biome/issues/653
27 │ import {a} from 'a'
> 28 │ import {d} from 'd'
│ ^
29 │ import {b} from 'b'
30 │ export const bb = a + b

i Unused imports might be the result of an incomplete refactoring.

i Safe fix: Remove the unused import.

26 26 │ // See https://github.com/biomejs/biome/issues/653
27 27 │ import {a} from 'a'
28 │ - import·{d}·from·'d'
29 │ - import·{b}·from·'b'
28 │ + import·{b}·from·'b'
30 29 │ export const bb = a + b


```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,12 @@ invalid.jsx:1:8 lint/nursery/noUnusedImports FIXABLE ━━━━━━━━

i Safe fix: Remove the unused import.

1 │ import·X·from·"react"
│ ---------------------
1 │ - import·X·from·"react"
2 │ - import·*·as·X·from·"react"
1 │ + import·*·as·X·from·"react"
3 2 │ import { default as X } from "react"
4 3 │


```

Expand All @@ -60,8 +64,13 @@ invalid.jsx:2:13 lint/nursery/noUnusedImports FIXABLE ━━━━━━━━

i Safe fix: Remove the unused import.

2 │ import·*·as·X·from·"react"
│ --------------------------
1 1 │ import X from "react"
2 │ - import·*·as·X·from·"react"
3 │ - import·{·default·as·X·}·from·"react"
2 │ + import·{·default·as·X·}·from·"react"
4 3 │
5 4 │ import React from "x"


```

Expand All @@ -81,8 +90,12 @@ invalid.jsx:3:21 lint/nursery/noUnusedImports FIXABLE ━━━━━━━━

i Safe fix: Remove the unused import.

3 │ import·{·default·as·X·}·from·"react"
│ ------------------------------------
1 1 │ import X from "react"
2 2 │ import * as X from "react"
3 │ - import·{·default·as·X·}·from·"react"
4 3 │
5 4 │ import React from "x"


```

Expand All @@ -102,8 +115,14 @@ invalid.jsx:5:8 lint/nursery/noUnusedImports FIXABLE ━━━━━━━━

i Safe fix: Remove the unused import.

5 │ import·React·from·"x"
│ ---------------------
3 3 │ import { default as X } from "react"
4 4 │
5 │ - import·React·from·"x"
6 │ - import·*·as·React·from·"x"
5 │ + import·*·as·React·from·"x"
7 6 │ import { default as React } from "x"
8 7 │ import React, { useEffect } from "x"


```

Expand All @@ -122,8 +141,14 @@ invalid.jsx:6:13 lint/nursery/noUnusedImports FIXABLE ━━━━━━━━

i Safe fix: Remove the unused import.

6 │ import·*·as·React·from·"x"
│ --------------------------
4 4 │
5 5 │ import React from "x"
6 │ - import·*·as·React·from·"x"
7 │ - import·{·default·as·React·}·from·"x"
6 │ + import·{·default·as·React·}·from·"x"
8 7 │ import React, { useEffect } from "x"
9 8 │


```

Expand All @@ -143,8 +168,14 @@ invalid.jsx:7:21 lint/nursery/noUnusedImports FIXABLE ━━━━━━━━

i Safe fix: Remove the unused import.

7 │ import·{·default·as·React·}·from·"x"
│ ------------------------------------
5 5 │ import React from "x"
6 6 │ import * as React from "x"
7 │ - import·{·default·as·React·}·from·"x"
8 │ - import·React,·{·useEffect·}·from·"x"
7 │ + import·React,·{·useEffect·}·from·"x"
9 8 │
10 9 │ // unsupported patterns


```

Expand Down Expand Up @@ -287,8 +318,14 @@ invalid.jsx:15:8 lint/nursery/noUnusedImports FIXABLE ━━━━━━━━

i Safe fix: Remove the unused import.

15 │ import·React·from·"react"
│ -------------------------
13 13 │
14 14 │ // React import (no exception)
15 │ - import·React·from·"react"
16 │ - import·*·as·React·from·"react"
15 │ + import·*·as·React·from·"react"
17 16 │ import { default as React } from "react"
18 17 │ import React, { useEffect } from "react"


```

Expand All @@ -308,8 +345,14 @@ invalid.jsx:16:13 lint/nursery/noUnusedImports FIXABLE ━━━━━━━

i Safe fix: Remove the unused import.

16 │ import·*·as·React·from·"react"
│ ------------------------------
14 14 │ // React import (no exception)
15 15 │ import React from "react"
16 │ - import·*·as·React·from·"react"
17 │ - import·{·default·as·React·}·from·"react"
16 │ + import·{·default·as·React·}·from·"react"
18 17 │ import React, { useEffect } from "react"
19 18 │


```

Expand All @@ -329,8 +372,13 @@ invalid.jsx:17:21 lint/nursery/noUnusedImports FIXABLE ━━━━━━━

i Safe fix: Remove the unused import.

17 │ import·{·default·as·React·}·from·"react"
│ ----------------------------------------
15 15 │ import React from "react"
16 16 │ import * as React from "react"
17 │ - import·{·default·as·React·}·from·"react"
18 │ - import·React,·{·useEffect·}·from·"react"
17 │ + import·React,·{·useEffect·}·from·"react"
19 18 │


```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,11 @@ invalid.ts:2:13 lint/nursery/noUnusedImports FIXABLE ━━━━━━━━

i Safe fix: Remove the unused import.

2 │ import·type·A·from·"mod";
│ -------------------------
1 1 │ // Header comment
2 │ - import·type·A·from·"mod";
3 2 │
4 3 │ // Header comment


```

Expand All @@ -67,8 +70,12 @@ invalid.ts:5:18 lint/nursery/noUnusedImports FIXABLE ━━━━━━━━

i Safe fix: Remove the unused import.

5 │ import·type·*·as·B·from·"mod";·//·Import·comment
│ ------------------------------------------------
3 3 │
4 4 │ // Header comment
5 │ - import·type·*·as·B·from·"mod";·//·Import·comment
6 5 │
7 6 │ // Header comment


```

Expand All @@ -87,8 +94,12 @@ invalid.ts:8:15 lint/nursery/noUnusedImports FIXABLE ━━━━━━━━

i Safe fix: Remove the unused import.

8 │ import·type·{·C·}·from·"mod";·//·Import·comment
│ -----------------------------------------------
6 6 │
7 7 │ // Header comment
8 │ - import·type·{·C·}·from·"mod";·//·Import·comment
9 8 │
10 9 │ // Header comment


```

Expand Down Expand Up @@ -283,8 +294,11 @@ invalid.ts:24:20 lint/nursery/noUnusedImports FIXABLE ━━━━━━━━

i Safe fix: Remove the unused import.

24 │ import·type·{·L·as·M,·}·from·"mod";·//·Import·comment
│ -----------------------------------------------------
22 22 │
23 23 │ // Header comment
24 │ - import·type·{·L·as·M,·}·from·"mod";·//·Import·comment
25 24 │


```

Expand Down
Loading