diff --git a/crates/oxc_minifier/src/ast_passes/remove_syntax.rs b/crates/oxc_minifier/src/ast_passes/remove_syntax.rs index 6abb14f115684..8a38721f4df7b 100644 --- a/crates/oxc_minifier/src/ast_passes/remove_syntax.rs +++ b/crates/oxc_minifier/src/ast_passes/remove_syntax.rs @@ -25,10 +25,13 @@ impl<'a> Traverse<'a> for RemoveSyntax { } fn enter_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) { - Self::strip_parenthesized_expression(expr, ctx); self.compress_console(expr, ctx); } + fn exit_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) { + Self::strip_parenthesized_expression(expr, ctx); + } + fn exit_arrow_function_expression( &mut self, expr: &mut ArrowFunctionExpression<'a>, @@ -46,7 +49,6 @@ impl<'a> RemoveSyntax { fn strip_parenthesized_expression(expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) { if let Expression::ParenthesizedExpression(paren_expr) = expr { *expr = ctx.ast.move_expression(&mut paren_expr.expression); - Self::strip_parenthesized_expression(expr, ctx); } } diff --git a/crates/oxc_minifier/tests/ast_passes/minimize_conditions.rs b/crates/oxc_minifier/tests/ast_passes/minimize_conditions.rs index d152d2502dbc9..7ce33c465d804 100644 --- a/crates/oxc_minifier/tests/ast_passes/minimize_conditions.rs +++ b/crates/oxc_minifier/tests/ast_passes/minimize_conditions.rs @@ -4,7 +4,11 @@ use oxc_minifier::CompressOptions; // TODO: handle negative cases fn test(source_text: &str, positive: &str, _negative: &str) { - let options = CompressOptions { minimize_conditions: true, ..CompressOptions::all_false() }; + let options = CompressOptions { + remove_syntax: true, + minimize_conditions: true, + ..CompressOptions::all_false() + }; crate::test(source_text, positive, options); } diff --git a/crates/oxc_minifier/tests/ast_passes/mod.rs b/crates/oxc_minifier/tests/ast_passes/mod.rs index 98033dab287bb..0aca536ff915f 100644 --- a/crates/oxc_minifier/tests/ast_passes/mod.rs +++ b/crates/oxc_minifier/tests/ast_passes/mod.rs @@ -3,5 +3,6 @@ mod dead_code_elimination; mod fold_conditions; mod fold_constants; mod minimize_conditions; +mod remove_syntax; mod reorder_constant_expression; mod substitute_alternate_syntax; diff --git a/crates/oxc_minifier/tests/ast_passes/remove_syntax.rs b/crates/oxc_minifier/tests/ast_passes/remove_syntax.rs new file mode 100644 index 0000000000000..1c7c92ff66118 --- /dev/null +++ b/crates/oxc_minifier/tests/ast_passes/remove_syntax.rs @@ -0,0 +1,29 @@ +use oxc_minifier::CompressOptions; + +fn test(source_text: &str, expected: &str) { + let options = CompressOptions { remove_syntax: true, ..CompressOptions::all_false() }; + crate::test(source_text, expected, options); +} + +#[test] +fn parens() { + test("(((x)))", "x"); + test("(((a + b))) * c", "(a + b) * c"); +} + +#[test] +fn drop_console() { + let options = + CompressOptions { remove_syntax: true, drop_console: true, ..CompressOptions::all_false() }; + crate::test("console.log()", "", options); +} + +#[test] +fn drop_debugger() { + let options = CompressOptions { + remove_syntax: true, + drop_debugger: true, + ..CompressOptions::all_false() + }; + crate::test("debugger", "", options); +}