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(noUnusedVariables): don't panic when renaming ref at the start #4144

Merged
merged 1 commit into from
Sep 30, 2024
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
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
9 changes: 6 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,12 @@ 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,
// SAFETY: A reference name has at least a length of 1 byte.
start + TextSize::from(1),
));
Scope {
data: self.data.clone(),
id,
Expand Down
Loading