Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(minifier): remove allow clippy::unused_self #6441

Merged
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
48 changes: 16 additions & 32 deletions crates/oxc_minifier/src/ast_passes/peephole_fold_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -72,15 +72,13 @@ impl<'a> PeepholeFoldConstants {
}

fn try_fold_useless_object_dot_define_properties_call(
&mut self,
_call_expr: &mut CallExpression<'a>,
_ctx: &mut TraverseCtx<'a>,
) -> Option<Expression<'a>> {
None
}

fn try_fold_ctor_cal(
&mut self,
_new_expr: &mut NewExpression<'a>,
_ctx: &mut TraverseCtx<'a>,
) -> Option<Expression<'a>> {
Expand All @@ -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<Expression<'a>> {
Expand Down Expand Up @@ -124,15 +121,13 @@ impl<'a> PeepholeFoldConstants {
// }

fn try_flatten_array_expression(
&mut self,
_new_expr: &mut ArrayExpression<'a>,
_ctx: &mut TraverseCtx<'a>,
) -> Option<Expression<'a>> {
None
}

fn try_flatten_object_expression(
&mut self,
_new_expr: &mut ObjectExpression<'a>,
_ctx: &mut TraverseCtx<'a>,
) -> Option<Expression<'a>> {
Expand All @@ -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 => {
Expand Down Expand Up @@ -273,21 +268,19 @@ impl<'a> PeepholeFoldConstants {
}

fn try_fold_logical_expression(
&self,
logical_expr: &mut LogicalExpression<'a>,
ctx: &mut TraverseCtx<'a>,
) -> Option<Expression<'a>> {
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),
}
}

/// Try to fold a AND / OR node.
///
/// 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<Expression<'a>> {
Expand Down Expand Up @@ -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<Expression<'a>> {
Expand Down Expand Up @@ -399,22 +391,21 @@ impl<'a> PeepholeFoldConstants {
}

fn try_fold_binary_expression(
&self,
e: &mut BinaryExpression<'a>,
ctx: &mut TraverseCtx<'a>,
) -> Option<Expression<'a>> {
// 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)
Expand All @@ -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>,
Expand Down Expand Up @@ -476,7 +466,6 @@ impl<'a> PeepholeFoldConstants {
}

fn try_fold_arithmetic_op(
&self,
operation: &mut BinaryExpression<'a>,
ctx: &mut TraverseCtx<'a>,
) -> Option<Expression<'a>> {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -550,7 +539,6 @@ impl<'a> PeepholeFoldConstants {
}

fn try_fold_infinity_arithmetic(
&self,
left: f64,
operator: BinaryOperator,
right: f64,
Expand Down Expand Up @@ -596,7 +584,6 @@ impl<'a> PeepholeFoldConstants {
}

fn try_fold_instanceof<'b>(
&self,
_span: Span,
_left: &'b Expression<'a>,
_right: &'b Expression<'a>,
Expand All @@ -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<Expression<'a>> {
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,
Expand All @@ -622,7 +608,6 @@ impl<'a> PeepholeFoldConstants {
}

fn evaluate_comparison<'b>(
&self,
op: BinaryOperator,
left: &'b Expression<'a>,
right: &'b Expression<'a>,
Expand Down Expand Up @@ -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>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Expression<'a>> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Expression<'a>> {
Expand Down
Loading