Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(minifier): add tests for remove_syntax #5749

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}