2 * @fileoverview Rule to flag unnecessary double negation in Boolean contexts
3 * @author Brandon Mills
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const astUtils = require("./utils/ast-utils");
14 //------------------------------------------------------------------------------
16 //------------------------------------------------------------------------------
23 description: "disallow unnecessary boolean casts",
24 category: "Possible Errors",
26 url: "https://eslint.org/docs/rules/no-extra-boolean-cast"
33 unexpectedCall: "Redundant Boolean call.",
34 unexpectedNegation: "Redundant double negation."
39 const sourceCode = context.getSourceCode();
41 // Node types which have a test which will coerce values to booleans.
42 const BOOLEAN_NODE_TYPES = [
46 "ConditionalExpression",
51 * Check if a node is in a context where its value would be coerced to a boolean at runtime.
52 * @param {ASTNode} node The node
53 * @param {ASTNode} parent Its parent
54 * @returns {boolean} If it is in a boolean context
56 function isInBooleanContext(node, parent) {
58 (BOOLEAN_NODE_TYPES.indexOf(parent.type) !== -1 &&
59 node === parent.test) ||
62 (parent.type === "UnaryExpression" &&
63 parent.operator === "!")
68 * Check if a node has comments inside.
69 * @param {ASTNode} node The node to check.
70 * @returns {boolean} `true` if it has comments inside.
72 function hasCommentsInside(node) {
73 return Boolean(sourceCode.getCommentsInside(node).length);
77 UnaryExpression(node) {
78 const ancestors = context.getAncestors(),
79 parent = ancestors.pop(),
80 grandparent = ancestors.pop();
82 // Exit early if it's guaranteed not to match
83 if (node.operator !== "!" ||
84 parent.type !== "UnaryExpression" ||
85 parent.operator !== "!") {
89 if (isInBooleanContext(parent, grandparent) ||
91 // Boolean(<bool>) and new Boolean(<bool>)
92 ((grandparent.type === "CallExpression" || grandparent.type === "NewExpression") &&
93 grandparent.callee.type === "Identifier" &&
94 grandparent.callee.name === "Boolean")
98 messageId: "unexpectedNegation",
100 if (hasCommentsInside(parent)) {
105 const tokenBefore = sourceCode.getTokenBefore(parent);
106 const firstReplacementToken = sourceCode.getFirstToken(node.argument);
108 if (tokenBefore && tokenBefore.range[1] === parent.range[0] &&
109 !astUtils.canTokensBeAdjacent(tokenBefore, firstReplacementToken)) {
113 return fixer.replaceText(parent, prefix + sourceCode.getText(node.argument));
118 CallExpression(node) {
119 const parent = node.parent;
121 if (node.callee.type !== "Identifier" || node.callee.name !== "Boolean") {
125 if (isInBooleanContext(node, parent)) {
128 messageId: "unexpectedCall",
130 if (!node.arguments.length) {
131 if (parent.type === "UnaryExpression" && parent.operator === "!") {
133 // !Boolean() -> true
135 if (hasCommentsInside(parent)) {
139 const replacement = "true";
141 const tokenBefore = sourceCode.getTokenBefore(parent);
143 if (tokenBefore && tokenBefore.range[1] === parent.range[0] &&
144 !astUtils.canTokensBeAdjacent(tokenBefore, replacement)) {
148 return fixer.replaceText(parent, prefix + replacement);
151 // Boolean() -> false
152 if (hasCommentsInside(node)) {
155 return fixer.replaceText(node, "false");
158 if (node.arguments.length > 1 || node.arguments[0].type === "SpreadElement" ||
159 hasCommentsInside(node)) {
163 const argument = node.arguments[0];
165 if (astUtils.getPrecedence(argument) < astUtils.getPrecedence(node.parent)) {
166 return fixer.replaceText(node, `(${sourceCode.getText(argument)})`);
168 return fixer.replaceText(node, sourceCode.getText(argument));