]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/no-negated-condition.js
b5cbadca50f908dcd0541ae71c13b9d557e55071
2 * @fileoverview Rule to disallow a negated condition
3 * @author Alberto RodrÃguez
7 //------------------------------------------------------------------------------
9 //------------------------------------------------------------------------------
16 description
: "disallow negated conditions",
18 url
: "https://eslint.org/docs/rules/no-negated-condition"
24 unexpectedNegated
: "Unexpected negated condition."
31 * Determines if a given node is an if-else without a condition on the else
32 * @param {ASTNode} node The node to check.
33 * @returns {boolean} True if the node has an else without an if.
36 function hasElseWithoutCondition(node
) {
37 return node
.alternate
&& node
.alternate
.type
!== "IfStatement";
41 * Determines if a given node is a negated unary expression
42 * @param {Object} test The test object to check.
43 * @returns {boolean} True if the node is a negated unary expression.
46 function isNegatedUnaryExpression(test
) {
47 return test
.type
=== "UnaryExpression" && test
.operator
=== "!";
51 * Determines if a given node is a negated binary expression
52 * @param {Test} test The test to check.
53 * @returns {boolean} True if the node is a negated binary expression.
56 function isNegatedBinaryExpression(test
) {
57 return test
.type
=== "BinaryExpression" &&
58 (test
.operator
=== "!=" || test
.operator
=== "!==");
62 * Determines if a given node has a negated if expression
63 * @param {ASTNode} node The node to check.
64 * @returns {boolean} True if the node has a negated if expression.
67 function isNegatedIf(node
) {
68 return isNegatedUnaryExpression(node
.test
) || isNegatedBinaryExpression(node
.test
);
73 if (!hasElseWithoutCondition(node
)) {
77 if (isNegatedIf(node
)) {
80 messageId
: "unexpectedNegated"
84 ConditionalExpression(node
) {
85 if (isNegatedIf(node
)) {
88 messageId
: "unexpectedNegated"