Skip to content

Commit 086a0c5

Browse files
authored
fix(linter): skip useDestructuring for variables with type annotations (#8975)
1 parent f5b0e8d commit 086a0c5

6 files changed

Lines changed: 147 additions & 0 deletions

File tree

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+
Fixed [#8478](https://github.com/biomejs/biome/issues/8478): [`useDestructuring`](https://biomejs.dev/linter/rules/use-destructuring/) no longer suggests destructuring when the variable has a type annotation, like `const foo: string = object.foo`.

crates/biome_js_analyze/src/lint/nursery/use_destructuring.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ declare_lint_rule! {
3939
/// var { bar } = foo;
4040
/// ```
4141
///
42+
/// ```ts
43+
/// // Variables with type annotations are ignored
44+
/// const foo: string = object.foo;
45+
/// ```
46+
///
4247
pub UseDestructuring {
4348
version: "2.3.9",
4449
name: "useDestructuring",
@@ -84,6 +89,10 @@ impl Rule for UseDestructuring {
8489
return None;
8590
}
8691

92+
if node.variable_annotation().is_some() {
93+
return None;
94+
}
95+
8796
let left = node.id().ok()?;
8897
let right = initializer.expression().ok()?;
8998

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* should generate diagnostics */
2+
3+
// No type annotation — should still suggest destructuring
4+
{
5+
const foo = object.foo;
6+
}
7+
{
8+
let foo = array[0];
9+
}
10+
{
11+
var foo = object['foo'];
12+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
---
2+
source: crates/biome_js_analyze/tests/spec_tests.rs
3+
expression: invalid.ts
4+
---
5+
# Input
6+
```ts
7+
/* should generate diagnostics */
8+
9+
// No type annotation — should still suggest destructuring
10+
{
11+
const foo = object.foo;
12+
}
13+
{
14+
let foo = array[0];
15+
}
16+
{
17+
var foo = object['foo'];
18+
}
19+
20+
```
21+
22+
# Diagnostics
23+
```
24+
invalid.ts:5:8 lint/nursery/useDestructuring ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
25+
26+
i Use object destructuring instead of accessing object properties.
27+
28+
3 │ // No type annotation — should still suggest destructuring
29+
4 │ {
30+
> 5const foo = object.foo;
31+
^^^^^^^^^^^^^^^^
32+
6}
33+
7 │ {
34+
35+
i Object destructuring is more readable and expressive than accessing individual properties.
36+
37+
i Replace the property access with object destructuring syntax.
38+
39+
i This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
40+
41+
42+
```
43+
44+
```
45+
invalid.ts:8:6 lint/nursery/useDestructuring ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
46+
47+
i Use array destructuring instead of accessing array elements by index.
48+
49+
6}
50+
7 │ {
51+
> 8let foo = array[0];
52+
^^^^^^^^^^^^^^
53+
9}
54+
10 │ {
55+
56+
i Array destructuring is more readable and expressive than accessing individual elements by index.
57+
58+
i Replace the array index access with array destructuring syntax.
59+
60+
i This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.
61+
62+
63+
```
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/* should not generate diagnostics */
2+
3+
// Type annotation on variable — should not suggest destructuring
4+
{
5+
const foo: string = object.foo;
6+
}
7+
{
8+
let foo: number = array[0];
9+
}
10+
{
11+
var foo: string = object['foo'];
12+
}
13+
{
14+
const foo: Foo = object.bar.foo;
15+
}
16+
17+
// Definite assignment annotation — should not suggest destructuring
18+
{
19+
let foo!: string;
20+
}
21+
22+
// as const with type annotation
23+
{
24+
const y: number = x.y;
25+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
source: crates/biome_js_analyze/tests/spec_tests.rs
3+
expression: valid.ts
4+
---
5+
# Input
6+
```ts
7+
/* should not generate diagnostics */
8+
9+
// Type annotation on variable — should not suggest destructuring
10+
{
11+
const foo: string = object.foo;
12+
}
13+
{
14+
let foo: number = array[0];
15+
}
16+
{
17+
var foo: string = object['foo'];
18+
}
19+
{
20+
const foo: Foo = object.bar.foo;
21+
}
22+
23+
// Definite assignment annotation — should not suggest destructuring
24+
{
25+
let foo!: string;
26+
}
27+
28+
// as const with type annotation
29+
{
30+
const y: number = x.y;
31+
}
32+
33+
```

0 commit comments

Comments
 (0)