File tree 2 files changed +11
-9
lines changed
2 files changed +11
-9
lines changed Original file line number Diff line number Diff line change @@ -44,20 +44,16 @@ impl<'a> RemoveDeadCode<'a> {
44
44
}
45
45
}
46
46
47
- pub fn remove_conditional ( & mut self , stmt : & mut Statement < ' a > ) {
48
- let Statement :: ExpressionStatement ( expression_stmt) = stmt else { return } ;
49
- let Expression :: ConditionalExpression ( conditional_expr) = & mut expression_stmt. expression
50
- else {
47
+ pub fn remove_conditional ( & mut self , expr : & mut Expression < ' a > ) {
48
+ let Expression :: ConditionalExpression ( conditional_expr) = expr else {
51
49
return ;
52
50
} ;
53
51
match self . test_expression ( & mut conditional_expr. test ) {
54
52
Some ( true ) => {
55
- expression_stmt. expression =
56
- self . ast . move_expression ( & mut conditional_expr. consequent ) ;
53
+ * expr = self . ast . move_expression ( & mut conditional_expr. consequent ) ;
57
54
}
58
55
Some ( false ) => {
59
- expression_stmt. expression =
60
- self . ast . move_expression ( & mut conditional_expr. alternate ) ;
56
+ * expr = self . ast . move_expression ( & mut conditional_expr. alternate ) ;
61
57
}
62
58
_ => { }
63
59
}
@@ -67,7 +63,10 @@ impl<'a> RemoveDeadCode<'a> {
67
63
impl < ' a > VisitMut < ' a > for RemoveDeadCode < ' a > {
68
64
fn visit_statement ( & mut self , stmt : & mut Statement < ' a > ) {
69
65
self . remove_if ( stmt) ;
70
- self . remove_conditional ( stmt) ;
71
66
walk_mut:: walk_statement ( self , stmt) ;
72
67
}
68
+
69
+ fn visit_expression ( & mut self , expr : & mut Expression < ' a > ) {
70
+ self . remove_conditional ( expr) ;
71
+ }
73
72
}
Original file line number Diff line number Diff line change @@ -42,6 +42,9 @@ fn remove_dead_code() {
42
42
test ( "!!false ? foo : bar;" , "bar" ) ;
43
43
test ( "!!true ? foo : bar;" , "foo" ) ;
44
44
45
+ test ( "const foo = true ? A : B" , "const foo=A" ) ;
46
+ test ( "const foo = false ? A : B" , "const foo=B" ) ;
47
+
45
48
// Shadowed `undefined` as a variable should not be erased.
46
49
test (
47
50
"function foo(undefined) { if (!undefined) { } }" ,
You can’t perform that action at this time.
0 commit comments