no-mixed-operators.js 7.0 KB

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