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