mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
refactor(minifier): remove allow clippy::unused_self (#6441)
This commit is contained in:
parent
2566ce787d
commit
46a38c63ff
6 changed files with 48 additions and 71 deletions
|
|
@ -45,17 +45,17 @@ impl<'a> Traverse<'a> for PeepholeFoldConstants {
|
||||||
fn exit_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
|
fn exit_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
|
||||||
if let Some(folded_expr) = match expr {
|
if let Some(folded_expr) = match expr {
|
||||||
Expression::CallExpression(e) => {
|
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
|
// TODO
|
||||||
// return tryFoldSpread(subtree);
|
// return tryFoldSpread(subtree);
|
||||||
Expression::ArrayExpression(e) => self.try_flatten_array_expression(e, ctx),
|
Expression::ArrayExpression(e) => Self::try_flatten_array_expression(e, ctx),
|
||||||
Expression::ObjectExpression(e) => self.try_flatten_object_expression(e, ctx),
|
Expression::ObjectExpression(e) => Self::try_flatten_object_expression(e, ctx),
|
||||||
Expression::BinaryExpression(e) => self.try_fold_binary_expression(e, ctx),
|
Expression::BinaryExpression(e) => Self::try_fold_binary_expression(e, ctx),
|
||||||
Expression::UnaryExpression(e) => self.try_fold_unary_expression(e, ctx),
|
Expression::UnaryExpression(e) => self.try_fold_unary_expression(e, ctx),
|
||||||
// TODO: return tryFoldGetProp(subtree);
|
// 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: tryFoldGetElem
|
||||||
// TODO: tryFoldAssign
|
// TODO: tryFoldAssign
|
||||||
_ => None,
|
_ => None,
|
||||||
|
|
@ -72,7 +72,6 @@ impl<'a> PeepholeFoldConstants {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn try_fold_useless_object_dot_define_properties_call(
|
fn try_fold_useless_object_dot_define_properties_call(
|
||||||
&mut self,
|
|
||||||
_call_expr: &mut CallExpression<'a>,
|
_call_expr: &mut CallExpression<'a>,
|
||||||
_ctx: &mut TraverseCtx<'a>,
|
_ctx: &mut TraverseCtx<'a>,
|
||||||
) -> Option<Expression<'a>> {
|
) -> Option<Expression<'a>> {
|
||||||
|
|
@ -80,7 +79,6 @@ impl<'a> PeepholeFoldConstants {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn try_fold_ctor_cal(
|
fn try_fold_ctor_cal(
|
||||||
&mut self,
|
|
||||||
_new_expr: &mut NewExpression<'a>,
|
_new_expr: &mut NewExpression<'a>,
|
||||||
_ctx: &mut TraverseCtx<'a>,
|
_ctx: &mut TraverseCtx<'a>,
|
||||||
) -> Option<Expression<'a>> {
|
) -> Option<Expression<'a>> {
|
||||||
|
|
@ -91,7 +89,6 @@ impl<'a> PeepholeFoldConstants {
|
||||||
/// `typeof("bar") --> "string"`
|
/// `typeof("bar") --> "string"`
|
||||||
/// `typeof(6) --> "number"`
|
/// `typeof(6) --> "number"`
|
||||||
fn try_fold_type_of(
|
fn try_fold_type_of(
|
||||||
&self,
|
|
||||||
expr: &mut UnaryExpression<'a>,
|
expr: &mut UnaryExpression<'a>,
|
||||||
ctx: &mut TraverseCtx<'a>,
|
ctx: &mut TraverseCtx<'a>,
|
||||||
) -> Option<Expression<'a>> {
|
) -> Option<Expression<'a>> {
|
||||||
|
|
@ -124,7 +121,6 @@ impl<'a> PeepholeFoldConstants {
|
||||||
// }
|
// }
|
||||||
|
|
||||||
fn try_flatten_array_expression(
|
fn try_flatten_array_expression(
|
||||||
&mut self,
|
|
||||||
_new_expr: &mut ArrayExpression<'a>,
|
_new_expr: &mut ArrayExpression<'a>,
|
||||||
_ctx: &mut TraverseCtx<'a>,
|
_ctx: &mut TraverseCtx<'a>,
|
||||||
) -> Option<Expression<'a>> {
|
) -> Option<Expression<'a>> {
|
||||||
|
|
@ -132,7 +128,6 @@ impl<'a> PeepholeFoldConstants {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn try_flatten_object_expression(
|
fn try_flatten_object_expression(
|
||||||
&mut self,
|
|
||||||
_new_expr: &mut ObjectExpression<'a>,
|
_new_expr: &mut ObjectExpression<'a>,
|
||||||
_ctx: &mut TraverseCtx<'a>,
|
_ctx: &mut TraverseCtx<'a>,
|
||||||
) -> Option<Expression<'a>> {
|
) -> Option<Expression<'a>> {
|
||||||
|
|
@ -149,7 +144,7 @@ impl<'a> PeepholeFoldConstants {
|
||||||
}
|
}
|
||||||
match expr.operator {
|
match expr.operator {
|
||||||
UnaryOperator::Void => self.try_reduce_void(expr, ctx),
|
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
|
// TODO: tryReduceOperandsForOp
|
||||||
#[allow(clippy::float_cmp)]
|
#[allow(clippy::float_cmp)]
|
||||||
UnaryOperator::LogicalNot => {
|
UnaryOperator::LogicalNot => {
|
||||||
|
|
@ -273,13 +268,12 @@ impl<'a> PeepholeFoldConstants {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn try_fold_logical_expression(
|
fn try_fold_logical_expression(
|
||||||
&self,
|
|
||||||
logical_expr: &mut LogicalExpression<'a>,
|
logical_expr: &mut LogicalExpression<'a>,
|
||||||
ctx: &mut TraverseCtx<'a>,
|
ctx: &mut TraverseCtx<'a>,
|
||||||
) -> Option<Expression<'a>> {
|
) -> Option<Expression<'a>> {
|
||||||
match logical_expr.operator {
|
match logical_expr.operator {
|
||||||
LogicalOperator::And | LogicalOperator::Or => self.try_fold_and_or(logical_expr, ctx),
|
LogicalOperator::And | LogicalOperator::Or => Self::try_fold_and_or(logical_expr, ctx),
|
||||||
LogicalOperator::Coalesce => self.try_fold_coalesce(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)
|
/// 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(
|
pub fn try_fold_and_or(
|
||||||
&self,
|
|
||||||
logical_expr: &mut LogicalExpression<'a>,
|
logical_expr: &mut LogicalExpression<'a>,
|
||||||
ctx: &mut TraverseCtx<'a>,
|
ctx: &mut TraverseCtx<'a>,
|
||||||
) -> Option<Expression<'a>> {
|
) -> Option<Expression<'a>> {
|
||||||
|
|
@ -365,7 +358,6 @@ impl<'a> PeepholeFoldConstants {
|
||||||
|
|
||||||
/// Try to fold a nullish coalesce `foo ?? bar`.
|
/// Try to fold a nullish coalesce `foo ?? bar`.
|
||||||
pub fn try_fold_coalesce(
|
pub fn try_fold_coalesce(
|
||||||
&self,
|
|
||||||
logical_expr: &mut LogicalExpression<'a>,
|
logical_expr: &mut LogicalExpression<'a>,
|
||||||
ctx: &mut TraverseCtx<'a>,
|
ctx: &mut TraverseCtx<'a>,
|
||||||
) -> Option<Expression<'a>> {
|
) -> Option<Expression<'a>> {
|
||||||
|
|
@ -399,22 +391,21 @@ impl<'a> PeepholeFoldConstants {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn try_fold_binary_expression(
|
fn try_fold_binary_expression(
|
||||||
&self,
|
|
||||||
e: &mut BinaryExpression<'a>,
|
e: &mut BinaryExpression<'a>,
|
||||||
ctx: &mut TraverseCtx<'a>,
|
ctx: &mut TraverseCtx<'a>,
|
||||||
) -> Option<Expression<'a>> {
|
) -> Option<Expression<'a>> {
|
||||||
// TODO: tryReduceOperandsForOp
|
// TODO: tryReduceOperandsForOp
|
||||||
match e.operator {
|
match e.operator {
|
||||||
op if op.is_bitshift() => {
|
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::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::Addition => Self::try_fold_addition(e.span, &e.left, &e.right, ctx),
|
||||||
BinaryOperator::Subtraction
|
BinaryOperator::Subtraction
|
||||||
| BinaryOperator::Division
|
| BinaryOperator::Division
|
||||||
| BinaryOperator::Remainder
|
| BinaryOperator::Remainder
|
||||||
| BinaryOperator::Multiplication
|
| 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 => {
|
BinaryOperator::BitwiseAnd | BinaryOperator::BitwiseOR | BinaryOperator::BitwiseXOR => {
|
||||||
// TODO:
|
// TODO:
|
||||||
// self.try_fold_arithmetic_op(e.span, &e.left, &e.right, ctx)
|
// self.try_fold_arithmetic_op(e.span, &e.left, &e.right, ctx)
|
||||||
|
|
@ -425,14 +416,13 @@ impl<'a> PeepholeFoldConstants {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
op if op.is_equality() || op.is_compare() => {
|
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,
|
_ => None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn try_fold_addition<'b>(
|
fn try_fold_addition<'b>(
|
||||||
&self,
|
|
||||||
span: Span,
|
span: Span,
|
||||||
left: &'b Expression<'a>,
|
left: &'b Expression<'a>,
|
||||||
right: &'b Expression<'a>,
|
right: &'b Expression<'a>,
|
||||||
|
|
@ -476,7 +466,6 @@ impl<'a> PeepholeFoldConstants {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn try_fold_arithmetic_op(
|
fn try_fold_arithmetic_op(
|
||||||
&self,
|
|
||||||
operation: &mut BinaryExpression<'a>,
|
operation: &mut BinaryExpression<'a>,
|
||||||
ctx: &mut TraverseCtx<'a>,
|
ctx: &mut TraverseCtx<'a>,
|
||||||
) -> Option<Expression<'a>> {
|
) -> Option<Expression<'a>> {
|
||||||
|
|
@ -504,7 +493,7 @@ impl<'a> PeepholeFoldConstants {
|
||||||
let left: f64 = ctx.get_number_value(&operation.left)?.try_into().ok()?;
|
let left: f64 = ctx.get_number_value(&operation.left)?.try_into().ok()?;
|
||||||
let right: f64 = ctx.get_number_value(&operation.right)?.try_into().ok()?;
|
let right: f64 = ctx.get_number_value(&operation.right)?.try_into().ok()?;
|
||||||
if !left.is_finite() || !right.is_finite() {
|
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 {
|
let result = match operation.operator {
|
||||||
BinaryOperator::Addition => left + right,
|
BinaryOperator::Addition => left + right,
|
||||||
|
|
@ -550,7 +539,6 @@ impl<'a> PeepholeFoldConstants {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn try_fold_infinity_arithmetic(
|
fn try_fold_infinity_arithmetic(
|
||||||
&self,
|
|
||||||
left: f64,
|
left: f64,
|
||||||
operator: BinaryOperator,
|
operator: BinaryOperator,
|
||||||
right: f64,
|
right: f64,
|
||||||
|
|
@ -596,7 +584,6 @@ impl<'a> PeepholeFoldConstants {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn try_fold_instanceof<'b>(
|
fn try_fold_instanceof<'b>(
|
||||||
&self,
|
|
||||||
_span: Span,
|
_span: Span,
|
||||||
_left: &'b Expression<'a>,
|
_left: &'b Expression<'a>,
|
||||||
_right: &'b Expression<'a>,
|
_right: &'b Expression<'a>,
|
||||||
|
|
@ -606,14 +593,13 @@ impl<'a> PeepholeFoldConstants {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn try_fold_comparison<'b>(
|
fn try_fold_comparison<'b>(
|
||||||
&self,
|
|
||||||
span: Span,
|
span: Span,
|
||||||
op: BinaryOperator,
|
op: BinaryOperator,
|
||||||
left: &'b Expression<'a>,
|
left: &'b Expression<'a>,
|
||||||
right: &'b Expression<'a>,
|
right: &'b Expression<'a>,
|
||||||
ctx: &mut TraverseCtx<'a>,
|
ctx: &mut TraverseCtx<'a>,
|
||||||
) -> Option<Expression<'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::True => true,
|
||||||
Tri::False => false,
|
Tri::False => false,
|
||||||
Tri::Unknown => return None,
|
Tri::Unknown => return None,
|
||||||
|
|
@ -622,7 +608,6 @@ impl<'a> PeepholeFoldConstants {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn evaluate_comparison<'b>(
|
fn evaluate_comparison<'b>(
|
||||||
&self,
|
|
||||||
op: BinaryOperator,
|
op: BinaryOperator,
|
||||||
left: &'b Expression<'a>,
|
left: &'b Expression<'a>,
|
||||||
right: &'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)
|
/// 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)]
|
#[allow(clippy::cast_possible_truncation)]
|
||||||
fn try_fold_shift<'b>(
|
fn try_fold_shift<'b>(
|
||||||
&self,
|
|
||||||
span: Span,
|
span: Span,
|
||||||
op: BinaryOperator,
|
op: BinaryOperator,
|
||||||
left: &'b Expression<'a>,
|
left: &'b Expression<'a>,
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,7 @@ impl<'a> CompressorPass<'a> for PeepholeMinimizeConditions {
|
||||||
impl<'a> Traverse<'a> for PeepholeMinimizeConditions {
|
impl<'a> Traverse<'a> for PeepholeMinimizeConditions {
|
||||||
fn exit_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
|
fn exit_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
|
||||||
if let Some(folded_expr) = match expr {
|
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,
|
_ => None,
|
||||||
} {
|
} {
|
||||||
*expr = folded_expr;
|
*expr = folded_expr;
|
||||||
|
|
@ -44,7 +44,6 @@ impl<'a> PeepholeMinimizeConditions {
|
||||||
|
|
||||||
/// Try to minimize NOT nodes such as `!(x==y)`.
|
/// Try to minimize NOT nodes such as `!(x==y)`.
|
||||||
fn try_minimize_not(
|
fn try_minimize_not(
|
||||||
&self,
|
|
||||||
expr: &mut UnaryExpression<'a>,
|
expr: &mut UnaryExpression<'a>,
|
||||||
ctx: &mut TraverseCtx<'a>,
|
ctx: &mut TraverseCtx<'a>,
|
||||||
) -> Option<Expression<'a>> {
|
) -> Option<Expression<'a>> {
|
||||||
|
|
|
||||||
|
|
@ -46,7 +46,7 @@ impl<'a> Traverse<'a> for PeepholeRemoveDeadCode {
|
||||||
|
|
||||||
fn enter_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
|
fn enter_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
|
||||||
if let Some(folded_expr) = match expr {
|
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,
|
_ => None,
|
||||||
} {
|
} {
|
||||||
*expr = folded_expr;
|
*expr = folded_expr;
|
||||||
|
|
@ -164,7 +164,6 @@ impl<'a> PeepholeRemoveDeadCode {
|
||||||
|
|
||||||
/// Try folding conditional expression (?:) if the condition results of the condition is known.
|
/// Try folding conditional expression (?:) if the condition results of the condition is known.
|
||||||
fn try_fold_conditional_expression(
|
fn try_fold_conditional_expression(
|
||||||
&self,
|
|
||||||
expr: &mut ConditionalExpression<'a>,
|
expr: &mut ConditionalExpression<'a>,
|
||||||
ctx: &mut TraverseCtx<'a>,
|
ctx: &mut TraverseCtx<'a>,
|
||||||
) -> Option<Expression<'a>> {
|
) -> Option<Expression<'a>> {
|
||||||
|
|
|
||||||
|
|
@ -82,12 +82,12 @@ impl<'a> Traverse<'a> for PeepholeSubstituteAlternateSyntax {
|
||||||
|
|
||||||
fn enter_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
|
fn enter_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
|
||||||
if let Expression::AssignmentExpression(assignment_expr) = expr {
|
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;
|
*expr = new_expr;
|
||||||
self.changed = true;
|
self.changed = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !self.compress_undefined(expr, ctx) {
|
if !Self::compress_undefined(expr, ctx) {
|
||||||
self.compress_boolean(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>) {
|
fn exit_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
|
||||||
match expr {
|
match expr {
|
||||||
Expression::NewExpression(new_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;
|
*expr = new_expr;
|
||||||
self.changed = true;
|
self.changed = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Expression::CallExpression(call_expr) => {
|
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;
|
*expr = call_expr;
|
||||||
self.changed = true;
|
self.changed = true;
|
||||||
}
|
}
|
||||||
|
|
@ -132,7 +132,7 @@ impl<'a> PeepholeSubstituteAlternateSyntax {
|
||||||
/* Utilities */
|
/* Utilities */
|
||||||
|
|
||||||
/// Transforms `undefined` => `void 0`
|
/// 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) {
|
if ctx.is_expression_undefined(expr) {
|
||||||
*expr = ctx.ast.void_0(expr.span());
|
*expr = ctx.ast.void_0(expr.span());
|
||||||
return true;
|
return true;
|
||||||
|
|
@ -316,7 +316,6 @@ impl<'a> PeepholeSubstituteAlternateSyntax {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn try_compress_assignment_expression(
|
fn try_compress_assignment_expression(
|
||||||
&mut self,
|
|
||||||
expr: &mut AssignmentExpression<'a>,
|
expr: &mut AssignmentExpression<'a>,
|
||||||
ctx: &mut TraverseCtx<'a>,
|
ctx: &mut TraverseCtx<'a>,
|
||||||
) -> Option<Expression<'a>> {
|
) -> Option<Expression<'a>> {
|
||||||
|
|
@ -360,7 +359,6 @@ impl<'a> PeepholeSubstituteAlternateSyntax {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn try_fold_new_expression(
|
fn try_fold_new_expression(
|
||||||
&mut self,
|
|
||||||
new_expr: &mut NewExpression<'a>,
|
new_expr: &mut NewExpression<'a>,
|
||||||
ctx: &mut TraverseCtx<'a>,
|
ctx: &mut TraverseCtx<'a>,
|
||||||
) -> Option<Expression<'a>> {
|
) -> Option<Expression<'a>> {
|
||||||
|
|
@ -373,18 +371,19 @@ impl<'a> PeepholeSubstituteAlternateSyntax {
|
||||||
} else if new_expr.callee.is_global_reference_name("Array", ctx.symbols()) {
|
} else if new_expr.callee.is_global_reference_name("Array", ctx.symbols()) {
|
||||||
// `new Array` -> `[]`
|
// `new Array` -> `[]`
|
||||||
if new_expr.arguments.is_empty() {
|
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 {
|
} else if new_expr.arguments.len() == 1 {
|
||||||
let arg = new_expr.arguments.get_mut(0).and_then(|arg| arg.as_expression_mut())?;
|
let arg = new_expr.arguments.get_mut(0).and_then(|arg| arg.as_expression_mut())?;
|
||||||
// `new Array(0)` -> `[]`
|
// `new Array(0)` -> `[]`
|
||||||
if arg.is_number_0() {
|
if arg.is_number_0() {
|
||||||
Some(self.empty_array_literal(ctx))
|
Some(Self::empty_array_literal(ctx))
|
||||||
}
|
}
|
||||||
// `new Array(8)` -> `Array(8)`
|
// `new Array(8)` -> `Array(8)`
|
||||||
else if arg.is_number_literal() {
|
else if arg.is_number_literal() {
|
||||||
Some(
|
Some(Self::array_constructor_call(
|
||||||
self.array_constructor_call(ctx.ast.move_vec(&mut new_expr.arguments), ctx),
|
ctx.ast.move_vec(&mut new_expr.arguments),
|
||||||
)
|
ctx,
|
||||||
|
))
|
||||||
}
|
}
|
||||||
// `new Array(literal)` -> `[literal]`
|
// `new Array(literal)` -> `[literal]`
|
||||||
else if arg.is_literal() || matches!(arg, Expression::ArrayExpression(_)) {
|
else if arg.is_literal() || matches!(arg, Expression::ArrayExpression(_)) {
|
||||||
|
|
@ -392,13 +391,14 @@ impl<'a> PeepholeSubstituteAlternateSyntax {
|
||||||
let element =
|
let element =
|
||||||
ctx.ast.array_expression_element_expression(ctx.ast.move_expression(arg));
|
ctx.ast.array_expression_element_expression(ctx.ast.move_expression(arg));
|
||||||
elements.push(element);
|
elements.push(element);
|
||||||
Some(self.array_literal(elements, ctx))
|
Some(Self::array_literal(elements, ctx))
|
||||||
}
|
}
|
||||||
// `new Array()` -> `Array()`
|
// `new Array()` -> `Array()`
|
||||||
else {
|
else {
|
||||||
Some(
|
Some(Self::array_constructor_call(
|
||||||
self.array_constructor_call(ctx.ast.move_vec(&mut new_expr.arguments), ctx),
|
ctx.ast.move_vec(&mut new_expr.arguments),
|
||||||
)
|
ctx,
|
||||||
|
))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// `new Array(1, 2, 3)` -> `[1, 2, 3]`
|
// `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 {
|
} else {
|
||||||
None
|
None
|
||||||
|
|
@ -418,7 +418,6 @@ impl<'a> PeepholeSubstituteAlternateSyntax {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn try_fold_call_expression(
|
fn try_fold_call_expression(
|
||||||
&mut self,
|
|
||||||
call_expr: &mut CallExpression<'a>,
|
call_expr: &mut CallExpression<'a>,
|
||||||
ctx: &mut TraverseCtx<'a>,
|
ctx: &mut TraverseCtx<'a>,
|
||||||
) -> Option<Expression<'a>> {
|
) -> Option<Expression<'a>> {
|
||||||
|
|
@ -431,21 +430,19 @@ impl<'a> PeepholeSubstituteAlternateSyntax {
|
||||||
} else if call_expr.callee.is_global_reference_name("Array", ctx.symbols()) {
|
} else if call_expr.callee.is_global_reference_name("Array", ctx.symbols()) {
|
||||||
// `Array()` -> `[]`
|
// `Array()` -> `[]`
|
||||||
if call_expr.arguments.is_empty() {
|
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 {
|
} else if call_expr.arguments.len() == 1 {
|
||||||
let arg = call_expr.arguments.get_mut(0).and_then(|arg| arg.as_expression_mut())?;
|
let arg = call_expr.arguments.get_mut(0).and_then(|arg| arg.as_expression_mut())?;
|
||||||
// `Array(0)` -> `[]`
|
// `Array(0)` -> `[]`
|
||||||
if arg.is_number_0() {
|
if arg.is_number_0() {
|
||||||
Some(self.empty_array_literal(ctx))
|
Some(Self::empty_array_literal(ctx))
|
||||||
}
|
}
|
||||||
// `Array(8)` -> `Array(8)`
|
// `Array(8)` -> `Array(8)`
|
||||||
else if arg.is_number_literal() {
|
else if arg.is_number_literal() {
|
||||||
Some(
|
Some(Self::array_constructor_call(
|
||||||
self.array_constructor_call(
|
ctx.ast.move_vec(&mut call_expr.arguments),
|
||||||
ctx.ast.move_vec(&mut call_expr.arguments),
|
ctx,
|
||||||
ctx,
|
))
|
||||||
),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
// `Array(literal)` -> `[literal]`
|
// `Array(literal)` -> `[literal]`
|
||||||
else if arg.is_literal() || matches!(arg, Expression::ArrayExpression(_)) {
|
else if arg.is_literal() || matches!(arg, Expression::ArrayExpression(_)) {
|
||||||
|
|
@ -453,7 +450,7 @@ impl<'a> PeepholeSubstituteAlternateSyntax {
|
||||||
let element =
|
let element =
|
||||||
ctx.ast.array_expression_element_expression(ctx.ast.move_expression(arg));
|
ctx.ast.array_expression_element_expression(ctx.ast.move_expression(arg));
|
||||||
elements.push(element);
|
elements.push(element);
|
||||||
Some(self.array_literal(elements, ctx))
|
Some(Self::array_literal(elements, ctx))
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
@ -467,7 +464,7 @@ impl<'a> PeepholeSubstituteAlternateSyntax {
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
Some(self.array_literal(elements, ctx))
|
Some(Self::array_literal(elements, ctx))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
|
|
@ -489,7 +486,6 @@ impl<'a> PeepholeSubstituteAlternateSyntax {
|
||||||
|
|
||||||
/// returns an `Array()` constructor call with zero, one, or more arguments, copying from the input
|
/// returns an `Array()` constructor call with zero, one, or more arguments, copying from the input
|
||||||
fn array_constructor_call(
|
fn array_constructor_call(
|
||||||
&self,
|
|
||||||
arguments: Vec<'a, Argument<'a>>,
|
arguments: Vec<'a, Argument<'a>>,
|
||||||
ctx: &mut TraverseCtx<'a>,
|
ctx: &mut TraverseCtx<'a>,
|
||||||
) -> Expression<'a> {
|
) -> Expression<'a> {
|
||||||
|
|
@ -499,7 +495,6 @@ impl<'a> PeepholeSubstituteAlternateSyntax {
|
||||||
|
|
||||||
/// returns an array literal `[]` of zero, one, or more elements, copying from the input
|
/// returns an array literal `[]` of zero, one, or more elements, copying from the input
|
||||||
fn array_literal(
|
fn array_literal(
|
||||||
&self,
|
|
||||||
elements: Vec<'a, ArrayExpressionElement<'a>>,
|
elements: Vec<'a, ArrayExpressionElement<'a>>,
|
||||||
ctx: &mut TraverseCtx<'a>,
|
ctx: &mut TraverseCtx<'a>,
|
||||||
) -> Expression<'a> {
|
) -> Expression<'a> {
|
||||||
|
|
@ -507,8 +502,8 @@ impl<'a> PeepholeSubstituteAlternateSyntax {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// returns a new empty array literal expression: `[]`
|
/// returns a new empty array literal expression: `[]`
|
||||||
fn empty_array_literal(&self, ctx: &mut TraverseCtx<'a>) -> Expression<'a> {
|
fn empty_array_literal(ctx: &mut TraverseCtx<'a>) -> Expression<'a> {
|
||||||
self.array_literal(ctx.ast.vec(), ctx)
|
Self::array_literal(ctx.ast.vec(), ctx)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -38,7 +38,7 @@ impl<'a> Compressor<'a> {
|
||||||
RemoveSyntax::new(self.options).build(program, &mut ctx);
|
RemoveSyntax::new(self.options).build(program, &mut ctx);
|
||||||
|
|
||||||
if self.options.dead_code_elimination {
|
if self.options.dead_code_elimination {
|
||||||
self.dead_code_elimination(program, &mut ctx);
|
Self::dead_code_elimination(program, &mut ctx);
|
||||||
return;
|
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);
|
PeepholeFoldConstants::new().build(program, ctx);
|
||||||
PeepholeMinimizeConditions::new().build(program, ctx);
|
PeepholeMinimizeConditions::new().build(program, ctx);
|
||||||
PeepholeRemoveDeadCode::new().build(program, ctx);
|
PeepholeRemoveDeadCode::new().build(program, ctx);
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
#![allow(clippy::wildcard_imports, clippy::new_without_default, clippy::unused_self)]
|
#![allow(clippy::wildcard_imports)]
|
||||||
|
|
||||||
//! ECMAScript Minifier
|
//! ECMAScript Minifier
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue