diff --git a/crates/oxc_minifier/src/peephole/convert_to_dotted_properties.rs b/crates/oxc_minifier/src/peephole/convert_to_dotted_properties.rs index e39abc9ee6277..7609a9b0b8c0a 100644 --- a/crates/oxc_minifier/src/peephole/convert_to_dotted_properties.rs +++ b/crates/oxc_minifier/src/peephole/convert_to_dotted_properties.rs @@ -91,10 +91,10 @@ mod test { #[test] fn test_convert_to_dotted_properties_quoted_props() { - test_same("({'':0})"); - test_same("({'1.0':0})"); - test("({'\\u1d17A':0})", "({ ᴗA: 0 })"); - // test("({'a\\u0004b':0})"); + test_same("v = ({'':0})"); + test_same("v = ({'1.0':0})"); + test("v = ({'\\u1d17A':0})", "v = ({ ᴗA: 0 })"); + // test("v = ({'a\\u0004b':0})"); } #[test] diff --git a/crates/oxc_minifier/src/peephole/minimize_conditions.rs b/crates/oxc_minifier/src/peephole/minimize_conditions.rs index f3e290672e216..282f8673b8096 100644 --- a/crates/oxc_minifier/src/peephole/minimize_conditions.rs +++ b/crates/oxc_minifier/src/peephole/minimize_conditions.rs @@ -1315,14 +1315,14 @@ mod test { #[test] fn test_try_compress_type_of_equal_string() { - test("typeof foo === 'number'", "typeof foo == 'number'"); - test("'number' === typeof foo", "typeof foo == 'number'"); - test("typeof foo === `number`", "typeof foo == 'number'"); - test("`number` === typeof foo", "typeof foo == 'number'"); - test("typeof foo !== 'number'", "typeof foo != 'number'"); - test("'number' !== typeof foo", "typeof foo != 'number'"); - test("typeof foo !== `number`", "typeof foo != 'number'"); - test("`number` !== typeof foo", "typeof foo != 'number'"); + test("v = typeof foo === 'number'", "v = typeof foo == 'number'"); + test("v = 'number' === typeof foo", "v = typeof foo == 'number'"); + test("v = typeof foo === `number`", "v = typeof foo == 'number'"); + test("v = `number` === typeof foo", "v = typeof foo == 'number'"); + test("v = typeof foo !== 'number'", "v = typeof foo != 'number'"); + test("v = 'number' !== typeof foo", "v = typeof foo != 'number'"); + test("v = typeof foo !== `number`", "v = typeof foo != 'number'"); + test("v = `number` !== typeof foo", "v = typeof foo != 'number'"); } #[test] diff --git a/crates/oxc_minifier/src/peephole/minimize_not_expression.rs b/crates/oxc_minifier/src/peephole/minimize_not_expression.rs index b27ebb078238f..b6a62df5fd5b0 100644 --- a/crates/oxc_minifier/src/peephole/minimize_not_expression.rs +++ b/crates/oxc_minifier/src/peephole/minimize_not_expression.rs @@ -83,7 +83,8 @@ mod test { #[test] fn minimize_nots_with_binary_expressions() { test("!(x === undefined)", "x !== void 0"); - test("!(typeof(x) === 'undefined')", "typeof x > 'u'"); + test("!(typeof(x) === 'undefined')", ""); + test("!(typeof(x()) === 'undefined')", "x() !== void 0"); test("!(x === void 0)", "x !== void 0"); test("!!delete x.y", "delete x.y"); test("!!!delete x.y", "delete x.y"); diff --git a/crates/oxc_minifier/src/peephole/remove_unused_expression.rs b/crates/oxc_minifier/src/peephole/remove_unused_expression.rs index ced3abb09d94d..b8210ffdb731f 100644 --- a/crates/oxc_minifier/src/peephole/remove_unused_expression.rs +++ b/crates/oxc_minifier/src/peephole/remove_unused_expression.rs @@ -13,17 +13,6 @@ impl<'a> PeepholeOptimizations { /// `SimplifyUnusedExpr`: pub fn remove_unused_expression(&self, e: &mut Expression<'a>, ctx: Ctx<'a, '_>) -> bool { match e { - Expression::NullLiteral(_) - | Expression::BooleanLiteral(_) - | Expression::NumericLiteral(_) - | Expression::BigIntLiteral(_) - | Expression::StringLiteral(_) - | Expression::ThisExpression(_) - | Expression::RegExpLiteral(_) - | Expression::FunctionExpression(_) - | Expression::ArrowFunctionExpression(_) - | Expression::MetaProperty(_) => true, - Expression::Identifier(ident) => ctx.symbols().has_binding(ident.reference_id()), Expression::ArrayExpression(_) => Self::fold_array_expression(e, ctx), Expression::UnaryExpression(_) => self.fold_unary_expression(e, ctx), Expression::NewExpression(e) => Self::fold_new_constructor(e, ctx), @@ -32,10 +21,13 @@ impl<'a> PeepholeOptimizations { // TODO // Expression::TemplateLiteral(_) // | Expression::ObjectExpression(_) - // | Expression::ConditionalExpression(_) => { + // | Expression::ConditionalExpression(_) + // | Expression::BinaryExpression(_) + // | Expression::CallExpression(_) + // | Expression::NewExpression(_) => { // false // } - _ => false, + _ => !e.may_have_side_effects(&ctx), } } diff --git a/crates/oxc_minifier/src/peephole/replace_known_methods.rs b/crates/oxc_minifier/src/peephole/replace_known_methods.rs index 715da58b4d1c1..8be02a5f491bf 100644 --- a/crates/oxc_minifier/src/peephole/replace_known_methods.rs +++ b/crates/oxc_minifier/src/peephole/replace_known_methods.rs @@ -1824,19 +1824,19 @@ mod test { #[test] fn test_fold_pow() { - test("Math.pow(2, 3)", "2 ** 3"); - test("Math.pow(a, 3)", "a ** 3"); - test("Math.pow(2, b)", "2 ** b"); - test("Math.pow(a, b)", "a ** +b"); - test("Math.pow(2n, 3n)", "2n ** +3n"); // errors both before and after - test("Math.pow(a + b, c)", "(a + b) ** +c"); - test_same("Math.pow()"); - test_same("Math.pow(1)"); - test_same("Math.pow(...a, 1)"); - test_same("Math.pow(1, ...a)"); - test_same("Math.pow(1, 2, 3)"); - test_es2015("Math.pow(2, 3)", "Math.pow(2, 3)"); - test_same("Unknown.pow(1, 2)"); + test("v = Math.pow(2, 3)", "v = 2 ** 3"); + test("v = Math.pow(a, 3)", "v = a ** 3"); + test("v = Math.pow(2, b)", "v = 2 ** b"); + test("v = Math.pow(a, b)", "v = a ** +b"); + test("v = Math.pow(2n, 3n)", "v = 2n ** +3n"); // errors both before and after + test("v = Math.pow(a + b, c)", "v = (a + b) ** +c"); + test_same("v = Math.pow()"); + test_same("v = Math.pow(1)"); + test_same("v = Math.pow(...a, 1)"); + test_same("v = Math.pow(1, ...a)"); + test_same("v = Math.pow(1, 2, 3)"); + test_es2015("v = Math.pow(2, 3)", "v = Math.pow(2, 3)"); + test_same("v = Unknown.pow(1, 2)"); } #[test] diff --git a/crates/oxc_minifier/src/peephole/substitute_alternate_syntax.rs b/crates/oxc_minifier/src/peephole/substitute_alternate_syntax.rs index bc057eb02bd58..7536668b08249 100644 --- a/crates/oxc_minifier/src/peephole/substitute_alternate_syntax.rs +++ b/crates/oxc_minifier/src/peephole/substitute_alternate_syntax.rs @@ -1517,47 +1517,47 @@ mod test { #[test] fn test_fold_is_typeof_equals_undefined_resolved() { - test("var x; typeof x !== 'undefined'", "var x; x !== void 0"); - test("var x; typeof x != 'undefined'", "var x; x !== void 0"); - test("var x; 'undefined' !== typeof x", "var x; x !== void 0"); - test("var x; 'undefined' != typeof x", "var x; x !== void 0"); + test("var x; v = typeof x !== 'undefined'", "var x; v = x !== void 0"); + test("var x; v = typeof x != 'undefined'", "var x; v = x !== void 0"); + test("var x; v = 'undefined' !== typeof x", "var x; v = x !== void 0"); + test("var x; v = 'undefined' != typeof x", "var x; v = x !== void 0"); - test("var x; typeof x === 'undefined'", "var x; x === void 0"); - test("var x; typeof x == 'undefined'", "var x; x === void 0"); - test("var x; 'undefined' === typeof x", "var x; x === void 0"); - test("var x; 'undefined' == typeof x", "var x; x === void 0"); + test("var x; v = typeof x === 'undefined'", "var x; v = x === void 0"); + test("var x; v = typeof x == 'undefined'", "var x; v = x === void 0"); + test("var x; v = 'undefined' === typeof x", "var x; v = x === void 0"); + test("var x; v = 'undefined' == typeof x", "var x; v = x === void 0"); test( - "var x; function foo() { typeof x !== 'undefined' }", - "var x; function foo() { x !== void 0 }", + "var x; function foo() { v = typeof x !== 'undefined' }", + "var x; function foo() { v = x !== void 0 }", ); test( - "typeof x !== 'undefined'; function foo() { var x }", - "typeof x < 'u'; function foo() { var x }", + "v = typeof x !== 'undefined'; function foo() { var x }", + "v = typeof x < 'u'; function foo() { var x }", ); - test("typeof x !== 'undefined'; { var x }", "x !== void 0; var x;"); - test("typeof x !== 'undefined'; { let x }", "typeof x < 'u'; { let x }"); - test("typeof x !== 'undefined'; var x", "x !== void 0; var x"); + test("v = typeof x !== 'undefined'; { var x }", "v = x !== void 0; var x;"); + test("v = typeof x !== 'undefined'; { let x }", "v = typeof x < 'u'; { let x }"); + test("v = typeof x !== 'undefined'; var x", "v = x !== void 0; var x"); // input and output both errors with same TDZ error - test("typeof x !== 'undefined'; let x", "x !== void 0; let x"); + test("v = typeof x !== 'undefined'; let x", "v = x !== void 0; let x"); - test("typeof x.y === 'undefined'", "x.y === void 0"); - test("typeof x.y !== 'undefined'", "x.y !== void 0"); - test("typeof (x + '') === 'undefined'", "x + '' === void 0"); + test("v = typeof x.y === 'undefined'", "v = x.y === void 0"); + test("v = typeof x.y !== 'undefined'", "v = x.y !== void 0"); + test("v = typeof (x + '') === 'undefined'", "v = x + '' === void 0"); } /// Port from #[test] fn test_fold_is_typeof_equals_undefined() { - test("typeof x !== 'undefined'", "typeof x < 'u'"); - test("typeof x != 'undefined'", "typeof x < 'u'"); - test("'undefined' !== typeof x", "typeof x < 'u'"); - test("'undefined' != typeof x", "typeof x < 'u'"); - - test("typeof x === 'undefined'", "typeof x > 'u'"); - test("typeof x == 'undefined'", "typeof x > 'u'"); - test("'undefined' === typeof x", "typeof x > 'u'"); - test("'undefined' == typeof x", "typeof x > 'u'"); + test("v = typeof x !== 'undefined'", "v = typeof x < 'u'"); + test("v = typeof x != 'undefined'", "v = typeof x < 'u'"); + test("v = 'undefined' !== typeof x", "v = typeof x < 'u'"); + test("v = 'undefined' != typeof x", "v = typeof x < 'u'"); + + test("v = typeof x === 'undefined'", "v = typeof x > 'u'"); + test("v = typeof x == 'undefined'", "v = typeof x > 'u'"); + test("v = 'undefined' === typeof x", "v = typeof x > 'u'"); + test("v = 'undefined' == typeof x", "v = typeof x > 'u'"); } #[test] diff --git a/tasks/minsize/minsize.snap b/tasks/minsize/minsize.snap index c93c99a2c306d..ab7cead98f8e8 100644 --- a/tasks/minsize/minsize.snap +++ b/tasks/minsize/minsize.snap @@ -15,7 +15,7 @@ Original | minified | minified | gzip | gzip | Fixture 1.01 MB | 440.17 kB | 458.89 kB | 122.38 kB | 126.71 kB | bundle.min.js -1.25 MB | 647.03 kB | 646.76 kB | 160.30 kB | 163.73 kB | three.js +1.25 MB | 647.02 kB | 646.76 kB | 160.29 kB | 163.73 kB | three.js 2.14 MB | 716.14 kB | 724.14 kB | 161.79 kB | 181.07 kB | victory.js