File tree 1 file changed +26
-2
lines changed
crates/oxc_minifier/src/ast_passes
1 file changed +26
-2
lines changed Original file line number Diff line number Diff line change @@ -495,8 +495,24 @@ impl<'a, 'b> PeepholeRemoveDeadCode {
495
495
}
496
496
497
497
match ctx. get_boolean_value ( & expr. test ) {
498
- Some ( true ) => Some ( ctx. ast . move_expression ( & mut expr. consequent ) ) ,
499
- Some ( false ) => Some ( ctx. ast . move_expression ( & mut expr. alternate ) ) ,
498
+ Some ( v) => {
499
+ if expr. test . may_have_side_effects ( ) {
500
+ let mut exprs = ctx. ast . vec_with_capacity ( 2 ) ;
501
+ exprs. push ( ctx. ast . move_expression ( & mut expr. test ) ) ;
502
+ exprs. push ( ctx. ast . move_expression ( if v {
503
+ & mut expr. consequent
504
+ } else {
505
+ & mut expr. alternate
506
+ } ) ) ;
507
+ Some ( ctx. ast . expression_sequence ( expr. span , exprs) )
508
+ } else {
509
+ Some ( ctx. ast . move_expression ( if v {
510
+ & mut expr. consequent
511
+ } else {
512
+ & mut expr. alternate
513
+ } ) )
514
+ }
515
+ }
500
516
None => None ,
501
517
}
502
518
}
@@ -782,6 +798,14 @@ mod test {
782
798
test ( "if (true) {}" , "" ) ;
783
799
}
784
800
801
+ #[ test]
802
+ fn test_fold_conditional ( ) {
803
+ test ( "true ? foo() : bar()" , "foo()" ) ;
804
+ test ( "false ? foo() : bar()" , "bar()" ) ;
805
+ test_same ( "foo() ? bar() : baz()" ) ;
806
+ test ( "foo && false ? foo() : bar()" , "(foo && false, bar());" ) ;
807
+ }
808
+
785
809
#[ test]
786
810
fn test_fold_iife ( ) {
787
811
fold_same ( "var k = () => {}" ) ;
You can’t perform that action at this time.
0 commit comments