no-mixed-operators.js 6.8 KB

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