perf(minifier): reuse is_error_constructor and simplify substitute_global_new_expression (#28149)

This commit is contained in:
Armano authored and GitHub committed 2026-10-24 18:13:04 -07:00
1 parent a22c9f2abc
commit 3493ce30c4
3 files changed
+14 -26

No files matched your search

@@ -154,8 +154,10 @@ pub(super) fn is_unconditionally_pure_constructor(name: &str) -> bool {
}
/// Whether the name is an Error constructor.
///
/// See <https://tc39.es/ecma262/multipage/fundamental-objects.html#sec-native-error-types-used-in-this-standard> for the list of native errors.
#[rustfmt::skip]
pub(super) fn is_error_constructor(name: &str) -> bool {
pub fn is_error_constructor(name: &str) -> bool {
matches!(name, "Error" | "EvalError" | "RangeError" | "ReferenceError"
| "SyntaxError" | "TypeError" | "URIError")
}
@@ -5,7 +5,9 @@ mod pure_function;
mod statements;
pub use context::{MayHaveSideEffectsContext, PropertyReadSideEffects};
pub use known_globals::{is_regexp_syntax_supported, is_typed_array_constructor, is_valid_regexp};
pub use known_globals::{
is_error_constructor, is_regexp_syntax_supported, is_typed_array_constructor, is_valid_regexp,
};
pub use pure_function::is_pure_function;
/// Returns true if subtree changes application state.
@@ -9,17 +9,19 @@ use oxc_ecmascript::{
constant_evaluation::{
ConstantEvaluation, ConstantValue, DetermineValueType, evaluate_to_js_string,
},
side_effects::{MayHaveSideEffects, MayHaveSideEffectsContext, is_typed_array_constructor},
side_effects::{
MayHaveSideEffects, MayHaveSideEffectsContext, is_error_constructor,
is_typed_array_constructor,
},
};
use oxc_semantic::ReferenceFlags;
use oxc_span::GetSpan;
use oxc_span::SPAN;
use oxc_span::{GetSpan, SPAN};
use oxc_str::static_ident;
use oxc_syntax::precedence::GetPrecedence;
use oxc_syntax::{
identifier::is_identifier_name_patched,
number::NumberBase,
operator::{BinaryOperator, UnaryOperator},
precedence::GetPrecedence,
};
use crate::TraverseCtx;
@@ -1335,8 +1337,7 @@ impl<'a> PeepholeOptimizations {
let Expression::NewExpression(e) = expr else { return };
let Expression::Identifier(ident) = &e.callee else { return };
let name = ident.name.as_str();
if !matches!(name, "Error" | "AggregateError" | "Function" | "RegExp")
&& !Self::is_native_error_name(name)
if !matches!(name, "Function" | "RegExp" | "AggregateError") && !is_error_constructor(name)
{
return;
}
@@ -1353,29 +1354,12 @@ impl<'a> PeepholeOptimizations {
!ty.is_undetermined() && !ty.is_object()
}))
}
"Error" | "AggregateError" | "Function" => true,
_ if Self::is_native_error_name(name) => true,
_ => unreachable!(),
_ => true,
} {
ctx.replace_expression_with(expr, Self::transform_new_expr_to_call_expr);
}
}
/// Whether the name matches any native error name.
///
/// See <https://tc39.es/ecma262/multipage/fundamental-objects.html#sec-native-error-types-used-in-this-standard> for the list of native errors.
fn is_native_error_name(name: &str) -> bool {
matches!(
name,
"EvalError"
| "RangeError"
| "ReferenceError"
| "SyntaxError"
| "TypeError"
| "URIError"
)
}
pub fn substitute_chain_call_expression(
expr: &mut ChainExpression<'a>,
ctx: &mut TraverseCtx<'a>,