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
2 changes: 1 addition & 1 deletion crates/oxc_ecmascript/src/constant_evaluation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub use is_literal_value::IsLiteralValue;
pub use value::ConstantValue;
pub use value_type::ValueType;

pub trait ConstantEvaluation<'a>: MayHaveSideEffects<'a> {
pub trait ConstantEvaluation<'a>: MayHaveSideEffects {
fn resolve_binding(&self, ident: &IdentifierReference<'a>) -> Option<ConstantValue<'a>> {
match ident.name.as_str() {
"undefined" if self.is_global_reference(ident) => Some(ConstantValue::Undefined),
Expand Down
18 changes: 9 additions & 9 deletions crates/oxc_ecmascript/src/side_effects/may_have_side_effects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ use oxc_ast::ast::*;
/// Returns true if subtree changes application state.
///
/// Ported from [closure-compiler](https://github.com/google/closure-compiler/blob/f3ce5ed8b630428e311fe9aa2e20d36560d975e2/src/com/google/javascript/jscomp/AstAnalyzer.java#L94)
pub trait MayHaveSideEffects<'a> {
fn is_global_reference(&self, ident: &oxc_ast::ast::IdentifierReference<'a>) -> bool {
pub trait MayHaveSideEffects {
fn is_global_reference(&self, ident: &IdentifierReference<'_>) -> bool {
matches!(ident.name.as_str(), "undefined" | "NaN" | "Infinity")
}

fn expression_may_have_side_efffects(&self, e: &Expression<'a>) -> bool {
fn expression_may_have_side_efffects(&self, e: &Expression<'_>) -> bool {
match e {
// Reference read can have a side effect.
Expression::Identifier(ident) => self.is_global_reference(ident),
Expand Down Expand Up @@ -50,7 +50,7 @@ pub trait MayHaveSideEffects<'a> {
}
}

fn unary_expression_may_have_side_effects(&self, e: &UnaryExpression<'a>) -> bool {
fn unary_expression_may_have_side_effects(&self, e: &UnaryExpression<'_>) -> bool {
/// A "simple" operator is one whose children are expressions, has no direct side-effects.
fn is_simple_unary_operator(operator: UnaryOperator) -> bool {
operator != UnaryOperator::Delete
Expand All @@ -61,7 +61,7 @@ pub trait MayHaveSideEffects<'a> {
true
}

fn binary_expression_may_have_side_effects(&self, e: &BinaryExpression<'a>) -> bool {
fn binary_expression_may_have_side_effects(&self, e: &BinaryExpression<'_>) -> bool {
// `instanceof` and `in` can throw `TypeError`
if matches!(e.operator, BinaryOperator::In | BinaryOperator::Instanceof) {
return true;
Expand All @@ -72,7 +72,7 @@ pub trait MayHaveSideEffects<'a> {

fn array_expression_element_may_have_side_effects(
&self,
e: &ArrayExpressionElement<'a>,
e: &ArrayExpressionElement<'_>,
) -> bool {
match e {
ArrayExpressionElement::SpreadElement(e) => {
Expand All @@ -85,7 +85,7 @@ pub trait MayHaveSideEffects<'a> {
}
}

fn object_property_kind_may_have_side_effects(&self, e: &ObjectPropertyKind<'a>) -> bool {
fn object_property_kind_may_have_side_effects(&self, e: &ObjectPropertyKind<'_>) -> bool {
match e {
ObjectPropertyKind::ObjectProperty(o) => self.object_property_may_have_side_effects(o),
ObjectPropertyKind::SpreadProperty(e) => {
Expand All @@ -94,12 +94,12 @@ pub trait MayHaveSideEffects<'a> {
}
}

fn object_property_may_have_side_effects(&self, e: &ObjectProperty<'a>) -> bool {
fn object_property_may_have_side_effects(&self, e: &ObjectProperty<'_>) -> bool {
self.property_key_may_have_side_effects(&e.key)
|| self.expression_may_have_side_efffects(&e.value)
}

fn property_key_may_have_side_effects(&self, key: &PropertyKey<'a>) -> bool {
fn property_key_may_have_side_effects(&self, key: &PropertyKey<'_>) -> bool {
match key {
PropertyKey::StaticIdentifier(_) | PropertyKey::PrivateIdentifier(_) => false,
match_expression!(PropertyKey) => {
Expand Down
8 changes: 6 additions & 2 deletions crates/oxc_minifier/src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ impl<'a, 'b> Deref for Ctx<'a, 'b> {

impl<'a> ConstantEvaluation<'a> for Ctx<'a, '_> {}

impl<'a> MayHaveSideEffects<'a> for Ctx<'a, '_> {
fn is_global_reference(&self, ident: &IdentifierReference<'a>) -> bool {
impl MayHaveSideEffects for Ctx<'_, '_> {
fn is_global_reference(&self, ident: &IdentifierReference<'_>) -> bool {
ident.is_global_reference(self.0.symbols())
}
}
Expand All @@ -36,6 +36,10 @@ impl<'a> Ctx<'a, '_> {
self.0.symbols()
}

pub fn is_global_reference(self, ident: &IdentifierReference<'a>) -> bool {
ident.is_global_reference(self.0.symbols())
}

pub fn eval_binary(self, e: &BinaryExpression<'a>) -> Option<Expression<'a>> {
self.eval_binary_expression(e).map(|v| self.value_to_expr(e.span, v))
}
Expand Down
1 change: 0 additions & 1 deletion crates/oxc_minifier/src/peephole/minimize_conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use oxc_allocator::Vec;
use oxc_ast::{ast::*, NONE};
use oxc_ecmascript::{
constant_evaluation::{ConstantEvaluation, ValueType},
side_effects::MayHaveSideEffects,
ToInt32,
};
use oxc_span::{cmp::ContentEq, GetSpan};
Expand Down
1 change: 0 additions & 1 deletion crates/oxc_minifier/src/peephole/normalize.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use oxc_allocator::Vec;
use oxc_ast::ast::*;
use oxc_ecmascript::side_effects::MayHaveSideEffects;
use oxc_semantic::IsGlobalReference;
use oxc_span::GetSpan;
use oxc_syntax::scope::ScopeFlags;
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_minifier/src/peephole/replace_known_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use cow_utils::CowUtils;

use oxc_ast::ast::*;
use oxc_ecmascript::{
constant_evaluation::ConstantEvaluation, side_effects::MayHaveSideEffects, StringCharAt,
StringCharCodeAt, StringIndexOf, StringLastIndexOf, StringSubstring, ToInt32,
constant_evaluation::ConstantEvaluation, StringCharAt, StringCharCodeAt, StringIndexOf,
StringLastIndexOf, StringSubstring, ToInt32,
};
use oxc_span::SPAN;
use oxc_syntax::es_target::ESTarget;
Expand Down
Loading