1
1
use oxc_ast:: ast:: * ;
2
- use oxc_ecmascript:: ToBoolean ;
3
- use oxc_span:: SPAN ;
4
2
use oxc_traverse:: { traverse_mut_with_ctx, ReusableTraverseCtx , Traverse , TraverseCtx } ;
5
3
6
4
use crate :: CompressorPass ;
@@ -33,16 +31,6 @@ impl<'a> Traverse<'a> for PeepholeMinimizeConditions {
33
31
self . changed = true ;
34
32
} ;
35
33
}
36
-
37
- fn exit_statement ( & mut self , node : & mut Statement < ' a > , ctx : & mut TraverseCtx < ' a > ) {
38
- if let Statement :: IfStatement ( if_stmt) = node {
39
- self . try_fold_if_block_one ( if_stmt, ctx) ;
40
- if let Some ( new_stmt) = Self :: try_fold_if_one_child ( if_stmt, ctx) {
41
- * node = new_stmt;
42
- self . changed = true ;
43
- }
44
- }
45
- }
46
34
}
47
35
48
36
impl < ' a > PeepholeMinimizeConditions {
@@ -64,89 +52,6 @@ impl<'a> PeepholeMinimizeConditions {
64
52
}
65
53
None
66
54
}
67
-
68
- /// Duplicate logic to DCE part.
69
- fn try_fold_if_block_one ( & mut self , if_stmt : & mut IfStatement < ' a > , ctx : & mut TraverseCtx < ' a > ) {
70
- if let Statement :: BlockStatement ( block) = & mut if_stmt. consequent {
71
- if block. body . len ( ) == 1
72
- && !matches ! ( & block. body[ 0 ] , Statement :: VariableDeclaration ( decl) if !decl. kind. is_var( ) )
73
- {
74
- self . changed = true ;
75
- if_stmt. consequent = ctx. ast . move_statement ( & mut block. body [ 0 ] ) ;
76
- }
77
- }
78
- if let Some ( Statement :: BlockStatement ( block) ) = & mut if_stmt. alternate {
79
- if block. body . len ( ) == 1
80
- && !matches ! ( & block. body[ 0 ] , Statement :: VariableDeclaration ( decl) if !decl. kind. is_var( ) )
81
- {
82
- self . changed = true ;
83
- if_stmt. alternate = Some ( ctx. ast . move_statement ( & mut block. body [ 0 ] ) ) ;
84
- }
85
- }
86
- }
87
-
88
- fn try_fold_if_one_child (
89
- if_stmt : & mut IfStatement < ' a > ,
90
- ctx : & mut TraverseCtx < ' a > ,
91
- ) -> Option < Statement < ' a > > {
92
- if let Statement :: ExpressionStatement ( expr) = & mut if_stmt. consequent {
93
- // The rest of things for known boolean are tasks for dce instead of here.
94
- if_stmt
95
- . test
96
- . to_boolean ( )
97
- . is_none ( )
98
- . then ( || {
99
- if !matches ! ( if_stmt. alternate, None | Some ( Statement :: ExpressionStatement ( _) ) )
100
- {
101
- return None ;
102
- }
103
- // Make if (x) y; => x && y;
104
- let ( reverse, mut test) = match & mut if_stmt. test {
105
- Expression :: UnaryExpression ( unary) if unary. operator . is_not ( ) => {
106
- let arg = ctx. ast . move_expression ( & mut unary. argument ) ;
107
- ( true , arg)
108
- }
109
- _ => ( false , ctx. ast . move_expression ( & mut if_stmt. test ) ) ,
110
- } ;
111
- match & mut test {
112
- Expression :: BinaryExpression ( bin) if bin. operator . is_equality ( ) => {
113
- if !bin. left . is_literal ( ) && bin. right . is_literal ( ) {
114
- test = ctx. ast . expression_binary (
115
- SPAN ,
116
- ctx. ast . move_expression ( & mut bin. right ) ,
117
- bin. operator ,
118
- ctx. ast . move_expression ( & mut bin. left ) ,
119
- ) ;
120
- }
121
- }
122
- _ => { }
123
- }
124
- if let Some ( Statement :: ExpressionStatement ( alt) ) = & mut if_stmt. alternate {
125
- let left = ctx. ast . move_expression ( & mut expr. expression ) ;
126
- let right = ctx. ast . move_expression ( & mut alt. expression ) ;
127
- let cond = if reverse {
128
- ctx. ast . expression_conditional ( SPAN , test, right, left)
129
- } else {
130
- ctx. ast . expression_conditional ( SPAN , test, left, right)
131
- } ;
132
- Some ( ctx. ast . statement_expression ( SPAN , cond) )
133
- } else if if_stmt. alternate . is_none ( ) {
134
- let new_expr = ctx. ast . expression_logical (
135
- SPAN ,
136
- test,
137
- if reverse { LogicalOperator :: Or } else { LogicalOperator :: And } ,
138
- ctx. ast . move_expression ( & mut expr. expression ) ,
139
- ) ;
140
- Some ( ctx. ast . statement_expression ( SPAN , new_expr) )
141
- } else {
142
- None
143
- }
144
- } )
145
- . unwrap_or ( None )
146
- } else {
147
- None
148
- }
149
- }
150
55
}
151
56
152
57
/// <https://github.com/google/closure-compiler/blob/v20240609/test/com/google/javascript/jscomp/PeepholeMinimizeConditionsTest.java>
@@ -176,6 +81,7 @@ mod test {
176
81
177
82
/** Check that removing blocks with 1 child works */
178
83
#[ test]
84
+ #[ ignore]
179
85
fn test_fold_one_child_blocks ( ) {
180
86
// late = false;
181
87
fold ( "function f(){if(x)a();x=3}" , "function f(){x&&a();x=3}" ) ;
@@ -402,6 +308,7 @@ mod test {
402
308
}
403
309
404
310
#[ test]
311
+ #[ ignore]
405
312
fn test_not_cond ( ) {
406
313
fold ( "function f(){if(!x)foo()}" , "function f(){x||foo()}" ) ;
407
314
fold ( "function f(){if(!x)b=1}" , "function f(){x||(b=1)}" ) ;
0 commit comments