Skip to content

Commit

Permalink
add option for recursive expression checks
Browse files Browse the repository at this point in the history
  • Loading branch information
kirkwaiblinger committed Apr 26, 2024
1 parent 8326217 commit 822ec6c
Show file tree
Hide file tree
Showing 2 changed files with 1,412 additions and 327 deletions.
65 changes: 45 additions & 20 deletions lib/rules/no-extra-boolean-cast.js
Expand Up @@ -35,6 +35,11 @@ module.exports = {
enforceForLogicalOperands: {
type: "boolean",
default: false
},

enforceForInnerOperands: {
type: "boolean",
default: false
}
},
additionalProperties: false
Expand All @@ -49,6 +54,9 @@ module.exports = {

create(context) {
const sourceCode = context.sourceCode;
const enforceForLogicalOperands = context.options[0]?.enforceForLogicalOperands === true;
const enforceForInnerOperands = context.options[0]?.enforceForInnerOperands === true;


// Node types which have a test which will coerce values to booleans.
const BOOLEAN_NODE_TYPES = new Set([
Expand Down Expand Up @@ -79,9 +87,7 @@ module.exports = {
*/
function isLogicalContext(node) {
return node.type === "LogicalExpression" &&
(node.operator === "||" || node.operator === "&&") &&
(context.options.length && context.options[0].enforceForLogicalOperands === true);

(node.operator === "||" || node.operator === "&&");
}


Expand Down Expand Up @@ -115,29 +121,48 @@ module.exports = {
return isInFlaggedContext(node.parent);
}

if (node.parent.type === "ConditionalExpression" && (node.parent.consequent === node || node.parent.alternate === node)) {
return isInFlaggedContext(node.parent);
}

/*
* Check last expression only in a sequence, i.e. if ((1, 2, Boolean(3))) {}, since
* the others don't affect the result of the expression.
* legacy behavior - enforceForLogicalOperands will only recurse on
* logical expressions, not on other contexts.
* enforceForInnerOperands will recurse on the rest.
*/
if (node.parent.type === "SequenceExpression" && node.parent.expressions.at(-1) === node) {
return isInFlaggedContext(node.parent);
}

// Check the right hand side of a `??` operator.
if (node.parent.type === "LogicalExpression" && node.parent.operator === "??" && node.parent.right === node) {
return isInFlaggedContext(node.parent);
if (enforceForLogicalOperands || enforceForInnerOperands) {
if (isLogicalContext(node.parent)) {
return isInFlaggedContext(node.parent);
}

}

return isInBooleanContext(node) ||
(isLogicalContext(node.parent) &&
if (enforceForInnerOperands) {
if (
node.parent.type === "ConditionalExpression" &&
(node.parent.consequent === node || node.parent.alternate === node)
) {
return isInFlaggedContext(node.parent);
}

// For nested logical statements
isInFlaggedContext(node.parent)
);
/*
* Check last expression only in a sequence, i.e. if ((1, 2, Boolean(3))) {}, since
* the others don't affect the result of the expression.
*/
if (
node.parent.type === "SequenceExpression" &&
node.parent.expressions.at(-1) === node
) {
return isInFlaggedContext(node.parent);
}

// Check the right hand side of a `??` operator.
if (
node.parent.type === "LogicalExpression" && node.parent.operator === "??" &&
node.parent.right === node
) {
return isInFlaggedContext(node.parent);
}
}

return isInBooleanContext(node);
}


Expand Down

0 comments on commit 822ec6c

Please sign in to comment.