feat(minifier): minimize not !(x === undefined) -> x !== undefined (#8429)

This commit is contained in:
Boshen 2025-01-11 06:30:36 +00:00
parent 7f69561caf
commit dab7a51e78
2 changed files with 18 additions and 8 deletions

View file

@ -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(
@ -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");

View file

@ -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