Actualizacion maquina principal
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / eslint / lib / rules / no-negated-condition.js
1 /**
2  * @fileoverview Rule to disallow a negated condition
3  * @author Alberto Rodríguez
4  */
5 "use strict";
6
7 //------------------------------------------------------------------------------
8 // Rule Definition
9 //------------------------------------------------------------------------------
10
11 module.exports = {
12     meta: {
13         type: "suggestion",
14
15         docs: {
16             description: "disallow negated conditions",
17             category: "Stylistic Issues",
18             recommended: false,
19             url: "https://eslint.org/docs/rules/no-negated-condition"
20         },
21
22         schema: []
23     },
24
25     create(context) {
26
27         /**
28          * Determines if a given node is an if-else without a condition on the else
29          * @param {ASTNode} node The node to check.
30          * @returns {boolean} True if the node has an else without an if.
31          * @private
32          */
33         function hasElseWithoutCondition(node) {
34             return node.alternate && node.alternate.type !== "IfStatement";
35         }
36
37         /**
38          * Determines if a given node is a negated unary expression
39          * @param {Object} test The test object to check.
40          * @returns {boolean} True if the node is a negated unary expression.
41          * @private
42          */
43         function isNegatedUnaryExpression(test) {
44             return test.type === "UnaryExpression" && test.operator === "!";
45         }
46
47         /**
48          * Determines if a given node is a negated binary expression
49          * @param {Test} test The test to check.
50          * @returns {boolean} True if the node is a negated binary expression.
51          * @private
52          */
53         function isNegatedBinaryExpression(test) {
54             return test.type === "BinaryExpression" &&
55                 (test.operator === "!=" || test.operator === "!==");
56         }
57
58         /**
59          * Determines if a given node has a negated if expression
60          * @param {ASTNode} node The node to check.
61          * @returns {boolean} True if the node has a negated if expression.
62          * @private
63          */
64         function isNegatedIf(node) {
65             return isNegatedUnaryExpression(node.test) || isNegatedBinaryExpression(node.test);
66         }
67
68         return {
69             IfStatement(node) {
70                 if (!hasElseWithoutCondition(node)) {
71                     return;
72                 }
73
74                 if (isNegatedIf(node)) {
75                     context.report({ node, message: "Unexpected negated condition." });
76                 }
77             },
78             ConditionalExpression(node) {
79                 if (isNegatedIf(node)) {
80                     context.report({ node, message: "Unexpected negated condition." });
81                 }
82             }
83         };
84     }
85 };