Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 1 | /** |
| 2 | * @fileoverview Rule to flag unnecessary double negation in Boolean contexts |
| 3 | * @author Brandon Mills |
| 4 | */ |
| 5 | |
| 6 | "use strict"; |
| 7 | |
| 8 | //------------------------------------------------------------------------------ |
| 9 | // Requirements |
| 10 | //------------------------------------------------------------------------------ |
| 11 | |
| 12 | const astUtils = require("./utils/ast-utils"); |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 13 | const eslintUtils = require("eslint-utils"); |
| 14 | |
| 15 | const precedence = astUtils.getPrecedence; |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 16 | |
| 17 | //------------------------------------------------------------------------------ |
| 18 | // Rule Definition |
| 19 | //------------------------------------------------------------------------------ |
| 20 | |
Tim van der Lippe | 0ceb465 | 2022-01-06 14:23:36 +0100 | [diff] [blame^] | 21 | /** @type {import('../shared/types').Rule} */ |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 22 | module.exports = { |
| 23 | meta: { |
| 24 | type: "suggestion", |
| 25 | |
| 26 | docs: { |
| 27 | description: "disallow unnecessary boolean casts", |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 28 | recommended: true, |
| 29 | url: "https://eslint.org/docs/rules/no-extra-boolean-cast" |
| 30 | }, |
| 31 | |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 32 | schema: [{ |
| 33 | type: "object", |
| 34 | properties: { |
| 35 | enforceForLogicalOperands: { |
| 36 | type: "boolean", |
| 37 | default: false |
| 38 | } |
| 39 | }, |
| 40 | additionalProperties: false |
| 41 | }], |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 42 | fixable: "code", |
| 43 | |
| 44 | messages: { |
| 45 | unexpectedCall: "Redundant Boolean call.", |
| 46 | unexpectedNegation: "Redundant double negation." |
| 47 | } |
| 48 | }, |
| 49 | |
| 50 | create(context) { |
| 51 | const sourceCode = context.getSourceCode(); |
| 52 | |
| 53 | // Node types which have a test which will coerce values to booleans. |
| 54 | const BOOLEAN_NODE_TYPES = [ |
| 55 | "IfStatement", |
| 56 | "DoWhileStatement", |
| 57 | "WhileStatement", |
| 58 | "ConditionalExpression", |
| 59 | "ForStatement" |
| 60 | ]; |
| 61 | |
| 62 | /** |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 63 | * Check if a node is a Boolean function or constructor. |
| 64 | * @param {ASTNode} node the node |
| 65 | * @returns {boolean} If the node is Boolean function or constructor |
| 66 | */ |
| 67 | function isBooleanFunctionOrConstructorCall(node) { |
| 68 | |
| 69 | // Boolean(<bool>) and new Boolean(<bool>) |
| 70 | return (node.type === "CallExpression" || node.type === "NewExpression") && |
| 71 | node.callee.type === "Identifier" && |
| 72 | node.callee.name === "Boolean"; |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Checks whether the node is a logical expression and that the option is enabled |
| 77 | * @param {ASTNode} node the node |
| 78 | * @returns {boolean} if the node is a logical expression and option is enabled |
| 79 | */ |
| 80 | function isLogicalContext(node) { |
| 81 | return node.type === "LogicalExpression" && |
| 82 | (node.operator === "||" || node.operator === "&&") && |
| 83 | (context.options.length && context.options[0].enforceForLogicalOperands === true); |
| 84 | |
| 85 | } |
| 86 | |
| 87 | |
| 88 | /** |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 89 | * Check if a node is in a context where its value would be coerced to a boolean at runtime. |
Tim van der Lippe | c8f6ffd | 2020-04-06 13:42:00 +0100 | [diff] [blame] | 90 | * @param {ASTNode} node The node |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 91 | * @returns {boolean} If it is in a boolean context |
| 92 | */ |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 93 | function isInBooleanContext(node) { |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 94 | return ( |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 95 | (isBooleanFunctionOrConstructorCall(node.parent) && |
| 96 | node === node.parent.arguments[0]) || |
| 97 | |
| 98 | (BOOLEAN_NODE_TYPES.indexOf(node.parent.type) !== -1 && |
| 99 | node === node.parent.test) || |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 100 | |
| 101 | // !<bool> |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 102 | (node.parent.type === "UnaryExpression" && |
| 103 | node.parent.operator === "!") |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 104 | ); |
| 105 | } |
| 106 | |
Tim van der Lippe | c8f6ffd | 2020-04-06 13:42:00 +0100 | [diff] [blame] | 107 | /** |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 108 | * Checks whether the node is a context that should report an error |
| 109 | * Acts recursively if it is in a logical context |
| 110 | * @param {ASTNode} node the node |
| 111 | * @returns {boolean} If the node is in one of the flagged contexts |
| 112 | */ |
| 113 | function isInFlaggedContext(node) { |
| 114 | if (node.parent.type === "ChainExpression") { |
| 115 | return isInFlaggedContext(node.parent); |
| 116 | } |
| 117 | |
| 118 | return isInBooleanContext(node) || |
| 119 | (isLogicalContext(node.parent) && |
| 120 | |
| 121 | // For nested logical statements |
| 122 | isInFlaggedContext(node.parent) |
| 123 | ); |
| 124 | } |
| 125 | |
| 126 | |
| 127 | /** |
Tim van der Lippe | c8f6ffd | 2020-04-06 13:42:00 +0100 | [diff] [blame] | 128 | * Check if a node has comments inside. |
| 129 | * @param {ASTNode} node The node to check. |
| 130 | * @returns {boolean} `true` if it has comments inside. |
| 131 | */ |
| 132 | function hasCommentsInside(node) { |
| 133 | return Boolean(sourceCode.getCommentsInside(node).length); |
| 134 | } |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 135 | |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 136 | /** |
| 137 | * Checks if the given node is wrapped in grouping parentheses. Parentheses for constructs such as if() don't count. |
| 138 | * @param {ASTNode} node The node to check. |
| 139 | * @returns {boolean} `true` if the node is parenthesized. |
| 140 | * @private |
| 141 | */ |
| 142 | function isParenthesized(node) { |
| 143 | return eslintUtils.isParenthesized(1, node, sourceCode); |
| 144 | } |
| 145 | |
| 146 | /** |
| 147 | * Determines whether the given node needs to be parenthesized when replacing the previous node. |
| 148 | * It assumes that `previousNode` is the node to be reported by this rule, so it has a limited list |
| 149 | * of possible parent node types. By the same assumption, the node's role in a particular parent is already known. |
| 150 | * For example, if the parent is `ConditionalExpression`, `previousNode` must be its `test` child. |
| 151 | * @param {ASTNode} previousNode Previous node. |
| 152 | * @param {ASTNode} node The node to check. |
Tim van der Lippe | 0fb4780 | 2021-11-08 16:23:10 +0000 | [diff] [blame] | 153 | * @throws {Error} (Unreachable.) |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 154 | * @returns {boolean} `true` if the node needs to be parenthesized. |
| 155 | */ |
| 156 | function needsParens(previousNode, node) { |
| 157 | if (previousNode.parent.type === "ChainExpression") { |
| 158 | return needsParens(previousNode.parent, node); |
| 159 | } |
| 160 | if (isParenthesized(previousNode)) { |
| 161 | |
| 162 | // parentheses around the previous node will stay, so there is no need for an additional pair |
| 163 | return false; |
| 164 | } |
| 165 | |
| 166 | // parent of the previous node will become parent of the replacement node |
| 167 | const parent = previousNode.parent; |
| 168 | |
| 169 | switch (parent.type) { |
| 170 | case "CallExpression": |
| 171 | case "NewExpression": |
| 172 | return node.type === "SequenceExpression"; |
| 173 | case "IfStatement": |
| 174 | case "DoWhileStatement": |
| 175 | case "WhileStatement": |
| 176 | case "ForStatement": |
| 177 | return false; |
| 178 | case "ConditionalExpression": |
| 179 | return precedence(node) <= precedence(parent); |
| 180 | case "UnaryExpression": |
| 181 | return precedence(node) < precedence(parent); |
| 182 | case "LogicalExpression": |
| 183 | if (astUtils.isMixedLogicalAndCoalesceExpressions(node, parent)) { |
| 184 | return true; |
| 185 | } |
| 186 | if (previousNode === parent.left) { |
| 187 | return precedence(node) < precedence(parent); |
| 188 | } |
| 189 | return precedence(node) <= precedence(parent); |
| 190 | |
| 191 | /* istanbul ignore next */ |
| 192 | default: |
| 193 | throw new Error(`Unexpected parent type: ${parent.type}`); |
| 194 | } |
| 195 | } |
| 196 | |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 197 | return { |
| 198 | UnaryExpression(node) { |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 199 | const parent = node.parent; |
| 200 | |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 201 | |
| 202 | // Exit early if it's guaranteed not to match |
| 203 | if (node.operator !== "!" || |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 204 | parent.type !== "UnaryExpression" || |
| 205 | parent.operator !== "!") { |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 206 | return; |
| 207 | } |
| 208 | |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 209 | |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 210 | if (isInFlaggedContext(parent)) { |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 211 | context.report({ |
Tim van der Lippe | c8f6ffd | 2020-04-06 13:42:00 +0100 | [diff] [blame] | 212 | node: parent, |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 213 | messageId: "unexpectedNegation", |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 214 | fix(fixer) { |
Tim van der Lippe | c8f6ffd | 2020-04-06 13:42:00 +0100 | [diff] [blame] | 215 | if (hasCommentsInside(parent)) { |
| 216 | return null; |
| 217 | } |
| 218 | |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 219 | if (needsParens(parent, node.argument)) { |
| 220 | return fixer.replaceText(parent, `(${sourceCode.getText(node.argument)})`); |
| 221 | } |
| 222 | |
Tim van der Lippe | c8f6ffd | 2020-04-06 13:42:00 +0100 | [diff] [blame] | 223 | let prefix = ""; |
| 224 | const tokenBefore = sourceCode.getTokenBefore(parent); |
| 225 | const firstReplacementToken = sourceCode.getFirstToken(node.argument); |
| 226 | |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 227 | if ( |
| 228 | tokenBefore && |
| 229 | tokenBefore.range[1] === parent.range[0] && |
| 230 | !astUtils.canTokensBeAdjacent(tokenBefore, firstReplacementToken) |
| 231 | ) { |
Tim van der Lippe | c8f6ffd | 2020-04-06 13:42:00 +0100 | [diff] [blame] | 232 | prefix = " "; |
| 233 | } |
| 234 | |
| 235 | return fixer.replaceText(parent, prefix + sourceCode.getText(node.argument)); |
| 236 | } |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 237 | }); |
| 238 | } |
| 239 | }, |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 240 | |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 241 | CallExpression(node) { |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 242 | if (node.callee.type !== "Identifier" || node.callee.name !== "Boolean") { |
| 243 | return; |
| 244 | } |
| 245 | |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 246 | if (isInFlaggedContext(node)) { |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 247 | context.report({ |
| 248 | node, |
| 249 | messageId: "unexpectedCall", |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 250 | fix(fixer) { |
| 251 | const parent = node.parent; |
| 252 | |
| 253 | if (node.arguments.length === 0) { |
Tim van der Lippe | c8f6ffd | 2020-04-06 13:42:00 +0100 | [diff] [blame] | 254 | if (parent.type === "UnaryExpression" && parent.operator === "!") { |
| 255 | |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 256 | /* |
| 257 | * !Boolean() -> true |
| 258 | */ |
Tim van der Lippe | c8f6ffd | 2020-04-06 13:42:00 +0100 | [diff] [blame] | 259 | |
| 260 | if (hasCommentsInside(parent)) { |
| 261 | return null; |
| 262 | } |
| 263 | |
| 264 | const replacement = "true"; |
| 265 | let prefix = ""; |
| 266 | const tokenBefore = sourceCode.getTokenBefore(parent); |
| 267 | |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 268 | if ( |
| 269 | tokenBefore && |
| 270 | tokenBefore.range[1] === parent.range[0] && |
| 271 | !astUtils.canTokensBeAdjacent(tokenBefore, replacement) |
| 272 | ) { |
Tim van der Lippe | c8f6ffd | 2020-04-06 13:42:00 +0100 | [diff] [blame] | 273 | prefix = " "; |
| 274 | } |
| 275 | |
| 276 | return fixer.replaceText(parent, prefix + replacement); |
| 277 | } |
| 278 | |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 279 | /* |
| 280 | * Boolean() -> false |
| 281 | */ |
| 282 | |
Tim van der Lippe | c8f6ffd | 2020-04-06 13:42:00 +0100 | [diff] [blame] | 283 | if (hasCommentsInside(node)) { |
| 284 | return null; |
| 285 | } |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 286 | |
Tim van der Lippe | c8f6ffd | 2020-04-06 13:42:00 +0100 | [diff] [blame] | 287 | return fixer.replaceText(node, "false"); |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 288 | } |
| 289 | |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 290 | if (node.arguments.length === 1) { |
| 291 | const argument = node.arguments[0]; |
| 292 | |
| 293 | if (argument.type === "SpreadElement" || hasCommentsInside(node)) { |
| 294 | return null; |
| 295 | } |
| 296 | |
| 297 | /* |
| 298 | * Boolean(expression) -> expression |
| 299 | */ |
| 300 | |
| 301 | if (needsParens(node, argument)) { |
| 302 | return fixer.replaceText(node, `(${sourceCode.getText(argument)})`); |
| 303 | } |
| 304 | |
| 305 | return fixer.replaceText(node, sourceCode.getText(argument)); |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 306 | } |
| 307 | |
Tim van der Lippe | 16aca39 | 2020-11-13 11:37:13 +0000 | [diff] [blame] | 308 | // two or more arguments |
| 309 | return null; |
Yang Guo | 4fd355c | 2019-09-19 10:59:03 +0200 | [diff] [blame] | 310 | } |
| 311 | }); |
| 312 | } |
| 313 | } |
| 314 | }; |
| 315 | |
| 316 | } |
| 317 | }; |