Skip to content

Commit

Permalink
feat: detect side effects in ConditionalExpression (#552)
Browse files Browse the repository at this point in the history
* feat: detect side effects in ConditionalExpression

* test: add test for conditional expressions

* chore: add todo comments

* chore: remove todos
  • Loading branch information
Demivan committed Mar 11, 2024
1 parent 10799d2 commit 4ec29f7
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion crates/rolldown/src/ast_scanner/side_effect_detector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@ impl<'a> SideEffectDetector<'a> {
Expression::SequenceExpression(seq_expr) => {
seq_expr.expressions.iter().any(|expr| self.detect_side_effect_of_expr(expr))
}
Expression::ConditionalExpression(cond_expr) => {
self.detect_side_effect_of_expr(&cond_expr.test)
|| self.detect_side_effect_of_expr(&cond_expr.consequent)
|| self.detect_side_effect_of_expr(&cond_expr.alternate)
}
Expression::TSAsExpression(_)
| Expression::TSSatisfiesExpression(_)
| Expression::TSTypeAssertion(_)
Expand All @@ -166,7 +171,6 @@ impl<'a> SideEffectDetector<'a> {
| Expression::BinaryExpression(_)
| Expression::CallExpression(_)
| Expression::ChainExpression(_)
| Expression::ConditionalExpression(_)
| Expression::ImportExpression(_)
| Expression::NewExpression(_)
| Expression::TaggedTemplateExpression(_)
Expand Down Expand Up @@ -366,4 +370,14 @@ mod test {
assert!(get_statements_side_effect("true, bar"));
assert!(get_statements_side_effect("foo, true"));
}

#[test]
fn test_conditional_expression() {
assert!(!get_statements_side_effect("true ? false : true"));
assert!(!get_statements_side_effect("null ? true : false"));
// accessing global variable may have side effect
assert!(get_statements_side_effect("true ? bar : true"));
assert!(get_statements_side_effect("foo ? true : false"));
assert!(get_statements_side_effect("true ? bar : true"));
}
}

0 comments on commit 4ec29f7

Please sign in to comment.