Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

no-mixed-operators.js 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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$/;
  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 && options.groups && options.groups.length > 0);
  41. const groups = hasGroups ? options.groups : DEFAULT_GROUPS;
  42. const allowSamePrecedence = (options && 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. }
  88. },
  89. additionalProperties: false
  90. }
  91. ]
  92. },
  93. create(context) {
  94. const sourceCode = context.getSourceCode();
  95. const options = normalizeOptions(context.options[0]);
  96. /**
  97. * Checks whether a given node should be ignored by options or not.
  98. *
  99. * @param {ASTNode} node - A node to check. This is a BinaryExpression
  100. * node or a LogicalExpression node. This parent node is one of
  101. * them, too.
  102. * @returns {boolean} `true` if the node should be ignored.
  103. */
  104. function shouldIgnore(node) {
  105. const a = node;
  106. const b = node.parent;
  107. return (
  108. !includesBothInAGroup(options.groups, a.operator, b.operator) ||
  109. (
  110. options.allowSamePrecedence &&
  111. astUtils.getPrecedence(a) === astUtils.getPrecedence(b)
  112. )
  113. );
  114. }
  115. /**
  116. * Checks whether the operator of a given node is mixed with parent
  117. * node's operator or not.
  118. *
  119. * @param {ASTNode} node - A node to check. This is a BinaryExpression
  120. * node or a LogicalExpression node. This parent node is one of
  121. * them, too.
  122. * @returns {boolean} `true` if the node was mixed.
  123. */
  124. function isMixedWithParent(node) {
  125. return (
  126. node.operator !== node.parent.operator &&
  127. !astUtils.isParenthesised(sourceCode, node)
  128. );
  129. }
  130. /**
  131. * Gets the operator token of a given node.
  132. *
  133. * @param {ASTNode} node - A node to check. This is a BinaryExpression
  134. * node or a LogicalExpression node.
  135. * @returns {Token} The operator token of the node.
  136. */
  137. function getOperatorToken(node) {
  138. return sourceCode.getTokenAfter(node.left, astUtils.isNotClosingParenToken);
  139. }
  140. /**
  141. * Reports both the operator of a given node and the operator of the
  142. * parent node.
  143. *
  144. * @param {ASTNode} node - A node to check. This is a BinaryExpression
  145. * node or a LogicalExpression node. This parent node is one of
  146. * them, too.
  147. * @returns {void}
  148. */
  149. function reportBothOperators(node) {
  150. const parent = node.parent;
  151. const left = (parent.left === node) ? node : parent;
  152. const right = (parent.left !== node) ? node : parent;
  153. const message =
  154. "Unexpected mix of '{{leftOperator}}' and '{{rightOperator}}'.";
  155. const data = {
  156. leftOperator: left.operator,
  157. rightOperator: right.operator
  158. };
  159. context.report({
  160. node: left,
  161. loc: getOperatorToken(left).loc.start,
  162. message,
  163. data
  164. });
  165. context.report({
  166. node: right,
  167. loc: getOperatorToken(right).loc.start,
  168. message,
  169. data
  170. });
  171. }
  172. /**
  173. * Checks between the operator of this node and the operator of the
  174. * parent node.
  175. *
  176. * @param {ASTNode} node - A node to check.
  177. * @returns {void}
  178. */
  179. function check(node) {
  180. if (TARGET_NODE_TYPE.test(node.parent.type) &&
  181. isMixedWithParent(node) &&
  182. !shouldIgnore(node)
  183. ) {
  184. reportBothOperators(node);
  185. }
  186. }
  187. return {
  188. BinaryExpression: check,
  189. LogicalExpression: check
  190. };
  191. }
  192. };