Skip to content

Commit d9b5133

Browse files
Moktoematipicoautofix-ci[bot]
authored
fix(lint): noUnusedImports/noUnusedVariables now flag truly-unused script bindings in Svelte/Vue/Astro (#10473)
Co-authored-by: Emanuele Stoppa <my.burning@gmail.com> Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 3617094 commit d9b5133

42 files changed

Lines changed: 895 additions & 188 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.changeset/tasty-kiwis-roll.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@biomejs/biome": patch
3+
---
4+
5+
Improved [`noUnusedImports`](https://biomejs.dev/linter/rules/no-unused-imports/), [`noUnusedVariables`](https://biomejs.dev/linter/rules/no-unused-variables/), [`noUnusedFunctionParameters`](https://biomejs.dev/linter/rules/no-unused-function-parameters/), and [`useImportType`](https://biomejs.dev/linter/rules/use-import-type/) for Svelte, Vue, and Astro files (with `html.experimentalFullSupportEnabled`). Bindings used only in the template — including component tags, attribute interpolations, directives, `bind:` shorthand, and snippet parameters — are no longer reported as unused, while genuinely unused ones still are.

crates/biome_cli/tests/cases/handle_vue_files.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1100,7 +1100,7 @@ fn unused_suppression_has_correct_span_in_vue_file() {
11001100
fs.insert(
11011101
file.into(),
11021102
r#"<template>
1103-
<div>Hello</div>
1103+
<div>{{ mdiSquareOutline }}</div>
11041104
</template>
11051105
11061106
<script lang="ts" setup>

crates/biome_cli/tests/snapshots/main_cases_handle_astro_files/full_support.snap

Lines changed: 75 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
source: crates/biome_cli/tests/snap_test.rs
3+
assertion_line: 520
34
expression: redactor(content)
45
---
56
## `biome.json`
@@ -18,7 +19,6 @@ expression: redactor(content)
1819

1920
```astro
2021
---
21-
import s from "src/utils";
2222
import { sure } from "sure.js";
2323
import z from "zod";
2424
@@ -92,108 +92,125 @@ check ━━━━━━━━━━━━━━━━━━━━━━━━
9292
# Emitted Messages
9393

9494
```block
95-
file.astro:16:11 lint/correctness/noUnusedVariables ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
95+
file.astro:15:11 lint/correctness/noUnusedVariables ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
9696
9797
! This interface Foo is unused.
9898
99-
15 │ // Still reported as unused
100-
> 16 │ interface Foo {
99+
14 │ // Still reported as unused
100+
> 15 │ interface Foo {
101101
^^^
102-
17name: string;
103-
18}
102+
16name: string;
103+
17}
104104
105105
i Unused variables are often the result of typos, incomplete refactors, or other sources of bugs.
106106
107107
108108
```
109109

110110
```block
111-
file.astro:20:6 lint/correctness/noUnusedVariables ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
111+
file.astro:19:6 lint/correctness/noUnusedVariables ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
112112
113113
! This type alias Bar is unused.
114114
115-
18 │ }
116-
19
117-
> 20 │ type Bar = {
115+
17 │ }
116+
18
117+
> 19 │ type Bar = {
118118
^^^
119-
21name: string;
120-
22};
119+
20name: string;
120+
21};
121121
122122
i Unused variables are often the result of typos, incomplete refactors, or other sources of bugs.
123123
124124
125125
```
126126

127127
```block
128-
file.astro:26:12 lint/correctness/noUnusedVariables ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
128+
file.astro:25:12 lint/correctness/noUnusedVariables ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
129129
130130
! This interface Props is unused.
131131
132-
24 │ function _doSomething() {
133-
25// Still reported as unused, Props interface must be at top-level
134-
> 26interface Props {
132+
23 │ function _doSomething() {
133+
24// Still reported as unused, Props interface must be at top-level
134+
> 25interface Props {
135135
^^^^^
136-
27 │ name: string;
137-
28 │ }
136+
26 │ name: string;
137+
27 │ }
138138
139139
i Unused variables are often the result of typos, incomplete refactors, or other sources of bugs.
140140
141141
142142
```
143143
144144
```block
145-
file.astro:30:7 lint/correctness/noUnusedVariables ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
145+
file.astro:29:7 lint/correctness/noUnusedVariables ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
146146
147147
! This type alias Props is unused.
148148
149-
28}
150-
29
151-
> 30 │ type Props = {
149+
27}
150+
28
151+
> 29 │ type Props = {
152152
^^^^^
153-
31name: string;
154-
32};
153+
30name: string;
154+
31};
155155
156156
i Unused variables are often the result of typos, incomplete refactors, or other sources of bugs.
157157
158158
159159
```
160160

161161
```block
162-
file.astro:41:1 lint/a11y/useHtmlLang ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
162+
file.astro:34:9 lint/correctness/noUnusedVariables ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
163+
164+
! This variable name is unused.
165+
166+
32 │ }
167+
33 │
168+
> 34 │ const { name } = Astro.props;
169+
│ ^^^^
170+
35 │
171+
36 │ let schema = z.object().optional();
172+
173+
i Unused variables are often the result of typos, incomplete refactors, or other sources of bugs.
174+
175+
176+
```
177+
178+
```block
179+
file.astro:40:1 lint/a11y/useHtmlLang ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
163180
164181
× Provide a lang attribute when using the html element.
165182
166-
39 │ ---
167-
40
168-
> 41 │ <html>
183+
38 │ ---
184+
39
185+
> 40 │ <html>
169186
│ ^^^^^^
170-
42 │ <head>
171-
43 │ <title>Astro</title>
187+
41 │ <head>
188+
42 │ <title>Astro</title>
172189
173190
i Setting a lang attribute on HTML document elements configures the language used by screen readers when no user default is specified.
174191
175192
176193
```
177194
178195
```block
179-
file.astro:11:6 lint/suspicious/noRedeclare ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
196+
file.astro:10:6 lint/suspicious/noRedeclare ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
180197
181198
× 'Props' is redeclared in the same scope.
182199
183-
9 │ }
184-
10
185-
> 11 │ type Props = {
200+
8 │ }
201+
9
202+
> 10 │ type Props = {
186203
^^^^^
187-
12name: string;
188-
13};
204+
11name: string;
205+
12};
189206
190207
i 'Props' is defined here:
191208
192-
6 │ // Always considered as used
193-
> 7 │ interface Props {
209+
5 │ // Always considered as used
210+
> 6 │ interface Props {
194211
^^^^^
195-
8name: string;
196-
9}
212+
7name: string;
213+
8}
197214
198215
i Redeclarations make it unclear which variable a reference points to and can hide earlier declarations.
199216
@@ -203,25 +220,25 @@ file.astro:11:6 lint/suspicious/noRedeclare ━━━━━━━━━━━━
203220
```
204221
205222
```block
206-
file.astro:30:7 lint/suspicious/noRedeclare ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
223+
file.astro:29:7 lint/suspicious/noRedeclare ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
207224
208225
× 'Props' is redeclared in the same scope.
209226
210-
28 │ }
211-
29
212-
> 30 │ type Props = {
227+
27 │ }
228+
28
229+
> 29 │ type Props = {
213230
^^^^^
214-
31name: string;
215-
32};
231+
30name: string;
232+
31};
216233
217234
i 'Props' is defined here:
218235
219-
24 │ function _doSomething() {
220-
25// Still reported as unused, Props interface must be at top-level
221-
> 26interface Props {
236+
23 │ function _doSomething() {
237+
24// Still reported as unused, Props interface must be at top-level
238+
> 25interface Props {
222239
^^^^^
223-
27 │ name: string;
224-
28 │ }
240+
26 │ name: string;
241+
27 │ }
225242
226243
i Redeclarations make it unclear which variable a reference points to and can hide earlier declarations.
227244
@@ -231,16 +248,16 @@ file.astro:30:7 lint/suspicious/noRedeclare ━━━━━━━━━━━━
231248
```
232249
233250
```block
234-
file.astro:50:15 lint/a11y/useGenericFontNames ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
251+
file.astro:49:15 lint/a11y/useGenericFontNames ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
235252
236253
× Generic font family missing.
237254
238-
48 │ <style>
239-
49 │ #id {
240-
> 50font-family: comic-sans;
255+
47 │ <style>
256+
48 │ #id {
257+
> 49font-family: comic-sans;
241258
^^^^^^^^^^
242-
51}
243-
52 │ .class {
259+
50}
260+
51 │ .class {
244261
245262
i Consider adding a generic font family as a fallback.
246263
@@ -257,5 +274,5 @@ file.astro:50:15 lint/a11y/useGenericFontNames ━━━━━━━━━━━
257274
```block
258275
Checked 1 file in <TIME>. Fixed 1 file.
259276
Found 4 errors.
260-
Found 4 warnings.
277+
Found 5 warnings.
261278
```

crates/biome_cli/tests/snapshots/main_cases_handle_astro_files/return_in_template_expression_should_error.snap

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
source: crates/biome_cli/tests/snap_test.rs
3+
assertion_line: 520
34
expression: redactor(content)
45
---
56
## `biome.json`
@@ -35,6 +36,28 @@ lint ━━━━━━━━━━━━━━━━━━━━━━━━━
3536

3637
# Emitted Messages
3738

39+
```block
40+
file.astro:2:7 lint/correctness/noUnusedVariables FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
41+
42+
! This variable x is unused.
43+
44+
1 │ ---
45+
> 2 │ const x = 5;
46+
│ ^
47+
3 │ ---
48+
4 │ <div>{ return x }</div>
49+
50+
i Unused variables are often the result of typos, incomplete refactors, or other sources of bugs.
51+
52+
i Unsafe fix: If this is intentional, prepend x with an underscore.
53+
54+
1 │ - const·x·=·5;
55+
1 │ + const·_x·=·5;
56+
2 2 │
57+
58+
59+
```
60+
3861
```block
3962
file.astro:4:8 parse ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
4063
@@ -53,4 +76,5 @@ file.astro:4:8 parse ━━━━━━━━━━━━━━━━━━━
5376
```block
5477
Checked 1 file in <TIME>. No fixes applied.
5578
Found 1 error.
79+
Found 1 warning.
5680
```

crates/biome_cli/tests/snapshots/main_cases_handle_svelte_files/full_support.snap

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
source: crates/biome_cli/tests/snap_test.rs
3+
assertion_line: 520
34
expression: redactor(content)
45
---
56
## `biome.json`
@@ -18,7 +19,6 @@ expression: redactor(content)
1819

1920
```svelte
2021
<script>
21-
import s from "src/utils";
2222
import { sure } from "sure.js";
2323
import z from "zod";
2424
@@ -61,33 +61,33 @@ check ━━━━━━━━━━━━━━━━━━━━━━━━
6161
# Emitted Messages
6262

6363
```block
64-
file.svelte:10:1 lint/a11y/useHtmlLang ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
64+
file.svelte:9:1 lint/a11y/useHtmlLang ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
6565
6666
× Provide a lang attribute when using the html element.
6767
68-
8 │ </script>
69-
9
70-
> 10 │ <html>
68+
7 │ </script>
69+
8
70+
> 9 │ <html>
7171
│ ^^^^^^
72-
11 │ <head>
73-
12 │ <title>Svelte</title>
72+
10 │ <head>
73+
11 │ <title>Svelte</title>
7474
7575
i Setting a lang attribute on HTML document elements configures the language used by screen readers when no user default is specified.
7676
7777
7878
```
7979
8080
```block
81-
file.svelte:19:15 lint/a11y/useGenericFontNames ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
81+
file.svelte:18:15 lint/a11y/useGenericFontNames ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
8282
8383
× Generic font family missing.
8484
85-
17 │ <style>
86-
18 │ #id {
87-
> 19font-family: comic-sans;
85+
16 │ <style>
86+
17 │ #id {
87+
> 18font-family: comic-sans;
8888
^^^^^^^^^^
89-
20}
90-
21 │ .class {
89+
19}
90+
20 │ .class {
9191
9292
i Consider adding a generic font family as a fallback.
9393

0 commit comments

Comments
 (0)