Skip to content

Commit

Permalink
fix(noUnusedVariables): don't panic when renaming ref at the start
Browse files Browse the repository at this point in the history
  • Loading branch information
Conaclos committed Sep 30, 2024
1 parent 53f1420 commit 06e7246
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 4 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ our [guidelines for writing a good changelog entry](https://github.com/biomejs/b

Contributed by @Conaclos

- [noUnusedVariables](https://biomejs.dev/linter/rules/no-unused-variables/) no longer panics when suggesting the renaming of a variable at the start of a file ([#4114](https://github.com/biomejs/biome/issues/4114)). Contributed by @Conaclos

- [noUselessEscapeInRegex](https://biomejs.dev/linter/rules/no-useless-escape-in-regex/) no longer panics on regexes that start with an empty character class. Contributed by @Conaclos

- [noUselessStringConcat](https://biomejs.dev/linter/rules/no-useless-string-concat/) no longer panics when it encounters malformed code. Contributed by @Conaclos
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
c;
const c = 0;
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
source: crates/biome_js_analyze/tests/spec_tests.rs
expression: issue4114.js
---
# Input
```jsx
c;
const c = 0;
```

# Diagnostics
```
issue4114.js:2:7 lint/correctness/noUnusedVariables FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
! This variable is unused.
1 │ c;
> 2 │ const c = 0;
│ ^
i Unused variables usually are result of incomplete refactoring, typos and other source of bugs.
i Unsafe fix: If this is intentional, prepend c with an underscore.
1 │ - c;
2 │ - const·c·=·0;
1 │ + _c;
2 │ + const·_c·=·0;
```
5 changes: 4 additions & 1 deletion crates/biome_js_semantic/src/semantic_model/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,16 @@ impl SemanticModelData {

/// Returns the [ScopeId] which the syntax is part of.
pub(crate) fn scope(&self, range: TextRange) -> ScopeId {
// Seeking an interval in `self.scope_by_range` require a non-empty interval
debug_assert!(range.len() > 0.into(), "the range must not be empty.");
let start = range.start().into();
let end = range.end().into();
let scopes = self
.scope_by_range
// Find overlapping intervals
.find(start, end)
// Only take intersecting intervals
.filter(|x| !(start < x.start || end > x.stop));

// We always want the most tight scope
match scopes.map(|x| x.val).max() {
Some(val) => val,
Expand Down
8 changes: 5 additions & 3 deletions crates/biome_js_semantic/src/semantic_model/reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,11 @@ impl Reference {

/// Returns the scope of this reference
pub fn scope(&self) -> Scope {
let id = self
.data
.scope(TextRange::new(self.range_start(), self.range_start()));
let start = self.range_start();
let id = self.data.scope(TextRange::new(
start,
start.checked_add(1.into()).unwrap_or(start),
));
Scope {
data: self.data.clone(),
id,
Expand Down

0 comments on commit 06e7246

Please sign in to comment.