项目原始demo,不改动
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
Это архивный репозиторий. Вы можете его клонировать или просматривать файлы, но не вносить изменения или открывать задачи/запросы на слияние.

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