Skip to content

Commit

Permalink
refactor(minifier): add tests for remove_syntax (#5749)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boshen committed Sep 13, 2024
1 parent 9a9d8f6 commit 2890c98
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
6 changes: 4 additions & 2 deletions crates/oxc_minifier/src/ast_passes/remove_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
Expand All @@ -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);
}
}

Expand Down
6 changes: 5 additions & 1 deletion crates/oxc_minifier/tests/ast_passes/minimize_conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
1 change: 1 addition & 0 deletions crates/oxc_minifier/tests/ast_passes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
29 changes: 29 additions & 0 deletions crates/oxc_minifier/tests/ast_passes/remove_syntax.rs
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 2890c98

Please sign in to comment.