Skip to content

Commit 72ebadc

Browse files
authored
fix: remove infinite loop on function A(A) {} (#6976)
1 parent 7c18d84 commit 72ebadc

File tree

10 files changed

+71
-2
lines changed

10 files changed

+71
-2
lines changed

.changeset/evil-mangos-type.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+
Fixed [#6692](https://github.com/biomejs/biome/issues/6692): The rules `noUnusedVariables` and `noUnusedFunctionParameters` no longer cause an infinite loop when the suggested name is not applicable (e.g. the suggested name is already declared in the scope).

crates/biome_js_analyze/src/lint/correctness/no_unused_function_parameters.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,9 @@ impl Rule for NoUnusedFunctionParameters {
216216
let new_name = format!("_{name_trimmed}");
217217

218218
let model = ctx.model();
219-
mutation.rename_node_declaration(model, binding, &new_name);
219+
if !mutation.rename_node_declaration(model, binding, &new_name) {
220+
return None;
221+
}
220222

221223
Some(JsRuleAction::new(
222224
ctx.metadata().action_category(ctx.category(), ctx.group()),

crates/biome_js_analyze/src/lint/correctness/no_unused_variables.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,9 @@ impl Rule for NoUnusedVariables {
373373
let new_name = format!("_{name_trimmed}");
374374

375375
let model = ctx.model();
376-
mutation.rename_node_declaration(model, binding, &new_name);
376+
if !mutation.rename_node_declaration(model, binding, &new_name) {
377+
return None;
378+
}
377379

378380
Some(JsRuleAction::new(
379381
ctx.metadata().action_category(ctx.category(), ctx.group()),

crates/biome_js_analyze/src/utils/rename.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ impl TryFrom<JsSyntaxNode> for AnyJsRenamableDeclaration {
156156

157157
pub trait RenameSymbolExtensions {
158158
/// Rename the binding and all its references to "new_name".
159+
#[must_use = "Check the return value and ignore the mutation if false was returned"]
159160
fn rename_node_declaration(
160161
&mut self,
161162
model: &SemanticModel,

crates/biome_js_analyze/tests/spec_tests.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,8 @@ fn check_code_action(
280280
options: JsParserOptions,
281281
root: &AnyJsRoot,
282282
) {
283+
assert!(!action.mutation.is_empty(), "Mutation must not be empty");
284+
283285
let (new_tree, text_edit) = match action
284286
.mutation
285287
.clone()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
function _A(A) {}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
source: crates/biome_js_analyze/tests/spec_tests.rs
3+
expression: issue6692.js
4+
---
5+
# Input
6+
```js
7+
function _A(A) {}
8+
9+
```
10+
11+
# Diagnostics
12+
```
13+
issue6692.js:1:13 lint/correctness/noUnusedFunctionParameters ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
14+
15+
! This parameter is unused.
16+
17+
> 1 │ function _A(A) {}
18+
│ ^
19+
2 │
20+
21+
i Unused parameters might be the result of an incomplete refactoring.
22+
23+
24+
```
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
function _A() {}
2+
function A() {}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
---
2+
source: crates/biome_js_analyze/tests/spec_tests.rs
3+
expression: issue6692.js
4+
---
5+
# Input
6+
```js
7+
function _A() {}
8+
function A() {}
9+
10+
```
11+
12+
# Diagnostics
13+
```
14+
issue6692.js:2:10 lint/correctness/noUnusedVariables ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
15+
16+
! This function A is unused.
17+
18+
1 │ function _A() {}
19+
> 2 │ function A() {}
20+
│ ^
21+
3 │
22+
23+
i Unused variables are often the result of an incomplete refactoring, typos, or other sources of bugs.
24+
25+
26+
```

crates/biome_rowan/src/ast/batch.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,10 @@ where
555555
pub fn root(&self) -> &SyntaxNode<L> {
556556
&self.root
557557
}
558+
559+
pub fn is_empty(&self) -> bool {
560+
self.changes.is_empty()
561+
}
558562
}
559563

560564
#[cfg(test)]

0 commit comments

Comments
 (0)