mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
refactor(minifier): add tests for remove_syntax (#5749)
This commit is contained in:
parent
9a9d8f61d4
commit
2890c98d62
4 changed files with 39 additions and 3 deletions
|
|
@ -25,10 +25,13 @@ impl<'a> Traverse<'a> for RemoveSyntax {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn enter_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
|
fn enter_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
|
||||||
Self::strip_parenthesized_expression(expr, ctx);
|
|
||||||
self.compress_console(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(
|
fn exit_arrow_function_expression(
|
||||||
&mut self,
|
&mut self,
|
||||||
expr: &mut ArrowFunctionExpression<'a>,
|
expr: &mut ArrowFunctionExpression<'a>,
|
||||||
|
|
@ -46,7 +49,6 @@ impl<'a> RemoveSyntax {
|
||||||
fn strip_parenthesized_expression(expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
|
fn strip_parenthesized_expression(expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
|
||||||
if let Expression::ParenthesizedExpression(paren_expr) = expr {
|
if let Expression::ParenthesizedExpression(paren_expr) = expr {
|
||||||
*expr = ctx.ast.move_expression(&mut paren_expr.expression);
|
*expr = ctx.ast.move_expression(&mut paren_expr.expression);
|
||||||
Self::strip_parenthesized_expression(expr, ctx);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,11 @@ use oxc_minifier::CompressOptions;
|
||||||
|
|
||||||
// TODO: handle negative cases
|
// TODO: handle negative cases
|
||||||
fn test(source_text: &str, positive: &str, _negative: &str) {
|
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);
|
crate::test(source_text, positive, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,5 +3,6 @@ mod dead_code_elimination;
|
||||||
mod fold_conditions;
|
mod fold_conditions;
|
||||||
mod fold_constants;
|
mod fold_constants;
|
||||||
mod minimize_conditions;
|
mod minimize_conditions;
|
||||||
|
mod remove_syntax;
|
||||||
mod reorder_constant_expression;
|
mod reorder_constant_expression;
|
||||||
mod substitute_alternate_syntax;
|
mod substitute_alternate_syntax;
|
||||||
|
|
|
||||||
29
crates/oxc_minifier/tests/ast_passes/remove_syntax.rs
Normal file
29
crates/oxc_minifier/tests/ast_passes/remove_syntax.rs
Normal 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);
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue