Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/neat-items-tan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@biomejs/biome": patch
---

Improved the performance of `noRedeclare` by eliminating string allocations
24 changes: 13 additions & 11 deletions crates/biome_js_analyze/src/lint/suspicious/no_redeclare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use biome_diagnostics::Severity;
use biome_js_semantic::Scope;
use biome_js_syntax::binding_ext::AnyJsBindingDeclaration;
use biome_js_syntax::{JsSyntaxKind, TextRange};
use biome_rowan::AstNode;
use biome_rowan::{AstNode, TokenText};
use biome_rule_options::no_redeclare::NoRedeclareOptions;
use rustc_hash::FxHashMap;

Expand Down Expand Up @@ -75,7 +75,7 @@ declare_lint_rule! {

#[derive(Debug)]
pub struct Redeclaration {
name: Box<str>,
name: TokenText,
declaration: TextRange,
redeclaration: TextRange,
}
Expand Down Expand Up @@ -118,7 +118,7 @@ impl Rule for NoRedeclare {
}

fn check_redeclarations_in_single_scope(scope: &Scope, redeclarations: &mut Vec<Redeclaration>) {
let mut declarations = FxHashMap::<Box<str>, (TextRange, AnyJsBindingDeclaration)>::default();
let mut declarations = FxHashMap::<TokenText, (TextRange, AnyJsBindingDeclaration)>::default();
if scope.syntax().kind() == JsSyntaxKind::JS_FUNCTION_BODY {
// Handle cases where a variable/type redeclares a parameter or type parameter.
// For example:
Expand Down Expand Up @@ -146,9 +146,11 @@ fn check_redeclarations_in_single_scope(scope: &Scope, redeclarations: &mut Vec<
if let Some(decl) = id_binding.declaration() {
// Ignore the function itself.
if !matches!(decl, AnyJsBindingDeclaration::JsFunctionExpression(_)) {
let name = id_binding.to_trimmed_text();
let Ok(name) = id_binding.name_token() else {
continue;
};
declarations.insert(
name.text().into(),
name.token_text_trimmed(),
(id_binding.syntax().text_trimmed_range(), decl),
);
}
Expand All @@ -162,7 +164,10 @@ fn check_redeclarations_in_single_scope(scope: &Scope, redeclarations: &mut Vec<
// We consider only binding of a declaration
// This allows to skip function parameters, methods, ...
if let Some(decl) = id_binding.declaration() {
let name = id_binding.to_trimmed_text();
let Ok(name) = id_binding.name_token() else {
continue;
};
let name = name.token_text_trimmed();
if let Some((first_text_range, first_decl)) = declarations.get(name.text()) {
// Do not report:
// - mergeable declarations.
Expand All @@ -181,16 +186,13 @@ fn check_redeclarations_in_single_scope(scope: &Scope, redeclarations: &mut Vec<
|| first_decl.is_infer_type() && decl.is_infer_type())
{
redeclarations.push(Redeclaration {
name: name.text().into(),
name,
declaration: *first_text_range,
redeclaration: id_binding.syntax().text_trimmed_range(),
})
}
} else {
declarations.insert(
name.text().into(),
(id_binding.syntax().text_trimmed_range(), decl),
);
declarations.insert(name, (id_binding.syntax().text_trimmed_range(), decl));
}
}
}
Expand Down
Loading