Skip to content

Commit d5a8f18

Browse files
committed
refactor(minifier): make Ctx take &mut TraverseCtx (#11771)
1 parent 8ef1be2 commit d5a8f18

18 files changed

+339
-298
lines changed

crates/oxc_minifier/src/ctx.rs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,16 @@ pub struct MinifierState<'a> {
1515

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

18-
#[derive(Clone, Copy)]
19-
pub struct Ctx<'a, 'b>(pub &'b TraverseCtx<'a>);
18+
pub struct Ctx<'a, 'b>(&'b mut TraverseCtx<'a>);
19+
20+
impl<'a, 'b> Ctx<'a, 'b> {
21+
pub fn new(ctx: &'b mut TraverseCtx<'a>) -> Self {
22+
Self(ctx)
23+
}
24+
}
2025

2126
impl<'a, 'b> Deref for Ctx<'a, 'b> {
22-
type Target = &'b TraverseCtx<'a>;
27+
type Target = &'b mut TraverseCtx<'a>;
2328

2429
fn deref(&self) -> &Self::Target {
2530
&self.0
@@ -65,28 +70,28 @@ impl<'a> Ctx<'a, '_> {
6570
self.0.scoping()
6671
}
6772

68-
pub fn is_global_reference(self, ident: &IdentifierReference<'a>) -> bool {
73+
pub fn is_global_reference(&self, ident: &IdentifierReference<'a>) -> bool {
6974
ident.is_global_reference(self.0.scoping())
7075
}
7176

72-
pub fn eval_binary(self, e: &BinaryExpression<'a>) -> Option<Expression<'a>> {
73-
if e.may_have_side_effects(&self) {
77+
pub fn eval_binary(&self, e: &BinaryExpression<'a>) -> Option<Expression<'a>> {
78+
if e.may_have_side_effects(self) {
7479
None
7580
} else {
76-
e.evaluate_value(&self).map(|v| self.value_to_expr(e.span, v))
81+
e.evaluate_value(self).map(|v| self.value_to_expr(e.span, v))
7782
}
7883
}
7984

8085
pub fn eval_binary_operation(
81-
self,
86+
&self,
8287
operator: BinaryOperator,
8388
left: &Expression<'a>,
8489
right: &Expression<'a>,
8590
) -> Option<ConstantValue<'a>> {
86-
binary_operation_evaluate_value(operator, left, right, &self)
91+
binary_operation_evaluate_value(operator, left, right, self)
8792
}
8893

89-
pub fn value_to_expr(self, span: Span, value: ConstantValue<'a>) -> Expression<'a> {
94+
pub fn value_to_expr(&self, span: Span, value: ConstantValue<'a>) -> Expression<'a> {
9095
match value {
9196
ConstantValue::Number(n) => {
9297
let number_base =
@@ -106,7 +111,7 @@ impl<'a> Ctx<'a, '_> {
106111
}
107112
}
108113

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

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

127132
/// If two expressions are equal.
128133
/// Special case `undefined` == `void 0`
129-
pub fn expr_eq(self, a: &Expression<'a>, b: &Expression<'a>) -> bool {
134+
pub fn expr_eq(&self, a: &Expression<'a>, b: &Expression<'a>) -> bool {
130135
use oxc_span::ContentEq;
131136
a.content_eq(b) || (self.is_expression_undefined(a) && self.is_expression_undefined(b))
132137
}

crates/oxc_minifier/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! ECMAScript Minifier
22
3-
#![allow(clippy::literal_string_with_formatting_args)]
3+
#![allow(clippy::literal_string_with_formatting_args, clippy::needless_pass_by_ref_mut)]
44

55
mod compressor;
66
mod ctx;

crates/oxc_minifier/src/peephole/convert_to_dotted_properties.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ impl<'a> LatePeepholeOptimizations {
1414
///
1515
/// `foo['bar']` -> `foo.bar`
1616
/// `foo?.['bar']` -> `foo?.bar`
17-
pub fn convert_to_dotted_properties(expr: &mut MemberExpression<'a>, ctx: Ctx<'a, '_>) {
17+
pub fn convert_to_dotted_properties(expr: &mut MemberExpression<'a>, ctx: &mut Ctx<'a, '_>) {
1818
let MemberExpression::ComputedMemberExpression(e) = expr else { return };
1919
let Expression::StringLiteral(s) = &e.expression else { return };
2020
if is_identifier_name(&s.value) {

0 commit comments

Comments
 (0)