refactor(minifier): add tests for remove_syntax (#5749)

This commit is contained in:
Boshen 2024-09-13 08:59:45 +00:00
parent 9a9d8f61d4
commit 2890c98d62
4 changed files with 39 additions and 3 deletions

View file

@ -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);
}
}

View file

@ -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);
}

View file

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

View file

@ -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);
}