From 46a38c63ff12cc9525c1eebac4458dcb237997b1 Mon Sep 17 00:00:00 2001 From: Boshen <1430279+Boshen@users.noreply.github.com> Date: Fri, 11 Oct 2024 02:54:24 +0000 Subject: [PATCH] refactor(minifier): remove allow `clippy::unused_self` (#6441) --- .../src/ast_passes/peephole_fold_constants.rs | 48 +++++---------- .../peephole_minimize_conditions.rs | 3 +- .../ast_passes/peephole_remove_dead_code.rs | 3 +- .../peephole_substitute_alternate_syntax.rs | 59 +++++++++---------- crates/oxc_minifier/src/compressor.rs | 4 +- crates/oxc_minifier/src/lib.rs | 2 +- 6 files changed, 48 insertions(+), 71 deletions(-) diff --git a/crates/oxc_minifier/src/ast_passes/peephole_fold_constants.rs b/crates/oxc_minifier/src/ast_passes/peephole_fold_constants.rs index 7e5b13cc6bfa0..b1f2010fff66e 100644 --- a/crates/oxc_minifier/src/ast_passes/peephole_fold_constants.rs +++ b/crates/oxc_minifier/src/ast_passes/peephole_fold_constants.rs @@ -45,17 +45,17 @@ impl<'a> Traverse<'a> for PeepholeFoldConstants { fn exit_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) { if let Some(folded_expr) = match expr { Expression::CallExpression(e) => { - self.try_fold_useless_object_dot_define_properties_call(e, ctx) + Self::try_fold_useless_object_dot_define_properties_call(e, ctx) } - Expression::NewExpression(e) => self.try_fold_ctor_cal(e, ctx), + Expression::NewExpression(e) => Self::try_fold_ctor_cal(e, ctx), // TODO // return tryFoldSpread(subtree); - Expression::ArrayExpression(e) => self.try_flatten_array_expression(e, ctx), - Expression::ObjectExpression(e) => self.try_flatten_object_expression(e, ctx), - Expression::BinaryExpression(e) => self.try_fold_binary_expression(e, ctx), + Expression::ArrayExpression(e) => Self::try_flatten_array_expression(e, ctx), + Expression::ObjectExpression(e) => Self::try_flatten_object_expression(e, ctx), + Expression::BinaryExpression(e) => Self::try_fold_binary_expression(e, ctx), Expression::UnaryExpression(e) => self.try_fold_unary_expression(e, ctx), // TODO: return tryFoldGetProp(subtree); - Expression::LogicalExpression(e) => self.try_fold_logical_expression(e, ctx), + Expression::LogicalExpression(e) => Self::try_fold_logical_expression(e, ctx), // TODO: tryFoldGetElem // TODO: tryFoldAssign _ => None, @@ -72,7 +72,6 @@ impl<'a> PeepholeFoldConstants { } fn try_fold_useless_object_dot_define_properties_call( - &mut self, _call_expr: &mut CallExpression<'a>, _ctx: &mut TraverseCtx<'a>, ) -> Option> { @@ -80,7 +79,6 @@ impl<'a> PeepholeFoldConstants { } fn try_fold_ctor_cal( - &mut self, _new_expr: &mut NewExpression<'a>, _ctx: &mut TraverseCtx<'a>, ) -> Option> { @@ -91,7 +89,6 @@ impl<'a> PeepholeFoldConstants { /// `typeof("bar") --> "string"` /// `typeof(6) --> "number"` fn try_fold_type_of( - &self, expr: &mut UnaryExpression<'a>, ctx: &mut TraverseCtx<'a>, ) -> Option> { @@ -124,7 +121,6 @@ impl<'a> PeepholeFoldConstants { // } fn try_flatten_array_expression( - &mut self, _new_expr: &mut ArrayExpression<'a>, _ctx: &mut TraverseCtx<'a>, ) -> Option> { @@ -132,7 +128,6 @@ impl<'a> PeepholeFoldConstants { } fn try_flatten_object_expression( - &mut self, _new_expr: &mut ObjectExpression<'a>, _ctx: &mut TraverseCtx<'a>, ) -> Option> { @@ -149,7 +144,7 @@ impl<'a> PeepholeFoldConstants { } match expr.operator { UnaryOperator::Void => self.try_reduce_void(expr, ctx), - UnaryOperator::Typeof => self.try_fold_type_of(expr, ctx), + UnaryOperator::Typeof => Self::try_fold_type_of(expr, ctx), // TODO: tryReduceOperandsForOp #[allow(clippy::float_cmp)] UnaryOperator::LogicalNot => { @@ -273,13 +268,12 @@ impl<'a> PeepholeFoldConstants { } fn try_fold_logical_expression( - &self, logical_expr: &mut LogicalExpression<'a>, ctx: &mut TraverseCtx<'a>, ) -> Option> { match logical_expr.operator { - LogicalOperator::And | LogicalOperator::Or => self.try_fold_and_or(logical_expr, ctx), - LogicalOperator::Coalesce => self.try_fold_coalesce(logical_expr, ctx), + LogicalOperator::And | LogicalOperator::Or => Self::try_fold_and_or(logical_expr, ctx), + LogicalOperator::Coalesce => Self::try_fold_coalesce(logical_expr, ctx), } } @@ -287,7 +281,6 @@ impl<'a> PeepholeFoldConstants { /// /// port from [closure-compiler](https://github.com/google/closure-compiler/blob/09094b551915a6487a980a783831cba58b5739d1/src/com/google/javascript/jscomp/PeepholeFoldConstants.java#L587) pub fn try_fold_and_or( - &self, logical_expr: &mut LogicalExpression<'a>, ctx: &mut TraverseCtx<'a>, ) -> Option> { @@ -365,7 +358,6 @@ impl<'a> PeepholeFoldConstants { /// Try to fold a nullish coalesce `foo ?? bar`. pub fn try_fold_coalesce( - &self, logical_expr: &mut LogicalExpression<'a>, ctx: &mut TraverseCtx<'a>, ) -> Option> { @@ -399,22 +391,21 @@ impl<'a> PeepholeFoldConstants { } fn try_fold_binary_expression( - &self, e: &mut BinaryExpression<'a>, ctx: &mut TraverseCtx<'a>, ) -> Option> { // TODO: tryReduceOperandsForOp match e.operator { op if op.is_bitshift() => { - self.try_fold_shift(e.span, e.operator, &e.left, &e.right, ctx) + Self::try_fold_shift(e.span, e.operator, &e.left, &e.right, ctx) } - BinaryOperator::Instanceof => self.try_fold_instanceof(e.span, &e.left, &e.right, ctx), - BinaryOperator::Addition => self.try_fold_addition(e.span, &e.left, &e.right, ctx), + BinaryOperator::Instanceof => Self::try_fold_instanceof(e.span, &e.left, &e.right, ctx), + BinaryOperator::Addition => Self::try_fold_addition(e.span, &e.left, &e.right, ctx), BinaryOperator::Subtraction | BinaryOperator::Division | BinaryOperator::Remainder | BinaryOperator::Multiplication - | BinaryOperator::Exponential => self.try_fold_arithmetic_op(e, ctx), + | BinaryOperator::Exponential => Self::try_fold_arithmetic_op(e, ctx), BinaryOperator::BitwiseAnd | BinaryOperator::BitwiseOR | BinaryOperator::BitwiseXOR => { // TODO: // self.try_fold_arithmetic_op(e.span, &e.left, &e.right, ctx) @@ -425,14 +416,13 @@ impl<'a> PeepholeFoldConstants { None } op if op.is_equality() || op.is_compare() => { - self.try_fold_comparison(e.span, e.operator, &e.left, &e.right, ctx) + Self::try_fold_comparison(e.span, e.operator, &e.left, &e.right, ctx) } _ => None, } } fn try_fold_addition<'b>( - &self, span: Span, left: &'b Expression<'a>, right: &'b Expression<'a>, @@ -476,7 +466,6 @@ impl<'a> PeepholeFoldConstants { } fn try_fold_arithmetic_op( - &self, operation: &mut BinaryExpression<'a>, ctx: &mut TraverseCtx<'a>, ) -> Option> { @@ -504,7 +493,7 @@ impl<'a> PeepholeFoldConstants { let left: f64 = ctx.get_number_value(&operation.left)?.try_into().ok()?; let right: f64 = ctx.get_number_value(&operation.right)?.try_into().ok()?; if !left.is_finite() || !right.is_finite() { - return self.try_fold_infinity_arithmetic(left, operation.operator, right, ctx); + return Self::try_fold_infinity_arithmetic(left, operation.operator, right, ctx); } let result = match operation.operator { BinaryOperator::Addition => left + right, @@ -550,7 +539,6 @@ impl<'a> PeepholeFoldConstants { } fn try_fold_infinity_arithmetic( - &self, left: f64, operator: BinaryOperator, right: f64, @@ -596,7 +584,6 @@ impl<'a> PeepholeFoldConstants { } fn try_fold_instanceof<'b>( - &self, _span: Span, _left: &'b Expression<'a>, _right: &'b Expression<'a>, @@ -606,14 +593,13 @@ impl<'a> PeepholeFoldConstants { } fn try_fold_comparison<'b>( - &self, span: Span, op: BinaryOperator, left: &'b Expression<'a>, right: &'b Expression<'a>, ctx: &mut TraverseCtx<'a>, ) -> Option> { - let value = match self.evaluate_comparison(op, left, right, ctx) { + let value = match Self::evaluate_comparison(op, left, right, ctx) { Tri::True => true, Tri::False => false, Tri::Unknown => return None, @@ -622,7 +608,6 @@ impl<'a> PeepholeFoldConstants { } fn evaluate_comparison<'b>( - &self, op: BinaryOperator, left: &'b Expression<'a>, right: &'b Expression<'a>, @@ -924,7 +909,6 @@ impl<'a> PeepholeFoldConstants { /// ported from [closure-compiler](https://github.com/google/closure-compiler/blob/a4c880032fba961f7a6c06ef99daa3641810bfdd/src/com/google/javascript/jscomp/PeepholeFoldConstants.java#L1114-L1162) #[allow(clippy::cast_possible_truncation)] fn try_fold_shift<'b>( - &self, span: Span, op: BinaryOperator, left: &'b Expression<'a>, diff --git a/crates/oxc_minifier/src/ast_passes/peephole_minimize_conditions.rs b/crates/oxc_minifier/src/ast_passes/peephole_minimize_conditions.rs index c0db66ddf4855..3595ae1a5654d 100644 --- a/crates/oxc_minifier/src/ast_passes/peephole_minimize_conditions.rs +++ b/crates/oxc_minifier/src/ast_passes/peephole_minimize_conditions.rs @@ -28,7 +28,7 @@ impl<'a> CompressorPass<'a> for PeepholeMinimizeConditions { impl<'a> Traverse<'a> for PeepholeMinimizeConditions { fn exit_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) { if let Some(folded_expr) = match expr { - Expression::UnaryExpression(e) if e.operator.is_not() => self.try_minimize_not(e, ctx), + Expression::UnaryExpression(e) if e.operator.is_not() => Self::try_minimize_not(e, ctx), _ => None, } { *expr = folded_expr; @@ -44,7 +44,6 @@ impl<'a> PeepholeMinimizeConditions { /// Try to minimize NOT nodes such as `!(x==y)`. fn try_minimize_not( - &self, expr: &mut UnaryExpression<'a>, ctx: &mut TraverseCtx<'a>, ) -> Option> { diff --git a/crates/oxc_minifier/src/ast_passes/peephole_remove_dead_code.rs b/crates/oxc_minifier/src/ast_passes/peephole_remove_dead_code.rs index 0190bcae65705..05c1a364743f2 100644 --- a/crates/oxc_minifier/src/ast_passes/peephole_remove_dead_code.rs +++ b/crates/oxc_minifier/src/ast_passes/peephole_remove_dead_code.rs @@ -46,7 +46,7 @@ impl<'a> Traverse<'a> for PeepholeRemoveDeadCode { fn enter_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) { if let Some(folded_expr) = match expr { - Expression::ConditionalExpression(e) => self.try_fold_conditional_expression(e, ctx), + Expression::ConditionalExpression(e) => Self::try_fold_conditional_expression(e, ctx), _ => None, } { *expr = folded_expr; @@ -164,7 +164,6 @@ impl<'a> PeepholeRemoveDeadCode { /// Try folding conditional expression (?:) if the condition results of the condition is known. fn try_fold_conditional_expression( - &self, expr: &mut ConditionalExpression<'a>, ctx: &mut TraverseCtx<'a>, ) -> Option> { diff --git a/crates/oxc_minifier/src/ast_passes/peephole_substitute_alternate_syntax.rs b/crates/oxc_minifier/src/ast_passes/peephole_substitute_alternate_syntax.rs index 53e2b157f438b..92596163c17ae 100644 --- a/crates/oxc_minifier/src/ast_passes/peephole_substitute_alternate_syntax.rs +++ b/crates/oxc_minifier/src/ast_passes/peephole_substitute_alternate_syntax.rs @@ -82,12 +82,12 @@ impl<'a> Traverse<'a> for PeepholeSubstituteAlternateSyntax { fn enter_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) { if let Expression::AssignmentExpression(assignment_expr) = expr { - if let Some(new_expr) = self.try_compress_assignment_expression(assignment_expr, ctx) { + if let Some(new_expr) = Self::try_compress_assignment_expression(assignment_expr, ctx) { *expr = new_expr; self.changed = true; } } - if !self.compress_undefined(expr, ctx) { + if !Self::compress_undefined(expr, ctx) { self.compress_boolean(expr, ctx); } } @@ -95,13 +95,13 @@ impl<'a> Traverse<'a> for PeepholeSubstituteAlternateSyntax { fn exit_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) { match expr { Expression::NewExpression(new_expr) => { - if let Some(new_expr) = self.try_fold_new_expression(new_expr, ctx) { + if let Some(new_expr) = Self::try_fold_new_expression(new_expr, ctx) { *expr = new_expr; self.changed = true; } } Expression::CallExpression(call_expr) => { - if let Some(call_expr) = self.try_fold_call_expression(call_expr, ctx) { + if let Some(call_expr) = Self::try_fold_call_expression(call_expr, ctx) { *expr = call_expr; self.changed = true; } @@ -132,7 +132,7 @@ impl<'a> PeepholeSubstituteAlternateSyntax { /* Utilities */ /// Transforms `undefined` => `void 0` - fn compress_undefined(&self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) -> bool { + fn compress_undefined(expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) -> bool { if ctx.is_expression_undefined(expr) { *expr = ctx.ast.void_0(expr.span()); return true; @@ -316,7 +316,6 @@ impl<'a> PeepholeSubstituteAlternateSyntax { } fn try_compress_assignment_expression( - &mut self, expr: &mut AssignmentExpression<'a>, ctx: &mut TraverseCtx<'a>, ) -> Option> { @@ -360,7 +359,6 @@ impl<'a> PeepholeSubstituteAlternateSyntax { } fn try_fold_new_expression( - &mut self, new_expr: &mut NewExpression<'a>, ctx: &mut TraverseCtx<'a>, ) -> Option> { @@ -373,18 +371,19 @@ impl<'a> PeepholeSubstituteAlternateSyntax { } else if new_expr.callee.is_global_reference_name("Array", ctx.symbols()) { // `new Array` -> `[]` if new_expr.arguments.is_empty() { - Some(self.empty_array_literal(ctx)) + Some(Self::empty_array_literal(ctx)) } else if new_expr.arguments.len() == 1 { let arg = new_expr.arguments.get_mut(0).and_then(|arg| arg.as_expression_mut())?; // `new Array(0)` -> `[]` if arg.is_number_0() { - Some(self.empty_array_literal(ctx)) + Some(Self::empty_array_literal(ctx)) } // `new Array(8)` -> `Array(8)` else if arg.is_number_literal() { - Some( - self.array_constructor_call(ctx.ast.move_vec(&mut new_expr.arguments), ctx), - ) + Some(Self::array_constructor_call( + ctx.ast.move_vec(&mut new_expr.arguments), + ctx, + )) } // `new Array(literal)` -> `[literal]` else if arg.is_literal() || matches!(arg, Expression::ArrayExpression(_)) { @@ -392,13 +391,14 @@ impl<'a> PeepholeSubstituteAlternateSyntax { let element = ctx.ast.array_expression_element_expression(ctx.ast.move_expression(arg)); elements.push(element); - Some(self.array_literal(elements, ctx)) + Some(Self::array_literal(elements, ctx)) } // `new Array()` -> `Array()` else { - Some( - self.array_constructor_call(ctx.ast.move_vec(&mut new_expr.arguments), ctx), - ) + Some(Self::array_constructor_call( + ctx.ast.move_vec(&mut new_expr.arguments), + ctx, + )) } } else { // `new Array(1, 2, 3)` -> `[1, 2, 3]` @@ -410,7 +410,7 @@ impl<'a> PeepholeSubstituteAlternateSyntax { }, ), ); - Some(self.array_literal(elements, ctx)) + Some(Self::array_literal(elements, ctx)) } } else { None @@ -418,7 +418,6 @@ impl<'a> PeepholeSubstituteAlternateSyntax { } fn try_fold_call_expression( - &mut self, call_expr: &mut CallExpression<'a>, ctx: &mut TraverseCtx<'a>, ) -> Option> { @@ -431,21 +430,19 @@ impl<'a> PeepholeSubstituteAlternateSyntax { } else if call_expr.callee.is_global_reference_name("Array", ctx.symbols()) { // `Array()` -> `[]` if call_expr.arguments.is_empty() { - Some(self.empty_array_literal(ctx)) + Some(Self::empty_array_literal(ctx)) } else if call_expr.arguments.len() == 1 { let arg = call_expr.arguments.get_mut(0).and_then(|arg| arg.as_expression_mut())?; // `Array(0)` -> `[]` if arg.is_number_0() { - Some(self.empty_array_literal(ctx)) + Some(Self::empty_array_literal(ctx)) } // `Array(8)` -> `Array(8)` else if arg.is_number_literal() { - Some( - self.array_constructor_call( - ctx.ast.move_vec(&mut call_expr.arguments), - ctx, - ), - ) + Some(Self::array_constructor_call( + ctx.ast.move_vec(&mut call_expr.arguments), + ctx, + )) } // `Array(literal)` -> `[literal]` else if arg.is_literal() || matches!(arg, Expression::ArrayExpression(_)) { @@ -453,7 +450,7 @@ impl<'a> PeepholeSubstituteAlternateSyntax { let element = ctx.ast.array_expression_element_expression(ctx.ast.move_expression(arg)); elements.push(element); - Some(self.array_literal(elements, ctx)) + Some(Self::array_literal(elements, ctx)) } else { None } @@ -467,7 +464,7 @@ impl<'a> PeepholeSubstituteAlternateSyntax { }, ), ); - Some(self.array_literal(elements, ctx)) + Some(Self::array_literal(elements, ctx)) } } else { None @@ -489,7 +486,6 @@ impl<'a> PeepholeSubstituteAlternateSyntax { /// returns an `Array()` constructor call with zero, one, or more arguments, copying from the input fn array_constructor_call( - &self, arguments: Vec<'a, Argument<'a>>, ctx: &mut TraverseCtx<'a>, ) -> Expression<'a> { @@ -499,7 +495,6 @@ impl<'a> PeepholeSubstituteAlternateSyntax { /// returns an array literal `[]` of zero, one, or more elements, copying from the input fn array_literal( - &self, elements: Vec<'a, ArrayExpressionElement<'a>>, ctx: &mut TraverseCtx<'a>, ) -> Expression<'a> { @@ -507,8 +502,8 @@ impl<'a> PeepholeSubstituteAlternateSyntax { } /// returns a new empty array literal expression: `[]` - fn empty_array_literal(&self, ctx: &mut TraverseCtx<'a>) -> Expression<'a> { - self.array_literal(ctx.ast.vec(), ctx) + fn empty_array_literal(ctx: &mut TraverseCtx<'a>) -> Expression<'a> { + Self::array_literal(ctx.ast.vec(), ctx) } } diff --git a/crates/oxc_minifier/src/compressor.rs b/crates/oxc_minifier/src/compressor.rs index cb1f7076020e9..6f7188a55b634 100644 --- a/crates/oxc_minifier/src/compressor.rs +++ b/crates/oxc_minifier/src/compressor.rs @@ -38,7 +38,7 @@ impl<'a> Compressor<'a> { RemoveSyntax::new(self.options).build(program, &mut ctx); if self.options.dead_code_elimination { - self.dead_code_elimination(program, &mut ctx); + Self::dead_code_elimination(program, &mut ctx); return; } @@ -76,7 +76,7 @@ impl<'a> Compressor<'a> { } } - fn dead_code_elimination(self, program: &mut Program<'a>, ctx: &mut TraverseCtx<'a>) { + fn dead_code_elimination(program: &mut Program<'a>, ctx: &mut TraverseCtx<'a>) { PeepholeFoldConstants::new().build(program, ctx); PeepholeMinimizeConditions::new().build(program, ctx); PeepholeRemoveDeadCode::new().build(program, ctx); diff --git a/crates/oxc_minifier/src/lib.rs b/crates/oxc_minifier/src/lib.rs index 9fad23f4f3b00..d2041e3cbf89c 100644 --- a/crates/oxc_minifier/src/lib.rs +++ b/crates/oxc_minifier/src/lib.rs @@ -1,4 +1,4 @@ -#![allow(clippy::wildcard_imports, clippy::new_without_default, clippy::unused_self)] +#![allow(clippy::wildcard_imports)] //! ECMAScript Minifier