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.

arrow-spacing.js 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /**
  2. * @fileoverview Rule to define spacing before/after arrow function's arrow.
  3. * @author Jxck
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const astUtils = require("../util/ast-utils");
  10. //------------------------------------------------------------------------------
  11. // Rule Definition
  12. //------------------------------------------------------------------------------
  13. module.exports = {
  14. meta: {
  15. type: "layout",
  16. docs: {
  17. description: "enforce consistent spacing before and after the arrow in arrow functions",
  18. category: "ECMAScript 6",
  19. recommended: false,
  20. url: "https://eslint.org/docs/rules/arrow-spacing"
  21. },
  22. fixable: "whitespace",
  23. schema: [
  24. {
  25. type: "object",
  26. properties: {
  27. before: {
  28. type: "boolean"
  29. },
  30. after: {
  31. type: "boolean"
  32. }
  33. },
  34. additionalProperties: false
  35. }
  36. ],
  37. messages: {
  38. expectedBefore: "Missing space before =>.",
  39. unexpectedBefore: "Unexpected space before =>.",
  40. expectedAfter: "Missing space after =>.",
  41. unexpectedAfter: "Unexpected space after =>."
  42. }
  43. },
  44. create(context) {
  45. // merge rules with default
  46. const rule = { before: true, after: true },
  47. option = context.options[0] || {};
  48. rule.before = option.before !== false;
  49. rule.after = option.after !== false;
  50. const sourceCode = context.getSourceCode();
  51. /**
  52. * Get tokens of arrow(`=>`) and before/after arrow.
  53. * @param {ASTNode} node The arrow function node.
  54. * @returns {Object} Tokens of arrow and before/after arrow.
  55. */
  56. function getTokens(node) {
  57. const arrow = sourceCode.getTokenBefore(node.body, astUtils.isArrowToken);
  58. return {
  59. before: sourceCode.getTokenBefore(arrow),
  60. arrow,
  61. after: sourceCode.getTokenAfter(arrow)
  62. };
  63. }
  64. /**
  65. * Count spaces before/after arrow(`=>`) token.
  66. * @param {Object} tokens Tokens before/after arrow.
  67. * @returns {Object} count of space before/after arrow.
  68. */
  69. function countSpaces(tokens) {
  70. const before = tokens.arrow.range[0] - tokens.before.range[1];
  71. const after = tokens.after.range[0] - tokens.arrow.range[1];
  72. return { before, after };
  73. }
  74. /**
  75. * Determines whether space(s) before after arrow(`=>`) is satisfy rule.
  76. * if before/after value is `true`, there should be space(s).
  77. * if before/after value is `false`, there should be no space.
  78. * @param {ASTNode} node The arrow function node.
  79. * @returns {void}
  80. */
  81. function spaces(node) {
  82. const tokens = getTokens(node);
  83. const countSpace = countSpaces(tokens);
  84. if (rule.before) {
  85. // should be space(s) before arrow
  86. if (countSpace.before === 0) {
  87. context.report({
  88. node: tokens.before,
  89. messageId: "expectedBefore",
  90. fix(fixer) {
  91. return fixer.insertTextBefore(tokens.arrow, " ");
  92. }
  93. });
  94. }
  95. } else {
  96. // should be no space before arrow
  97. if (countSpace.before > 0) {
  98. context.report({
  99. node: tokens.before,
  100. messageId: "unexpectedBefore",
  101. fix(fixer) {
  102. return fixer.removeRange([tokens.before.range[1], tokens.arrow.range[0]]);
  103. }
  104. });
  105. }
  106. }
  107. if (rule.after) {
  108. // should be space(s) after arrow
  109. if (countSpace.after === 0) {
  110. context.report({
  111. node: tokens.after,
  112. messageId: "expectedAfter",
  113. fix(fixer) {
  114. return fixer.insertTextAfter(tokens.arrow, " ");
  115. }
  116. });
  117. }
  118. } else {
  119. // should be no space after arrow
  120. if (countSpace.after > 0) {
  121. context.report({
  122. node: tokens.after,
  123. messageId: "unexpectedAfter",
  124. fix(fixer) {
  125. return fixer.removeRange([tokens.arrow.range[1], tokens.after.range[0]]);
  126. }
  127. });
  128. }
  129. }
  130. }
  131. return {
  132. ArrowFunctionExpression: spaces
  133. };
  134. }
  135. };