Skip to content

Commit

Permalink
feat(minifier): fold void 1 -> void 0 (#5670)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Sep 10, 2024
1 parent 2da42ef commit 68c3cf5
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
8 changes: 7 additions & 1 deletion crates/oxc_ast/src/ast_builder_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,16 @@ impl<'a> AstBuilder<'a> {

/* ---------- Constructors ---------- */

/// `0`
#[inline]
pub fn number_0(self) -> Expression<'a> {
self.expression_numeric_literal(Span::default(), 0.0, "0", NumberBase::Decimal)
}

/// `void 0`
#[inline]
pub fn void_0(self) -> Expression<'a> {
let num = self.expression_numeric_literal(Span::default(), 0.0, "0", NumberBase::Decimal);
let num = self.number_0();
Expression::UnaryExpression(self.alloc(self.unary_expression(
Span::default(),
UnaryOperator::Void,
Expand Down
16 changes: 15 additions & 1 deletion crates/oxc_minifier/src/ast_passes/fold_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//!
//! <https://github.com/google/closure-compiler/blob/master/src/com/google/javascript/jscomp/PeepholeFoldConstants.java>
use std::cmp::Ordering;
use std::{cmp::Ordering, mem};

use num_bigint::BigInt;
use oxc_ast::{ast::*, AstBuilder, Visit};
Expand Down Expand Up @@ -166,6 +166,7 @@ impl<'a> FoldConstants<'a> {
ctx: &mut TraverseCtx<'a>,
) -> Option<Expression<'a>> {
match expr.operator {
UnaryOperator::Void => Self::try_reduce_void(expr, ctx),
UnaryOperator::Typeof => self.try_fold_type_of(expr, ctx),
UnaryOperator::LogicalNot => {
expr.argument.to_boolean().map(|b| self.ast.expression_boolean_literal(SPAN, !b))
Expand All @@ -182,6 +183,19 @@ impl<'a> FoldConstants<'a> {
}
}

/// `void 1` -> `void 0`
fn try_reduce_void(
expr: &mut UnaryExpression<'a>,
ctx: &mut TraverseCtx<'a>,
) -> Option<Expression<'a>> {
if (!expr.argument.is_number() || !expr.argument.is_number_0())
&& !expr.may_have_side_effects()
{
let _ = mem::replace(&mut expr.argument, ctx.ast.number_0());
}
None
}

/// Folds 'typeof(foo)' if foo is a literal, e.g.
/// `typeof("bar") --> "string"`
/// `typeof(6) --> "number"`
Expand Down
1 change: 0 additions & 1 deletion crates/oxc_minifier/tests/ast_passes/fold_constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,6 @@ fn test_fold_logical_op2() {
}

#[test]
#[ignore]
fn test_fold_void() {
test_same("void 0");
test("void 1", "void 0");
Expand Down

0 comments on commit 68c3cf5

Please sign in to comment.