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): cleanup peephole_minimize_conditions #8114

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
10 changes: 6 additions & 4 deletions crates/oxc_minifier/src/ast_passes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,13 @@ pub struct LatePeepholeOptimizations {

impl LatePeepholeOptimizations {
pub fn new() -> Self {
let in_fixed_loop = true;
Self {
x0_statement_fusion: StatementFusion::new(),
x1_peephole_remove_dead_code: PeepholeRemoveDeadCode::new(),
x2_peephole_minimize_conditions: PeepholeMinimizeConditions::new(),
x2_peephole_minimize_conditions: PeepholeMinimizeConditions::new(in_fixed_loop),
x3_peephole_substitute_alternate_syntax: PeepholeSubstituteAlternateSyntax::new(
/* in_fixed_loop */ true,
in_fixed_loop,
),
x4_peephole_replace_known_methods: PeepholeReplaceKnownMethods::new(),
x5_peephole_fold_constants: PeepholeFoldConstants::new(),
Expand Down Expand Up @@ -195,10 +196,11 @@ pub struct PeepholeOptimizations {

impl PeepholeOptimizations {
pub fn new() -> Self {
let in_fixed_loop = false;
Self {
x2_peephole_minimize_conditions: PeepholeMinimizeConditions::new(),
x2_peephole_minimize_conditions: PeepholeMinimizeConditions::new(in_fixed_loop),
x3_peephole_substitute_alternate_syntax: PeepholeSubstituteAlternateSyntax::new(
/* in_fixed_loop */ false,
in_fixed_loop,
),
x4_peephole_replace_known_methods: PeepholeReplaceKnownMethods::new(),
x5_peephole_remove_dead_code: PeepholeRemoveDeadCode::new(),
Expand Down
40 changes: 15 additions & 25 deletions crates/oxc_minifier/src/ast_passes/peephole_minimize_conditions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ use crate::CompressorPass;
///
/// <https://github.com/google/closure-compiler/blob/v20240609/src/com/google/javascript/jscomp/PeepholeMinimizeConditions.java>
pub struct PeepholeMinimizeConditions {
/// Do not compress syntaxes that are hard to analyze inside the fixed loop.
#[allow(unused)]
in_fixed_loop: bool,

pub(crate) changed: bool,
}

Expand All @@ -24,7 +28,7 @@ impl<'a> CompressorPass<'a> for PeepholeMinimizeConditions {
impl<'a> Traverse<'a> for PeepholeMinimizeConditions {
fn exit_expression(&mut self, expr: &mut Expression<'a>, ctx: &mut TraverseCtx<'a>) {
if let Some(folded_expr) = match expr {
Expression::UnaryExpression(e) if e.operator.is_not() => Self::try_minimize_not(e, ctx),
Expression::UnaryExpression(e) => Self::try_minimize_not(e, ctx),
_ => None,
} {
*expr = folded_expr;
Expand All @@ -34,23 +38,23 @@ impl<'a> Traverse<'a> for PeepholeMinimizeConditions {
}

impl<'a> PeepholeMinimizeConditions {
pub fn new() -> Self {
Self { changed: false }
pub fn new(in_fixed_loop: bool) -> Self {
Self { in_fixed_loop, changed: false }
}

/// Try to minimize NOT nodes such as `!(x==y)`.
fn try_minimize_not(
expr: &mut UnaryExpression<'a>,
ctx: &mut TraverseCtx<'a>,
) -> Option<Expression<'a>> {
debug_assert!(expr.operator.is_not());
if let Expression::BinaryExpression(binary_expr) = &mut expr.argument {
if let Some(new_op) = binary_expr.operator.equality_inverse_operator() {
binary_expr.operator = new_op;
return Some(ctx.ast.move_expression(&mut expr.argument));
}
// TODO: tryMinimizeCondition(node.getFirstChild());
if !expr.operator.is_not() {
return None;
}
None
let Expression::BinaryExpression(binary_expr) = &mut expr.argument else { return None };
let new_op = binary_expr.operator.equality_inverse_operator()?;
binary_expr.operator = new_op;
Some(ctx.ast.move_expression(&mut expr.argument))
}
}

Expand All @@ -63,7 +67,7 @@ mod test {

fn test(source_text: &str, positive: &str) {
let allocator = Allocator::default();
let mut pass = super::PeepholeMinimizeConditions::new();
let mut pass = super::PeepholeMinimizeConditions::new(true);
tester::test(&allocator, source_text, positive, &mut pass);
}

Expand Down Expand Up @@ -951,15 +955,13 @@ mod test {
}

#[test]
#[ignore]
fn test_remove_else_cause3() {
test_same("function f() { a:{if (x) break a; else f() } }");
test_same("function f() { if (x) { a:{ break a } } else f() }");
test_same("function f() { if (x) a:{ break a } else f() }");
}

#[test]
#[ignore]
fn test_remove_else_cause4() {
test_same("function f() { if (x) { if (y) { return 1; } } else f() }");
}
Expand Down Expand Up @@ -1000,7 +1002,6 @@ mod test {
}

#[test]
#[ignore]
fn test_coercion_substitution_disabled() {
// enableTypeCheck();
test_same("var x = {}; if (x != null) throw 'a';");
Expand All @@ -1011,14 +1012,12 @@ mod test {
}

#[test]
#[ignore]
fn test_coercion_substitution_boolean_result0() {
// enableTypeCheck();
test_same("var x = {}; var y = x != null;");
}

#[test]
#[ignore]
fn test_coercion_substitution_boolean_result1() {
// enableTypeCheck();
test_same("var x = {}; var y = x == null;");
Expand Down Expand Up @@ -1060,15 +1059,13 @@ mod test {
}

#[test]
#[ignore]
fn test_coercion_substitution_expression() {
// enableTypeCheck();
test_same("var x = {}; x != null && alert('b');");
test_same("var x = 1; x != 0 && alert('b');");
}

#[test]
#[ignore]
fn test_coercion_substitution_hook() {
// enableTypeCheck();
test_same(concat!("var x = {};", "var y = x != null ? 1 : 2;"));
Expand All @@ -1087,31 +1084,27 @@ mod test {
}

#[test]
#[ignore]
fn test_coercion_substitution_while() {
// enableTypeCheck();
test_same("var x = {}; while (x != null) throw 'a';");
test_same("var x = 1; while (x != 0) throw 'a';");
}

#[test]
#[ignore]
fn test_coercion_substitution_unknown_type() {
// enableTypeCheck();
test_same("var x = /** @type {?} */ ({});\nif (x != null) throw 'a';\n");
test_same("var x = /** @type {?} */ (1);\nif (x != 0) throw 'a';\n");
}

#[test]
#[ignore]
fn test_coercion_substitution_all_type() {
// enableTypeCheck();
test_same("var x = /** @type {*} */ ({});\nif (x != null) throw 'a';\n");
test_same("var x = /** @type {*} */ (1);\nif (x != 0) throw 'a';\n");
}

#[test]
#[ignore]
fn test_coercion_substitution_primitives_vs_null() {
// enableTypeCheck();
test_same("var x = 0;\nif (x != null) throw 'a';\n");
Expand All @@ -1120,7 +1113,6 @@ mod test {
}

#[test]
#[ignore]
fn test_coercion_substitution_non_number_vs_zero() {
// enableTypeCheck();
test_same("var x = {};\nif (x != 0) throw 'a';\n");
Expand All @@ -1129,14 +1121,12 @@ mod test {
}

#[test]
#[ignore]
fn test_coercion_substitution_boxed_number_vs_zero() {
// enableTypeCheck();
test_same("var x = new Number(0);\nif (x != 0) throw 'a';\n");
}

#[test]
#[ignore]
fn test_coercion_substitution_boxed_primitives() {
// enableTypeCheck();
test_same("var x = new Number(); if (x != null) throw 'a';");
Expand Down
Loading