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
31 changes: 18 additions & 13 deletions crates/oxc_minifier/src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@ pub struct MinifierState<'a> {

pub type TraverseCtx<'a> = oxc_traverse::TraverseCtx<'a, MinifierState<'a>>;

#[derive(Clone, Copy)]
pub struct Ctx<'a, 'b>(pub &'b TraverseCtx<'a>);
pub struct Ctx<'a, 'b>(&'b mut TraverseCtx<'a>);

impl<'a, 'b> Ctx<'a, 'b> {
pub fn new(ctx: &'b mut TraverseCtx<'a>) -> Self {
Self(ctx)
}
}

impl<'a, 'b> Deref for Ctx<'a, 'b> {
type Target = &'b TraverseCtx<'a>;
type Target = &'b mut TraverseCtx<'a>;

fn deref(&self) -> &Self::Target {
&self.0
Expand Down Expand Up @@ -65,28 +70,28 @@ impl<'a> Ctx<'a, '_> {
self.0.scoping()
}

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

pub fn eval_binary(self, e: &BinaryExpression<'a>) -> Option<Expression<'a>> {
if e.may_have_side_effects(&self) {
pub fn eval_binary(&self, e: &BinaryExpression<'a>) -> Option<Expression<'a>> {
if e.may_have_side_effects(self) {
None
} else {
e.evaluate_value(&self).map(|v| self.value_to_expr(e.span, v))
e.evaluate_value(self).map(|v| self.value_to_expr(e.span, v))
}
}

pub fn eval_binary_operation(
self,
&self,
operator: BinaryOperator,
left: &Expression<'a>,
right: &Expression<'a>,
) -> Option<ConstantValue<'a>> {
binary_operation_evaluate_value(operator, left, right, &self)
binary_operation_evaluate_value(operator, left, right, self)
}

pub fn value_to_expr(self, span: Span, value: ConstantValue<'a>) -> Expression<'a> {
pub fn value_to_expr(&self, span: Span, value: ConstantValue<'a>) -> Expression<'a> {
match value {
ConstantValue::Number(n) => {
let number_base =
Expand All @@ -106,7 +111,7 @@ impl<'a> Ctx<'a, '_> {
}
}

pub fn is_expression_undefined(self, expr: &Expression) -> bool {
pub fn is_expression_undefined(&self, expr: &Expression) -> bool {
match expr {
Expression::Identifier(ident) if self.is_identifier_undefined(ident) => true,
Expression::UnaryExpression(e) if e.operator.is_void() && e.argument.is_number() => {
Expand All @@ -117,7 +122,7 @@ impl<'a> Ctx<'a, '_> {
}

#[inline]
pub fn is_identifier_undefined(self, ident: &IdentifierReference) -> bool {
pub fn is_identifier_undefined(&self, ident: &IdentifierReference) -> bool {
if ident.name == "undefined" && ident.is_global_reference(self.scoping()) {
return true;
}
Expand All @@ -126,7 +131,7 @@ impl<'a> Ctx<'a, '_> {

/// If two expressions are equal.
/// Special case `undefined` == `void 0`
pub fn expr_eq(self, a: &Expression<'a>, b: &Expression<'a>) -> bool {
pub fn expr_eq(&self, a: &Expression<'a>, b: &Expression<'a>) -> bool {
use oxc_span::ContentEq;
a.content_eq(b) || (self.is_expression_undefined(a) && self.is_expression_undefined(b))
}
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_minifier/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! ECMAScript Minifier

#![allow(clippy::literal_string_with_formatting_args)]
#![allow(clippy::literal_string_with_formatting_args, clippy::needless_pass_by_ref_mut)]

mod compressor;
mod ctx;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl<'a> LatePeepholeOptimizations {
///
/// `foo['bar']` -> `foo.bar`
/// `foo?.['bar']` -> `foo?.bar`
pub fn convert_to_dotted_properties(expr: &mut MemberExpression<'a>, ctx: Ctx<'a, '_>) {
pub fn convert_to_dotted_properties(expr: &mut MemberExpression<'a>, ctx: &mut Ctx<'a, '_>) {
let MemberExpression::ComputedMemberExpression(e) = expr else { return };
let Expression::StringLiteral(s) = &e.expression else { return };
if is_identifier_name(&s.value) {
Expand Down
Loading
Loading