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 | /** @type {import('../shared/types').Rule} */
|
---|
12 | module.exports = {
|
---|
13 | meta: {
|
---|
14 | type: "suggestion",
|
---|
15 |
|
---|
16 | docs: {
|
---|
17 | description: "Disallow negated conditions",
|
---|
18 | recommended: false,
|
---|
19 | url: "https://eslint.org/docs/latest/rules/no-negated-condition"
|
---|
20 | },
|
---|
21 |
|
---|
22 | schema: [],
|
---|
23 |
|
---|
24 | messages: {
|
---|
25 | unexpectedNegated: "Unexpected negated condition."
|
---|
26 | }
|
---|
27 | },
|
---|
28 |
|
---|
29 | create(context) {
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Determines if a given node is an if-else without a condition on the else
|
---|
33 | * @param {ASTNode} node The node to check.
|
---|
34 | * @returns {boolean} True if the node has an else without an if.
|
---|
35 | * @private
|
---|
36 | */
|
---|
37 | function hasElseWithoutCondition(node) {
|
---|
38 | return node.alternate && node.alternate.type !== "IfStatement";
|
---|
39 | }
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Determines if a given node is a negated unary expression
|
---|
43 | * @param {Object} test The test object to check.
|
---|
44 | * @returns {boolean} True if the node is a negated unary expression.
|
---|
45 | * @private
|
---|
46 | */
|
---|
47 | function isNegatedUnaryExpression(test) {
|
---|
48 | return test.type === "UnaryExpression" && test.operator === "!";
|
---|
49 | }
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Determines if a given node is a negated binary expression
|
---|
53 | * @param {Test} test The test to check.
|
---|
54 | * @returns {boolean} True if the node is a negated binary expression.
|
---|
55 | * @private
|
---|
56 | */
|
---|
57 | function isNegatedBinaryExpression(test) {
|
---|
58 | return test.type === "BinaryExpression" &&
|
---|
59 | (test.operator === "!=" || test.operator === "!==");
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Determines if a given node has a negated if expression
|
---|
64 | * @param {ASTNode} node The node to check.
|
---|
65 | * @returns {boolean} True if the node has a negated if expression.
|
---|
66 | * @private
|
---|
67 | */
|
---|
68 | function isNegatedIf(node) {
|
---|
69 | return isNegatedUnaryExpression(node.test) || isNegatedBinaryExpression(node.test);
|
---|
70 | }
|
---|
71 |
|
---|
72 | return {
|
---|
73 | IfStatement(node) {
|
---|
74 | if (!hasElseWithoutCondition(node)) {
|
---|
75 | return;
|
---|
76 | }
|
---|
77 |
|
---|
78 | if (isNegatedIf(node)) {
|
---|
79 | context.report({
|
---|
80 | node,
|
---|
81 | messageId: "unexpectedNegated"
|
---|
82 | });
|
---|
83 | }
|
---|
84 | },
|
---|
85 | ConditionalExpression(node) {
|
---|
86 | if (isNegatedIf(node)) {
|
---|
87 | context.report({
|
---|
88 | node,
|
---|
89 | messageId: "unexpectedNegated"
|
---|
90 | });
|
---|
91 | }
|
---|
92 | }
|
---|
93 | };
|
---|
94 | }
|
---|
95 | };
|
---|