Skip to content

Commit

Permalink
add short circuit check
Browse files Browse the repository at this point in the history
  • Loading branch information
humancalico committed Jul 9, 2020
1 parent 3398244 commit 5ca775b
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions src/rules/no_constant_condition.rs
Expand Up @@ -42,17 +42,31 @@ impl NoConstantConditionVisitor {
);
}

fn check_condition(&self, condition: &Expr) {
fn check_short_circuit(&self, expr: &Expr, operator: swc_ecma_ast::BinaryOp) -> bool {
match expr {
Expr::Lit::Bool(boolean) {
(operator == swc_ecma_ast::BinaryOp::LogicalOr && boolean.value == true) || (operator == swc_ecma_ast::BinaryOp::LogicalAnd && boolean.value == false)
},
Expr::Unary(unary) => operator == swc_ecma_ast::BinaryOp::LogicalAnd && unary.op == swc_ecma_ast::UnaryOp::Void,
Expr::Bin(bin) => {
if bin.op == swc_ecma_ast::BinaryOp::LogicalAnd || bin.op == swc_ecma_ast::BinaryOp::LogicalOr {
self.check_short_circuit(bin.left, bin.op) || self.check_short_circuit(bin.right, bin.op)
} else {false}
},
_ => false
}
}

fn is_constant(&self, condition: &Expr) -> (bool, Span) {
match condition {
Expr::Lit(lit) => self.add_diagnostic(lit.span()),
Expr::Lit(lit) => (true, lit.span()),
Expr::Bin(bin) => {
if bin.op != swc_ecma_ast::BinaryOp::In {
self.check_condition(&bin.left);
self.check_condition(&bin.right);
}
if bin.op == swc_ecma_ast::BinaryOp::LogicalOr || bin.op == swc_ecma_ast::BinaryOp::LogicalAnd {
let is_left_constant = self.is_constant(&bin.left).0;
let is_right_constant = self.is_constant(&bin.right).0;
} else {
(self.is_constant(&bin.left).0 && self.is_constant(&bin.right).0 && bin.op != swc_ecma_ast::BinaryOp::In, bin.span) }
}
_ => {}
}
}
}

Expand Down

0 comments on commit 5ca775b

Please sign in to comment.