perf(minifier): use ident when checking specific id names (#27208)

change `is_specific_id` to be generic and accept `PartialEq` of `Ident`
instead of casting it to str
This commit is contained in:
Armano authored and GitHub committed 2026-10-07 09:50:40 +01:00
1 parent bb412e9c90
commit 19c227d9fb
6 files changed
+22 -14

No files matched your search

+10 -2
View File
@@ -176,7 +176,11 @@ impl<'a> Expression<'a> {
}
/// Returns `true` if this [`Expression`] is an [`IdentifierReference`] with specified `name`.
pub fn is_specific_id(&self, name: &str) -> bool {
#[expect(clippy::needless_pass_by_value)]
pub fn is_specific_id<N>(&self, name: N) -> bool
where
Ident<'a>: PartialEq<N>,
{
match self.get_inner_expression() {
Expression::Identifier(ident) => ident.name == name,
_ => false,
@@ -530,7 +534,11 @@ impl<'a> PropertyKey<'a> {
}
/// Returns `true` if this property key is exactly equal to the given identifier name.
pub fn is_specific_id(&self, name: &str) -> bool {
#[expect(clippy::needless_pass_by_value)]
pub fn is_specific_id<N>(&self, name: N) -> bool
where
Ident<'a>: PartialEq<N>,
{
match self {
PropertyKey::StaticIdentifier(ident) => ident.name == name,
_ => false,
@@ -1034,7 +1034,7 @@ fn is_side_effect_free_unbound_identifier_ref<'a>(
bin_expr.operator,
BinaryOperator::Inequality | BinaryOperator::StrictInequality
)
&& unary.argument.is_specific_id(&ident.name)
&& unary.argument.is_specific_id(ident.name)
{
return true;
}
@@ -1070,7 +1070,7 @@ fn is_side_effect_free_unbound_identifier_ref<'a>(
bin_expr.operator,
BinaryOperator::LessThan | BinaryOperator::LessEqualThan
)
&& unary.argument.is_specific_id(&ident.name)
&& unary.argument.is_specific_id(ident.name)
{
return true;
}
@@ -140,7 +140,7 @@ impl Rule for PreferStructuredClone {
}
} else if is_method_call(call_expr, None, Some(&[function]), None, None)
|| is_method_call(call_expr, Some(&[function]), None, None, None)
|| call_expr.callee.is_specific_id(function)
|| call_expr.callee.is_specific_id(function.as_str())
{
ctx.diagnostic_with_suggestion(
prefer_structured_clone_diagnostic(call_expr.span),
@@ -362,7 +362,7 @@ impl<'a> PeepholeOptimizations {
// `(a = foo) != null ? a : b` -> `(a = foo) ?? b`
let maybe_same_id_expr =
if is_negate { &mut expr.consequent } else { &mut expr.alternate };
if maybe_same_id_expr.is_specific_id(&target_id_name) {
if maybe_same_id_expr.is_specific_id(target_id_name) {
return Some(Expression::new_logical_expression(
expr.span,
value_expr.take_in(ctx),
@@ -387,7 +387,7 @@ impl<'a> PeepholeOptimizations {
let expr_to_inject_optional_chaining =
if is_negate { &mut expr.consequent } else { &mut expr.alternate };
if Self::inject_optional_chaining_if_matched(
&target_id_name,
target_id_name,
value_expr,
expr_to_inject_optional_chaining,
ctx,
@@ -689,7 +689,7 @@ impl<'a> PeepholeOptimizations {
///
/// For `target_expr` = `a`, `expr` = `a.b`, this function changes `expr` to `a?.b` and returns true.
pub fn inject_optional_chaining_if_matched(
target_id_name: &str,
target_id_name: Ident<'a>,
expr_to_inject: &mut Expression<'a>,
expr: &mut Expression<'a>,
ctx: &mut TraverseCtx<'a>,
@@ -713,7 +713,7 @@ impl<'a> PeepholeOptimizations {
/// See [`Self::inject_optional_chaining_if_matched`]
fn inject_optional_chaining_if_matched_inner(
target_id_name: &str,
target_id_name: Ident<'a>,
expr_to_inject: &mut Expression<'a>,
expr: &mut Expression<'a>,
ctx: &mut TraverseCtx<'a>,
@@ -178,7 +178,7 @@ impl<'a> PeepholeOptimizations {
};
if let Some((name, id)) = name_and_id
&& Self::inject_optional_chaining_if_matched(
&name,
name,
id,
logical_right,
ctx,
@@ -692,12 +692,12 @@ impl<'a> PeepholeOptimizations {
return VerifyArrayArgResult::Invalid;
};
if test_expr.operator == BinaryOperator::GreaterThan
&& test_expr.left.is_specific_id(&name_e)
&& test_expr.left.is_specific_id(name_e)
&& matches!(&test_expr.right, Expression::NumericLiteral(n) if n.value == offset)
&& cons_expr.operator == BinaryOperator::Subtraction
&& matches!(&cons_expr.left, Expression::Identifier(id) if id.name == name_e)
&& matches!(&cons_expr.right, Expression::NumericLiteral(n) if n.value == offset)
&& matches!(&cond_expr.alternate, Expression::NumericLiteral(n) if n.value == 0.0)
&& cons_expr.left.is_specific_id(name_e)
&& cond_expr.alternate.is_number_0()
{
VerifyArrayArgResult::WithOffset
} else {
@@ -1166,7 +1166,7 @@ impl<'a> PeepholeOptimizations {
) -> Option<&'a str> {
match callee {
Expression::StaticMemberExpression(e) => {
if !matches!(&e.object, Expression::Identifier(ident) if ident.name == "window") {
if !e.object.is_specific_id("window") {
return None;
}
Some(e.property.name.as_str())