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.

switch-colon-spacing.js 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**
  2. * @fileoverview Rule to enforce spacing around colons of switch statements.
  3. * @author Toru Nagashima
  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 spacing around colons of switch statements",
  18. category: "Stylistic Issues",
  19. recommended: false,
  20. url: "https://eslint.org/docs/rules/switch-colon-spacing"
  21. },
  22. schema: [
  23. {
  24. type: "object",
  25. properties: {
  26. before: { type: "boolean" },
  27. after: { type: "boolean" }
  28. },
  29. additionalProperties: false
  30. }
  31. ],
  32. fixable: "whitespace"
  33. },
  34. create(context) {
  35. const sourceCode = context.getSourceCode();
  36. const options = context.options[0] || {};
  37. const beforeSpacing = options.before === true; // false by default
  38. const afterSpacing = options.after !== false; // true by default
  39. /**
  40. * Get the colon token of the given SwitchCase node.
  41. * @param {ASTNode} node The SwitchCase node to get.
  42. * @returns {Token} The colon token of the node.
  43. */
  44. function getColonToken(node) {
  45. if (node.test) {
  46. return sourceCode.getTokenAfter(node.test, astUtils.isColonToken);
  47. }
  48. return sourceCode.getFirstToken(node, 1);
  49. }
  50. /**
  51. * Check whether the spacing between the given 2 tokens is valid or not.
  52. * @param {Token} left The left token to check.
  53. * @param {Token} right The right token to check.
  54. * @param {boolean} expected The expected spacing to check. `true` if there should be a space.
  55. * @returns {boolean} `true` if the spacing between the tokens is valid.
  56. */
  57. function isValidSpacing(left, right, expected) {
  58. return (
  59. astUtils.isClosingBraceToken(right) ||
  60. !astUtils.isTokenOnSameLine(left, right) ||
  61. sourceCode.isSpaceBetweenTokens(left, right) === expected
  62. );
  63. }
  64. /**
  65. * Check whether comments exist between the given 2 tokens.
  66. * @param {Token} left The left token to check.
  67. * @param {Token} right The right token to check.
  68. * @returns {boolean} `true` if comments exist between the given 2 tokens.
  69. */
  70. function commentsExistBetween(left, right) {
  71. return sourceCode.getFirstTokenBetween(
  72. left,
  73. right,
  74. {
  75. includeComments: true,
  76. filter: astUtils.isCommentToken
  77. }
  78. ) !== null;
  79. }
  80. /**
  81. * Fix the spacing between the given 2 tokens.
  82. * @param {RuleFixer} fixer The fixer to fix.
  83. * @param {Token} left The left token of fix range.
  84. * @param {Token} right The right token of fix range.
  85. * @param {boolean} spacing The spacing style. `true` if there should be a space.
  86. * @returns {Fix|null} The fix object.
  87. */
  88. function fix(fixer, left, right, spacing) {
  89. if (commentsExistBetween(left, right)) {
  90. return null;
  91. }
  92. if (spacing) {
  93. return fixer.insertTextAfter(left, " ");
  94. }
  95. return fixer.removeRange([left.range[1], right.range[0]]);
  96. }
  97. return {
  98. SwitchCase(node) {
  99. const colonToken = getColonToken(node);
  100. const beforeToken = sourceCode.getTokenBefore(colonToken);
  101. const afterToken = sourceCode.getTokenAfter(colonToken);
  102. if (!isValidSpacing(beforeToken, colonToken, beforeSpacing)) {
  103. context.report({
  104. node,
  105. loc: colonToken.loc,
  106. message: "{{verb}} space(s) before this colon.",
  107. data: { verb: beforeSpacing ? "Expected" : "Unexpected" },
  108. fix: fixer => fix(fixer, beforeToken, colonToken, beforeSpacing)
  109. });
  110. }
  111. if (!isValidSpacing(colonToken, afterToken, afterSpacing)) {
  112. context.report({
  113. node,
  114. loc: colonToken.loc,
  115. message: "{{verb}} space(s) after this colon.",
  116. data: { verb: afterSpacing ? "Expected" : "Unexpected" },
  117. fix: fixer => fix(fixer, colonToken, afterToken, afterSpacing)
  118. });
  119. }
  120. }
  121. };
  122. }
  123. };