2 * @fileoverview Rule to disallow mixed binary operators.
3 * @author Toru Nagashima
8 //------------------------------------------------------------------------------
10 //------------------------------------------------------------------------------
12 const astUtils = require("./utils/ast-utils.js");
14 //------------------------------------------------------------------------------
16 //------------------------------------------------------------------------------
18 const ARITHMETIC_OPERATORS = ["+", "-", "*", "/", "%", "**"];
19 const BITWISE_OPERATORS = ["&", "|", "^", "~", "<<", ">>", ">>>"];
20 const COMPARISON_OPERATORS = ["==", "!=", "===", "!==", ">", ">=", "<", "<="];
21 const LOGICAL_OPERATORS = ["&&", "||"];
22 const RELATIONAL_OPERATORS = ["in", "instanceof"];
23 const TERNARY_OPERATOR = ["?:"];
24 const ALL_OPERATORS = [].concat(
32 const DEFAULT_GROUPS = [
39 const TARGET_NODE_TYPE = /^(?:Binary|Logical|Conditional)Expression$/u;
43 * @param {Object|undefined} options A options object to normalize.
44 * @returns {Object} Normalized option object.
46 function normalizeOptions(options = {}) {
47 const hasGroups = options.groups && options.groups.length > 0;
48 const groups = hasGroups ? options.groups : DEFAULT_GROUPS;
49 const allowSamePrecedence = options.allowSamePrecedence !== false;
58 * Checks whether any group which includes both given operator exists or not.
59 * @param {Array.<string[]>} groups A list of groups to check.
60 * @param {string} left An operator.
61 * @param {string} right Another operator.
62 * @returns {boolean} `true` if such group existed.
64 function includesBothInAGroup(groups, left, right) {
65 return groups.some(group => group.indexOf(left) !== -1 && group.indexOf(right) !== -1);
69 * Checks whether the given node is a conditional expression and returns the test node else the left node.
70 * @param {ASTNode} node A node which can be a BinaryExpression or a LogicalExpression node.
71 * This parent node can be BinaryExpression, LogicalExpression
72 * , or a ConditionalExpression node
73 * @returns {ASTNode} node the appropriate node(left or test).
75 function getChildNode(node) {
76 return node.type === "ConditionalExpression" ? node.test : node.left;
79 //------------------------------------------------------------------------------
81 //------------------------------------------------------------------------------
88 description: "disallow mixed binary operators",
89 category: "Stylistic Issues",
91 url: "https://eslint.org/docs/rules/no-mixed-operators"
102 items: { enum: ALL_OPERATORS },
108 allowSamePrecedence: {
113 additionalProperties: false
119 const sourceCode = context.getSourceCode();
120 const options = normalizeOptions(context.options[0]);
123 * Checks whether a given node should be ignored by options or not.
124 * @param {ASTNode} node A node to check. This is a BinaryExpression
125 * node or a LogicalExpression node. This parent node is one of
127 * @returns {boolean} `true` if the node should be ignored.
129 function shouldIgnore(node) {
131 const b = node.parent;
134 !includesBothInAGroup(options.groups, a.operator, b.type === "ConditionalExpression" ? "?:" : b.operator) ||
136 options.allowSamePrecedence &&
137 astUtils.getPrecedence(a) === astUtils.getPrecedence(b)
143 * Checks whether the operator of a given node is mixed with parent
144 * node's operator or not.
145 * @param {ASTNode} node A node to check. This is a BinaryExpression
146 * node or a LogicalExpression node. This parent node is one of
148 * @returns {boolean} `true` if the node was mixed.
150 function isMixedWithParent(node) {
153 node.operator !== node.parent.operator &&
154 !astUtils.isParenthesised(sourceCode, node)
159 * Checks whether the operator of a given node is mixed with a
160 * conditional expression.
161 * @param {ASTNode} node A node to check. This is a conditional
163 * @returns {boolean} `true` if the node was mixed.
165 function isMixedWithConditionalParent(node) {
166 return !astUtils.isParenthesised(sourceCode, node) && !astUtils.isParenthesised(sourceCode, node.test);
170 * Gets the operator token of a given node.
171 * @param {ASTNode} node A node to check. This is a BinaryExpression
172 * node or a LogicalExpression node.
173 * @returns {Token} The operator token of the node.
175 function getOperatorToken(node) {
176 return sourceCode.getTokenAfter(getChildNode(node), astUtils.isNotClosingParenToken);
180 * Reports both the operator of a given node and the operator of the
182 * @param {ASTNode} node A node to check. This is a BinaryExpression
183 * node or a LogicalExpression node. This parent node is one of
187 function reportBothOperators(node) {
188 const parent = node.parent;
189 const left = (getChildNode(parent) === node) ? node : parent;
190 const right = (getChildNode(parent) !== node) ? node : parent;
192 "Unexpected mix of '{{leftOperator}}' and '{{rightOperator}}'.";
194 leftOperator: left.operator || "?:",
195 rightOperator: right.operator || "?:"
200 loc: getOperatorToken(left).loc,
206 loc: getOperatorToken(right).loc,
213 * Checks between the operator of this node and the operator of the
215 * @param {ASTNode} node A node to check.
218 function check(node) {
219 if (TARGET_NODE_TYPE.test(node.parent.type)) {
220 if (node.parent.type === "ConditionalExpression" && !shouldIgnore(node) && isMixedWithConditionalParent(node.parent)) {
221 reportBothOperators(node);
223 if (TARGET_NODE_TYPE.test(node.parent.type) &&
224 isMixedWithParent(node) &&
227 reportBothOperators(node);
235 BinaryExpression: check,
236 LogicalExpression: check