Skip to content

Commit

Permalink
feat(minifier): minimize not !(x === undefined) -> x !== undefined (
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Jan 11, 2025
1 parent 7f69561 commit dab7a51
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
22 changes: 16 additions & 6 deletions crates/oxc_minifier/src/ast_passes/peephole_minimize_conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,21 @@ impl<'a> PeepholeMinimizeConditions {
expr: &mut UnaryExpression<'a>,
ctx: &mut TraverseCtx<'a>,
) -> Option<Expression<'a>> {
if expr.operator.is_not() {
if let Expression::UnaryExpression(e1) = &mut expr.argument {
if e1.operator.is_not() && ValueType::from(&e1.argument).is_boolean() {
return Some(ctx.ast.move_expression(&mut e1.argument));
}
if !expr.operator.is_not() {
return None;
}
match &mut expr.argument {
Expression::UnaryExpression(e)
if e.operator.is_not() && ValueType::from(&e.argument).is_boolean() =>
{
Some(ctx.ast.move_expression(&mut e.argument))
}
Expression::BinaryExpression(e) if e.operator.is_equality() => {
e.operator = e.operator.equality_inverse_operator().unwrap();
Some(ctx.ast.move_expression(&mut expr.argument))
}
_ => None,
}
None
}

fn try_minimize_if(
Expand Down Expand Up @@ -1968,6 +1975,9 @@ mod test {

#[test]
fn minimize_nots_with_binary_expressions() {
test("!(x === undefined)", "x !== undefined");
test("!(typeof(x) === 'undefined')", "typeof x != 'undefined'");
test("!(x === void 0)", "x !== void 0");
test("!!delete x.y", "delete x.y");
test("!!!delete x.y", "!delete x.y");
test("!!!!delete x.y", "delete x.y");
Expand Down
2 changes: 1 addition & 1 deletion tasks/minsize/minsize.snap
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
| Oxc | ESBuild | Oxc | ESBuild |
Original | minified | minified | gzip | gzip | Fixture
-------------------------------------------------------------------------------------
72.14 kB | 23.71 kB | 23.70 kB | 8.61 kB | 8.54 kB | react.development.js
72.14 kB | 23.70 kB | 23.70 kB | 8.60 kB | 8.54 kB | react.development.js

173.90 kB | 59.79 kB | 59.82 kB | 19.41 kB | 19.33 kB | moment.js

Expand Down

0 comments on commit dab7a51

Please sign in to comment.