1 | /**
|
---|
2 | * @fileoverview Rule to disallow mixed binary operators.
|
---|
3 | * @author Toru Nagashima
|
---|
4 | * @deprecated in ESLint v8.53.0
|
---|
5 | */
|
---|
6 |
|
---|
7 | "use strict";
|
---|
8 |
|
---|
9 | //------------------------------------------------------------------------------
|
---|
10 | // Requirements
|
---|
11 | //------------------------------------------------------------------------------
|
---|
12 |
|
---|
13 | const astUtils = require("./utils/ast-utils.js");
|
---|
14 |
|
---|
15 | //------------------------------------------------------------------------------
|
---|
16 | // Helpers
|
---|
17 | //------------------------------------------------------------------------------
|
---|
18 |
|
---|
19 | const ARITHMETIC_OPERATORS = ["+", "-", "*", "/", "%", "**"];
|
---|
20 | const BITWISE_OPERATORS = ["&", "|", "^", "~", "<<", ">>", ">>>"];
|
---|
21 | const COMPARISON_OPERATORS = ["==", "!=", "===", "!==", ">", ">=", "<", "<="];
|
---|
22 | const LOGICAL_OPERATORS = ["&&", "||"];
|
---|
23 | const RELATIONAL_OPERATORS = ["in", "instanceof"];
|
---|
24 | const TERNARY_OPERATOR = ["?:"];
|
---|
25 | const COALESCE_OPERATOR = ["??"];
|
---|
26 | const ALL_OPERATORS = [].concat(
|
---|
27 | ARITHMETIC_OPERATORS,
|
---|
28 | BITWISE_OPERATORS,
|
---|
29 | COMPARISON_OPERATORS,
|
---|
30 | LOGICAL_OPERATORS,
|
---|
31 | RELATIONAL_OPERATORS,
|
---|
32 | TERNARY_OPERATOR,
|
---|
33 | COALESCE_OPERATOR
|
---|
34 | );
|
---|
35 | const DEFAULT_GROUPS = [
|
---|
36 | ARITHMETIC_OPERATORS,
|
---|
37 | BITWISE_OPERATORS,
|
---|
38 | COMPARISON_OPERATORS,
|
---|
39 | LOGICAL_OPERATORS,
|
---|
40 | RELATIONAL_OPERATORS
|
---|
41 | ];
|
---|
42 | const TARGET_NODE_TYPE = /^(?:Binary|Logical|Conditional)Expression$/u;
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Normalizes options.
|
---|
46 | * @param {Object|undefined} options A options object to normalize.
|
---|
47 | * @returns {Object} Normalized option object.
|
---|
48 | */
|
---|
49 | function normalizeOptions(options = {}) {
|
---|
50 | const hasGroups = options.groups && options.groups.length > 0;
|
---|
51 | const groups = hasGroups ? options.groups : DEFAULT_GROUPS;
|
---|
52 | const allowSamePrecedence = options.allowSamePrecedence !== false;
|
---|
53 |
|
---|
54 | return {
|
---|
55 | groups,
|
---|
56 | allowSamePrecedence
|
---|
57 | };
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Checks whether any group which includes both given operator exists or not.
|
---|
62 | * @param {Array<string[]>} groups A list of groups to check.
|
---|
63 | * @param {string} left An operator.
|
---|
64 | * @param {string} right Another operator.
|
---|
65 | * @returns {boolean} `true` if such group existed.
|
---|
66 | */
|
---|
67 | function includesBothInAGroup(groups, left, right) {
|
---|
68 | return groups.some(group => group.includes(left) && group.includes(right));
|
---|
69 | }
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * Checks whether the given node is a conditional expression and returns the test node else the left node.
|
---|
73 | * @param {ASTNode} node A node which can be a BinaryExpression or a LogicalExpression node.
|
---|
74 | * This parent node can be BinaryExpression, LogicalExpression
|
---|
75 | * , or a ConditionalExpression node
|
---|
76 | * @returns {ASTNode} node the appropriate node(left or test).
|
---|
77 | */
|
---|
78 | function getChildNode(node) {
|
---|
79 | return node.type === "ConditionalExpression" ? node.test : node.left;
|
---|
80 | }
|
---|
81 |
|
---|
82 | //------------------------------------------------------------------------------
|
---|
83 | // Rule Definition
|
---|
84 | //------------------------------------------------------------------------------
|
---|
85 |
|
---|
86 | /** @type {import('../shared/types').Rule} */
|
---|
87 | module.exports = {
|
---|
88 | meta: {
|
---|
89 | deprecated: true,
|
---|
90 | replacedBy: [],
|
---|
91 | type: "suggestion",
|
---|
92 |
|
---|
93 | docs: {
|
---|
94 | description: "Disallow mixed binary operators",
|
---|
95 | recommended: false,
|
---|
96 | url: "https://eslint.org/docs/latest/rules/no-mixed-operators"
|
---|
97 | },
|
---|
98 |
|
---|
99 | schema: [
|
---|
100 | {
|
---|
101 | type: "object",
|
---|
102 | properties: {
|
---|
103 | groups: {
|
---|
104 | type: "array",
|
---|
105 | items: {
|
---|
106 | type: "array",
|
---|
107 | items: { enum: ALL_OPERATORS },
|
---|
108 | minItems: 2,
|
---|
109 | uniqueItems: true
|
---|
110 | },
|
---|
111 | uniqueItems: true
|
---|
112 | },
|
---|
113 | allowSamePrecedence: {
|
---|
114 | type: "boolean",
|
---|
115 | default: true
|
---|
116 | }
|
---|
117 | },
|
---|
118 | additionalProperties: false
|
---|
119 | }
|
---|
120 | ],
|
---|
121 |
|
---|
122 | messages: {
|
---|
123 | unexpectedMixedOperator: "Unexpected mix of '{{leftOperator}}' and '{{rightOperator}}'. Use parentheses to clarify the intended order of operations."
|
---|
124 | }
|
---|
125 | },
|
---|
126 |
|
---|
127 | create(context) {
|
---|
128 | const sourceCode = context.sourceCode;
|
---|
129 | const options = normalizeOptions(context.options[0]);
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * Checks whether a given node should be ignored by options or not.
|
---|
133 | * @param {ASTNode} node A node to check. This is a BinaryExpression
|
---|
134 | * node or a LogicalExpression node. This parent node is one of
|
---|
135 | * them, too.
|
---|
136 | * @returns {boolean} `true` if the node should be ignored.
|
---|
137 | */
|
---|
138 | function shouldIgnore(node) {
|
---|
139 | const a = node;
|
---|
140 | const b = node.parent;
|
---|
141 |
|
---|
142 | return (
|
---|
143 | !includesBothInAGroup(options.groups, a.operator, b.type === "ConditionalExpression" ? "?:" : b.operator) ||
|
---|
144 | (
|
---|
145 | options.allowSamePrecedence &&
|
---|
146 | astUtils.getPrecedence(a) === astUtils.getPrecedence(b)
|
---|
147 | )
|
---|
148 | );
|
---|
149 | }
|
---|
150 |
|
---|
151 | /**
|
---|
152 | * Checks whether the operator of a given node is mixed with parent
|
---|
153 | * node's operator or not.
|
---|
154 | * @param {ASTNode} node A node to check. This is a BinaryExpression
|
---|
155 | * node or a LogicalExpression node. This parent node is one of
|
---|
156 | * them, too.
|
---|
157 | * @returns {boolean} `true` if the node was mixed.
|
---|
158 | */
|
---|
159 | function isMixedWithParent(node) {
|
---|
160 |
|
---|
161 | return (
|
---|
162 | node.operator !== node.parent.operator &&
|
---|
163 | !astUtils.isParenthesised(sourceCode, node)
|
---|
164 | );
|
---|
165 | }
|
---|
166 |
|
---|
167 | /**
|
---|
168 | * Gets the operator token of a given node.
|
---|
169 | * @param {ASTNode} node A node to check. This is a BinaryExpression
|
---|
170 | * node or a LogicalExpression node.
|
---|
171 | * @returns {Token} The operator token of the node.
|
---|
172 | */
|
---|
173 | function getOperatorToken(node) {
|
---|
174 | return sourceCode.getTokenAfter(getChildNode(node), astUtils.isNotClosingParenToken);
|
---|
175 | }
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * Reports both the operator of a given node and the operator of the
|
---|
179 | * parent node.
|
---|
180 | * @param {ASTNode} node A node to check. This is a BinaryExpression
|
---|
181 | * node or a LogicalExpression node. This parent node is one of
|
---|
182 | * them, too.
|
---|
183 | * @returns {void}
|
---|
184 | */
|
---|
185 | function reportBothOperators(node) {
|
---|
186 | const parent = node.parent;
|
---|
187 | const left = (getChildNode(parent) === node) ? node : parent;
|
---|
188 | const right = (getChildNode(parent) !== node) ? node : parent;
|
---|
189 | const data = {
|
---|
190 | leftOperator: left.operator || "?:",
|
---|
191 | rightOperator: right.operator || "?:"
|
---|
192 | };
|
---|
193 |
|
---|
194 | context.report({
|
---|
195 | node: left,
|
---|
196 | loc: getOperatorToken(left).loc,
|
---|
197 | messageId: "unexpectedMixedOperator",
|
---|
198 | data
|
---|
199 | });
|
---|
200 | context.report({
|
---|
201 | node: right,
|
---|
202 | loc: getOperatorToken(right).loc,
|
---|
203 | messageId: "unexpectedMixedOperator",
|
---|
204 | data
|
---|
205 | });
|
---|
206 | }
|
---|
207 |
|
---|
208 | /**
|
---|
209 | * Checks between the operator of this node and the operator of the
|
---|
210 | * parent node.
|
---|
211 | * @param {ASTNode} node A node to check.
|
---|
212 | * @returns {void}
|
---|
213 | */
|
---|
214 | function check(node) {
|
---|
215 | if (
|
---|
216 | TARGET_NODE_TYPE.test(node.parent.type) &&
|
---|
217 | isMixedWithParent(node) &&
|
---|
218 | !shouldIgnore(node)
|
---|
219 | ) {
|
---|
220 | reportBothOperators(node);
|
---|
221 | }
|
---|
222 | }
|
---|
223 |
|
---|
224 | return {
|
---|
225 | BinaryExpression: check,
|
---|
226 | LogicalExpression: check
|
---|
227 | };
|
---|
228 | }
|
---|
229 | };
|
---|