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.

space-infix-ops.js 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /**
  2. * @fileoverview Require spaces around infix operators
  3. * @author Michael Ficarra
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. module.exports = {
  10. meta: {
  11. type: "layout",
  12. docs: {
  13. description: "require spacing around infix operators",
  14. category: "Stylistic Issues",
  15. recommended: false,
  16. url: "https://eslint.org/docs/rules/space-infix-ops"
  17. },
  18. fixable: "whitespace",
  19. schema: [
  20. {
  21. type: "object",
  22. properties: {
  23. int32Hint: {
  24. type: "boolean"
  25. }
  26. },
  27. additionalProperties: false
  28. }
  29. ]
  30. },
  31. create(context) {
  32. const int32Hint = context.options[0] ? context.options[0].int32Hint === true : false;
  33. const sourceCode = context.getSourceCode();
  34. /**
  35. * Returns the first token which violates the rule
  36. * @param {ASTNode} left - The left node of the main node
  37. * @param {ASTNode} right - The right node of the main node
  38. * @param {string} op - The operator of the main node
  39. * @returns {Object} The violator token or null
  40. * @private
  41. */
  42. function getFirstNonSpacedToken(left, right, op) {
  43. const operator = sourceCode.getFirstTokenBetween(left, right, token => token.value === op);
  44. const prev = sourceCode.getTokenBefore(operator);
  45. const next = sourceCode.getTokenAfter(operator);
  46. if (!sourceCode.isSpaceBetweenTokens(prev, operator) || !sourceCode.isSpaceBetweenTokens(operator, next)) {
  47. return operator;
  48. }
  49. return null;
  50. }
  51. /**
  52. * Reports an AST node as a rule violation
  53. * @param {ASTNode} mainNode - The node to report
  54. * @param {Object} culpritToken - The token which has a problem
  55. * @returns {void}
  56. * @private
  57. */
  58. function report(mainNode, culpritToken) {
  59. context.report({
  60. node: mainNode,
  61. loc: culpritToken.loc.start,
  62. message: "Infix operators must be spaced.",
  63. fix(fixer) {
  64. const previousToken = sourceCode.getTokenBefore(culpritToken);
  65. const afterToken = sourceCode.getTokenAfter(culpritToken);
  66. let fixString = "";
  67. if (culpritToken.range[0] - previousToken.range[1] === 0) {
  68. fixString = " ";
  69. }
  70. fixString += culpritToken.value;
  71. if (afterToken.range[0] - culpritToken.range[1] === 0) {
  72. fixString += " ";
  73. }
  74. return fixer.replaceText(culpritToken, fixString);
  75. }
  76. });
  77. }
  78. /**
  79. * Check if the node is binary then report
  80. * @param {ASTNode} node node to evaluate
  81. * @returns {void}
  82. * @private
  83. */
  84. function checkBinary(node) {
  85. const leftNode = (node.left.typeAnnotation) ? node.left.typeAnnotation : node.left;
  86. const rightNode = node.right;
  87. // search for = in AssignmentPattern nodes
  88. const operator = node.operator || "=";
  89. const nonSpacedNode = getFirstNonSpacedToken(leftNode, rightNode, operator);
  90. if (nonSpacedNode) {
  91. if (!(int32Hint && sourceCode.getText(node).endsWith("|0"))) {
  92. report(node, nonSpacedNode);
  93. }
  94. }
  95. }
  96. /**
  97. * Check if the node is conditional
  98. * @param {ASTNode} node node to evaluate
  99. * @returns {void}
  100. * @private
  101. */
  102. function checkConditional(node) {
  103. const nonSpacedConsequesntNode = getFirstNonSpacedToken(node.test, node.consequent, "?");
  104. const nonSpacedAlternateNode = getFirstNonSpacedToken(node.consequent, node.alternate, ":");
  105. if (nonSpacedConsequesntNode) {
  106. report(node, nonSpacedConsequesntNode);
  107. } else if (nonSpacedAlternateNode) {
  108. report(node, nonSpacedAlternateNode);
  109. }
  110. }
  111. /**
  112. * Check if the node is a variable
  113. * @param {ASTNode} node node to evaluate
  114. * @returns {void}
  115. * @private
  116. */
  117. function checkVar(node) {
  118. const leftNode = (node.id.typeAnnotation) ? node.id.typeAnnotation : node.id;
  119. const rightNode = node.init;
  120. if (rightNode) {
  121. const nonSpacedNode = getFirstNonSpacedToken(leftNode, rightNode, "=");
  122. if (nonSpacedNode) {
  123. report(node, nonSpacedNode);
  124. }
  125. }
  126. }
  127. return {
  128. AssignmentExpression: checkBinary,
  129. AssignmentPattern: checkBinary,
  130. BinaryExpression: checkBinary,
  131. LogicalExpression: checkBinary,
  132. ConditionalExpression: checkConditional,
  133. VariableDeclarator: checkVar
  134. };
  135. }
  136. };