Skip to content

Commit

Permalink
update no-extra-boolean-cast
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea committed Jun 22, 2020
1 parent b91d17b commit d0c2f82
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/rules/no-extra-boolean-cast.js
Expand Up @@ -111,6 +111,10 @@ module.exports = {
* @returns {boolean} If the node is in one of the flagged contexts
*/
function isInFlaggedContext(node) {
if (node.parent.type === "ChainExpression") {
return isInFlaggedContext(node.parent);
}

return isInBooleanContext(node) ||
(isLogicalContext(node.parent) &&

Expand Down Expand Up @@ -149,6 +153,9 @@ module.exports = {
* @returns {boolean} `true` if the node needs to be parenthesized.
*/
function needsParens(previousNode, node) {
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 Down
15 changes: 15 additions & 0 deletions tests/lib/rules/no-extra-boolean-cast.js
Expand Up @@ -2408,6 +2408,21 @@ ruleTester.run("no-extra-boolean-cast", rule, {
options: [{ enforceForLogicalOperands: true }],
parserOptions: { ecmaVersion: 2020 },
errors: [{ messageId: "unexpectedCall", type: "CallExpression" }]
},

// Optional chaining
{
code: "if (Boolean?.(foo)) ;",
output: "if (foo) ;",
parserOptions: { ecmaVersion: 2020 },
errors: [{ messageId: "unexpectedCall" }]
},
{
code: "if (Boolean?.(a ?? b) || c) {}",
output: "if ((a ?? b) || c) {}",
options: [{ enforceForLogicalOperands: true }],
parserOptions: { ecmaVersion: 2020 },
errors: [{ messageId: "unexpectedCall" }]
}
]
});

0 comments on commit d0c2f82

Please sign in to comment.