Skip to content

Commit

Permalink
refactor: improve logical expression logic to avoid unnecessary loops
Browse files Browse the repository at this point in the history
  • Loading branch information
GSTJ committed May 12, 2023
1 parent d091787 commit 8f15cdf
Showing 1 changed file with 6 additions and 10 deletions.
16 changes: 6 additions & 10 deletions src/rules/jsx-explicit-boolean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,13 @@ function checkBooleanValidity(node, context) {
// Example: a && b && c && <div />, where all operands are boolean
case "LogicalExpression": {
const { operator, left, right } = node;
const isAndOperator = operator === "&&";
const operands = [left, right];

if (isAndOperator) {
// Check boolean validity for all operands in the logical expression
return operands.every((operand) =>
checkBooleanValidity(operand, context)
);
}

return false;
if (operator !== "&&") return false;

return (
checkBooleanValidity(left, context) &&
checkBooleanValidity(right, context)
);
}

// Example: a ? b : c, where both b and c are boolean
Expand Down

0 comments on commit 8f15cdf

Please sign in to comment.