File tree Expand file tree Collapse file tree 1 file changed +26
-2
lines changed
crates/oxc_minifier/src/ast_passes Expand file tree Collapse file tree 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 {
495495 }
496496
497497 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+ }
500516 None => None ,
501517 }
502518 }
@@ -782,6 +798,14 @@ mod test {
782798 test ( "if (true) {}" , "" ) ;
783799 }
784800
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+
785809 #[ test]
786810 fn test_fold_iife ( ) {
787811 fold_same ( "var k = () => {}" ) ;
You can’t perform that action at this time.
0 commit comments