1 | /**
|
---|
2 | * @fileoverview Rule to disallow negating the left operand of relational operators
|
---|
3 | * @author Toru Nagashima
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | //------------------------------------------------------------------------------
|
---|
9 | // Requirements
|
---|
10 | //------------------------------------------------------------------------------
|
---|
11 |
|
---|
12 | const astUtils = require("./utils/ast-utils");
|
---|
13 |
|
---|
14 | //------------------------------------------------------------------------------
|
---|
15 | // Helpers
|
---|
16 | //------------------------------------------------------------------------------
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * Checks whether the given operator is `in` or `instanceof`
|
---|
20 | * @param {string} op The operator type to check.
|
---|
21 | * @returns {boolean} `true` if the operator is `in` or `instanceof`
|
---|
22 | */
|
---|
23 | function isInOrInstanceOfOperator(op) {
|
---|
24 | return op === "in" || op === "instanceof";
|
---|
25 | }
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Checks whether the given operator is an ordering relational operator or not.
|
---|
29 | * @param {string} op The operator type to check.
|
---|
30 | * @returns {boolean} `true` if the operator is an ordering relational operator.
|
---|
31 | */
|
---|
32 | function isOrderingRelationalOperator(op) {
|
---|
33 | return op === "<" || op === ">" || op === ">=" || op === "<=";
|
---|
34 | }
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Checks whether the given node is a logical negation expression or not.
|
---|
38 | * @param {ASTNode} node The node to check.
|
---|
39 | * @returns {boolean} `true` if the node is a logical negation expression.
|
---|
40 | */
|
---|
41 | function isNegation(node) {
|
---|
42 | return node.type === "UnaryExpression" && node.operator === "!";
|
---|
43 | }
|
---|
44 |
|
---|
45 | //------------------------------------------------------------------------------
|
---|
46 | // Rule Definition
|
---|
47 | //------------------------------------------------------------------------------
|
---|
48 |
|
---|
49 | /** @type {import('../shared/types').Rule} */
|
---|
50 | module.exports = {
|
---|
51 | meta: {
|
---|
52 | type: "problem",
|
---|
53 |
|
---|
54 | docs: {
|
---|
55 | description: "Disallow negating the left operand of relational operators",
|
---|
56 | recommended: true,
|
---|
57 | url: "https://eslint.org/docs/latest/rules/no-unsafe-negation"
|
---|
58 | },
|
---|
59 |
|
---|
60 | hasSuggestions: true,
|
---|
61 |
|
---|
62 | schema: [
|
---|
63 | {
|
---|
64 | type: "object",
|
---|
65 | properties: {
|
---|
66 | enforceForOrderingRelations: {
|
---|
67 | type: "boolean",
|
---|
68 | default: false
|
---|
69 | }
|
---|
70 | },
|
---|
71 | additionalProperties: false
|
---|
72 | }
|
---|
73 | ],
|
---|
74 |
|
---|
75 | fixable: null,
|
---|
76 |
|
---|
77 | messages: {
|
---|
78 | unexpected: "Unexpected negating the left operand of '{{operator}}' operator.",
|
---|
79 | suggestNegatedExpression: "Negate '{{operator}}' expression instead of its left operand. This changes the current behavior.",
|
---|
80 | suggestParenthesisedNegation: "Wrap negation in '()' to make the intention explicit. This preserves the current behavior."
|
---|
81 | }
|
---|
82 | },
|
---|
83 |
|
---|
84 | create(context) {
|
---|
85 | const sourceCode = context.sourceCode;
|
---|
86 | const options = context.options[0] || {};
|
---|
87 | const enforceForOrderingRelations = options.enforceForOrderingRelations === true;
|
---|
88 |
|
---|
89 | return {
|
---|
90 | BinaryExpression(node) {
|
---|
91 | const operator = node.operator;
|
---|
92 | const orderingRelationRuleApplies = enforceForOrderingRelations && isOrderingRelationalOperator(operator);
|
---|
93 |
|
---|
94 | if (
|
---|
95 | (isInOrInstanceOfOperator(operator) || orderingRelationRuleApplies) &&
|
---|
96 | isNegation(node.left) &&
|
---|
97 | !astUtils.isParenthesised(sourceCode, node.left)
|
---|
98 | ) {
|
---|
99 | context.report({
|
---|
100 | node,
|
---|
101 | loc: node.left.loc,
|
---|
102 | messageId: "unexpected",
|
---|
103 | data: { operator },
|
---|
104 | suggest: [
|
---|
105 | {
|
---|
106 | messageId: "suggestNegatedExpression",
|
---|
107 | data: { operator },
|
---|
108 | fix(fixer) {
|
---|
109 | const negationToken = sourceCode.getFirstToken(node.left);
|
---|
110 | const fixRange = [negationToken.range[1], node.range[1]];
|
---|
111 | const text = sourceCode.text.slice(fixRange[0], fixRange[1]);
|
---|
112 |
|
---|
113 | return fixer.replaceTextRange(fixRange, `(${text})`);
|
---|
114 | }
|
---|
115 | },
|
---|
116 | {
|
---|
117 | messageId: "suggestParenthesisedNegation",
|
---|
118 | fix(fixer) {
|
---|
119 | return fixer.replaceText(node.left, `(${sourceCode.getText(node.left)})`);
|
---|
120 | }
|
---|
121 | }
|
---|
122 | ]
|
---|
123 | });
|
---|
124 | }
|
---|
125 | }
|
---|
126 | };
|
---|
127 | }
|
---|
128 | };
|
---|