Skip to content

Commit

Permalink
fix: [no-extra-boolean-cast] inspect comma expressions and ?? express…
Browse files Browse the repository at this point in the history
…ions.

Fixes eslint#18186
  • Loading branch information
kirkwaiblinger committed Mar 21, 2024
1 parent 09bd7fe commit ce5ac38
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/rules/no-extra-boolean-cast.js
Expand Up @@ -115,6 +115,19 @@ module.exports = {
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.
*/
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) ||
(isLogicalContext(node.parent) &&

Expand Down Expand Up @@ -157,6 +170,7 @@ module.exports = {
if (previousNode.parent.type === "ChainExpression") {
return needsParens(previousNode.parent, node);
}

if (isParenthesized(previousNode)) {

// parentheses around the previous node will stay, so there is no need for an additional pair
Expand All @@ -174,6 +188,7 @@ module.exports = {
case "DoWhileStatement":
case "WhileStatement":
case "ForStatement":
case "SequenceExpression":
return false;
case "ConditionalExpression":
return precedence(node) <= precedence(parent);
Expand Down
30 changes: 30 additions & 0 deletions tests/lib/rules/no-extra-boolean-cast.js
Expand Up @@ -34,6 +34,7 @@ ruleTester.run("no-extra-boolean-cast", rule, {
"for(Boolean(foo);;) {}",
"for(;; Boolean(foo)) {}",
"if (new Boolean(foo)) {}",
"if ((Boolean(1), 2)) {}",
{
code: "var foo = bar || !!baz",
options: [{ enforceForLogicalOperands: true }]
Expand Down Expand Up @@ -2435,6 +2436,35 @@ ruleTester.run("no-extra-boolean-cast", rule, {
ecmaVersion: 2020
},
errors: [{ messageId: "unexpectedCall" }]
},
{
code: "if ((1, 2, Boolean(3))) {}",
output: "if ((1, 2, 3)) {}",
errors: [{ messageId: "unexpectedCall" }]
},
{
code: "if (a ?? Boolean(b)) {}",
output: "if (a ?? b) {}",
options: [{ enforceForLogicalOperands: true }],
errors: [{ messageId: "unexpectedCall" }]
},
{
code: "if (a ?? Boolean(b)) {}",
output: "if (a ?? b) {}",
options: [{ enforceForLogicalOperands: false }],
errors: [{ messageId: "unexpectedCall" }]
},
{
code: "if ((a, b, c ?? (d, e, f ?? Boolean(g)))) {}",
output: "if ((a, b, c ?? (d, e, f ?? g))) {}",
options: [{ enforceForLogicalOperands: false }],
errors: [{ messageId: "unexpectedCall" }]
},
{
code: "if ((a, b, c ?? (d, e, f ?? Boolean(g)))) {}",
output: "if ((a, b, c ?? (d, e, f ?? g))) {}",
options: [{ enforceForLogicalOperands: true }],
errors: [{ messageId: "unexpectedCall" }]
}
]
});

0 comments on commit ce5ac38

Please sign in to comment.