@@ -11,14 +11,17 @@ impl<'a> PeepholeOptimizations {
1111 /// Simplify syntax when we know it's used inside a boolean context, e.g. `if (boolean_context) {}`.
1212 ///
1313 /// `SimplifyBooleanExpr`: <https://github.com/evanw/esbuild/blob/v0.24.2/internal/js_ast/js_ast_helpers.go#L2059>
14- pub fn try_fold_expr_in_boolean_context ( expr : & mut Expression < ' a > , ctx : & mut Ctx < ' a , ' _ > ) {
14+ pub fn minimize_expression_in_boolean_context (
15+ expr : & mut Expression < ' a > ,
16+ ctx : & mut Ctx < ' a , ' _ > ,
17+ ) {
1518 match expr {
1619 // "!!a" => "a"
1720 Expression :: UnaryExpression ( u1) if u1. operator . is_not ( ) => {
1821 if let Expression :: UnaryExpression ( u2) = & mut u1. argument {
1922 if u2. operator . is_not ( ) {
2023 let mut e = u2. argument . take_in ( ctx. ast ) ;
21- Self :: try_fold_expr_in_boolean_context ( & mut e, ctx) ;
24+ Self :: minimize_expression_in_boolean_context ( & mut e, ctx) ;
2225 * expr = e;
2326 ctx. state . changed = true ;
2427 }
@@ -44,8 +47,8 @@ impl<'a> PeepholeOptimizations {
4447 }
4548 // "if (!!a && !!b)" => "if (a && b)"
4649 Expression :: LogicalExpression ( e) if e. operator . is_and ( ) => {
47- Self :: try_fold_expr_in_boolean_context ( & mut e. left , ctx) ;
48- Self :: try_fold_expr_in_boolean_context ( & mut e. right , ctx) ;
50+ Self :: minimize_expression_in_boolean_context ( & mut e. left , ctx) ;
51+ Self :: minimize_expression_in_boolean_context ( & mut e. right , ctx) ;
4952 // "if (anything && truthyNoSideEffects)" => "if (anything)"
5053 if e. right . get_side_free_boolean_value ( ctx) == Some ( true ) {
5154 * expr = e. left . take_in ( ctx. ast ) ;
@@ -54,8 +57,8 @@ impl<'a> PeepholeOptimizations {
5457 }
5558 // "if (!!a ||!!b)" => "if (a || b)"
5659 Expression :: LogicalExpression ( e) if e. operator == LogicalOperator :: Or => {
57- Self :: try_fold_expr_in_boolean_context ( & mut e. left , ctx) ;
58- Self :: try_fold_expr_in_boolean_context ( & mut e. right , ctx) ;
60+ Self :: minimize_expression_in_boolean_context ( & mut e. left , ctx) ;
61+ Self :: minimize_expression_in_boolean_context ( & mut e. right , ctx) ;
5962 // "if (anything || falsyNoSideEffects)" => "if (anything)"
6063 if e. right . get_side_free_boolean_value ( ctx) == Some ( false ) {
6164 * expr = e. left . take_in ( ctx. ast ) ;
@@ -64,8 +67,8 @@ impl<'a> PeepholeOptimizations {
6467 }
6568 Expression :: ConditionalExpression ( e) => {
6669 // "if (a ? !!b : !!c)" => "if (a ? b : c)"
67- Self :: try_fold_expr_in_boolean_context ( & mut e. consequent , ctx) ;
68- Self :: try_fold_expr_in_boolean_context ( & mut e. alternate , ctx) ;
70+ Self :: minimize_expression_in_boolean_context ( & mut e. consequent , ctx) ;
71+ Self :: minimize_expression_in_boolean_context ( & mut e. alternate , ctx) ;
6972 if let Some ( boolean) = e. consequent . get_side_free_boolean_value ( ctx) {
7073 let right = e. alternate . take_in ( ctx. ast ) ;
7174 let left = e. test . take_in ( ctx. ast ) ;
@@ -98,7 +101,7 @@ impl<'a> PeepholeOptimizations {
98101 }
99102 Expression :: SequenceExpression ( seq_expr) => {
100103 if let Some ( last) = seq_expr. expressions . last_mut ( ) {
101- Self :: try_fold_expr_in_boolean_context ( last, ctx) ;
104+ Self :: minimize_expression_in_boolean_context ( last, ctx) ;
102105 }
103106 }
104107 _ => { }
0 commit comments