switch-colon-spacing.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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("./utils/ast-utils");
  10. //------------------------------------------------------------------------------
  11. // Rule Definition
  12. //------------------------------------------------------------------------------
  13. /** @type {import('../shared/types').Rule} */
  14. module.exports = {
  15. meta: {
  16. type: "layout",
  17. docs: {
  18. description: "Enforce spacing around colons of switch statements",
  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", default: false },
  27. after: { type: "boolean", default: true }
  28. },
  29. additionalProperties: false
  30. }
  31. ],
  32. fixable: "whitespace",
  33. messages: {
  34. expectedBefore: "Expected space(s) before this colon.",
  35. expectedAfter: "Expected space(s) after this colon.",
  36. unexpectedBefore: "Unexpected space(s) before this colon.",
  37. unexpectedAfter: "Unexpected space(s) after this colon."
  38. }
  39. },
  40. create(context) {
  41. const sourceCode = context.getSourceCode();
  42. const options = context.options[0] || {};
  43. const beforeSpacing = options.before === true; // false by default
  44. const afterSpacing = options.after !== false; // true by default
  45. /**
  46. * Check whether the spacing between the given 2 tokens is valid or not.
  47. * @param {Token} left The left token to check.
  48. * @param {Token} right The right token to check.
  49. * @param {boolean} expected The expected spacing to check. `true` if there should be a space.
  50. * @returns {boolean} `true` if the spacing between the tokens is valid.
  51. */
  52. function isValidSpacing(left, right, expected) {
  53. return (
  54. astUtils.isClosingBraceToken(right) ||
  55. !astUtils.isTokenOnSameLine(left, right) ||
  56. sourceCode.isSpaceBetweenTokens(left, right) === expected
  57. );
  58. }
  59. /**
  60. * Check whether comments exist between the given 2 tokens.
  61. * @param {Token} left The left token to check.
  62. * @param {Token} right The right token to check.
  63. * @returns {boolean} `true` if comments exist between the given 2 tokens.
  64. */
  65. function commentsExistBetween(left, right) {
  66. return sourceCode.getFirstTokenBetween(
  67. left,
  68. right,
  69. {
  70. includeComments: true,
  71. filter: astUtils.isCommentToken
  72. }
  73. ) !== null;
  74. }
  75. /**
  76. * Fix the spacing between the given 2 tokens.
  77. * @param {RuleFixer} fixer The fixer to fix.
  78. * @param {Token} left The left token of fix range.
  79. * @param {Token} right The right token of fix range.
  80. * @param {boolean} spacing The spacing style. `true` if there should be a space.
  81. * @returns {Fix|null} The fix object.
  82. */
  83. function fix(fixer, left, right, spacing) {
  84. if (commentsExistBetween(left, right)) {
  85. return null;
  86. }
  87. if (spacing) {
  88. return fixer.insertTextAfter(left, " ");
  89. }
  90. return fixer.removeRange([left.range[1], right.range[0]]);
  91. }
  92. return {
  93. SwitchCase(node) {
  94. const colonToken = astUtils.getSwitchCaseColonToken(node, sourceCode);
  95. const beforeToken = sourceCode.getTokenBefore(colonToken);
  96. const afterToken = sourceCode.getTokenAfter(colonToken);
  97. if (!isValidSpacing(beforeToken, colonToken, beforeSpacing)) {
  98. context.report({
  99. node,
  100. loc: colonToken.loc,
  101. messageId: beforeSpacing ? "expectedBefore" : "unexpectedBefore",
  102. fix: fixer => fix(fixer, beforeToken, colonToken, beforeSpacing)
  103. });
  104. }
  105. if (!isValidSpacing(colonToken, afterToken, afterSpacing)) {
  106. context.report({
  107. node,
  108. loc: colonToken.loc,
  109. messageId: afterSpacing ? "expectedAfter" : "unexpectedAfter",
  110. fix: fixer => fix(fixer, colonToken, afterToken, afterSpacing)
  111. });
  112. }
  113. }
  114. };
  115. }
  116. };