switch-colon-spacing.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /**
  2. * @fileoverview Rule to enforce spacing around colons of switch statements.
  3. * @author Toru Nagashima
  4. * @deprecated in ESLint v8.53.0
  5. */
  6. "use strict";
  7. //------------------------------------------------------------------------------
  8. // Requirements
  9. //------------------------------------------------------------------------------
  10. const astUtils = require("./utils/ast-utils");
  11. //------------------------------------------------------------------------------
  12. // Rule Definition
  13. //------------------------------------------------------------------------------
  14. /** @type {import('../types').Rule.RuleModule} */
  15. module.exports = {
  16. meta: {
  17. deprecated: {
  18. message: "Formatting rules are being moved out of ESLint core.",
  19. url: "https://eslint.org/blog/2023/10/deprecating-formatting-rules/",
  20. deprecatedSince: "8.53.0",
  21. availableUntil: "11.0.0",
  22. replacedBy: [
  23. {
  24. message:
  25. "ESLint Stylistic now maintains deprecated stylistic core rules.",
  26. url: "https://eslint.style/guide/migration",
  27. plugin: {
  28. name: "@stylistic/eslint-plugin",
  29. url: "https://eslint.style",
  30. },
  31. rule: {
  32. name: "switch-colon-spacing",
  33. url: "https://eslint.style/rules/switch-colon-spacing",
  34. },
  35. },
  36. ],
  37. },
  38. type: "layout",
  39. docs: {
  40. description: "Enforce spacing around colons of switch statements",
  41. recommended: false,
  42. url: "https://eslint.org/docs/latest/rules/switch-colon-spacing",
  43. },
  44. schema: [
  45. {
  46. type: "object",
  47. properties: {
  48. before: { type: "boolean", default: false },
  49. after: { type: "boolean", default: true },
  50. },
  51. additionalProperties: false,
  52. },
  53. ],
  54. fixable: "whitespace",
  55. messages: {
  56. expectedBefore: "Expected space(s) before this colon.",
  57. expectedAfter: "Expected space(s) after this colon.",
  58. unexpectedBefore: "Unexpected space(s) before this colon.",
  59. unexpectedAfter: "Unexpected space(s) after this colon.",
  60. },
  61. },
  62. create(context) {
  63. const sourceCode = context.sourceCode;
  64. const options = context.options[0] || {};
  65. const beforeSpacing = options.before === true; // false by default
  66. const afterSpacing = options.after !== false; // true by default
  67. /**
  68. * Check whether the spacing between the given 2 tokens is valid or not.
  69. * @param {Token} left The left token to check.
  70. * @param {Token} right The right token to check.
  71. * @param {boolean} expected The expected spacing to check. `true` if there should be a space.
  72. * @returns {boolean} `true` if the spacing between the tokens is valid.
  73. */
  74. function isValidSpacing(left, right, expected) {
  75. return (
  76. astUtils.isClosingBraceToken(right) ||
  77. !astUtils.isTokenOnSameLine(left, right) ||
  78. sourceCode.isSpaceBetweenTokens(left, right) === expected
  79. );
  80. }
  81. /**
  82. * Check whether comments exist between the given 2 tokens.
  83. * @param {Token} left The left token to check.
  84. * @param {Token} right The right token to check.
  85. * @returns {boolean} `true` if comments exist between the given 2 tokens.
  86. */
  87. function commentsExistBetween(left, right) {
  88. return (
  89. sourceCode.getFirstTokenBetween(left, right, {
  90. includeComments: true,
  91. filter: astUtils.isCommentToken,
  92. }) !== null
  93. );
  94. }
  95. /**
  96. * Fix the spacing between the given 2 tokens.
  97. * @param {RuleFixer} fixer The fixer to fix.
  98. * @param {Token} left The left token of fix range.
  99. * @param {Token} right The right token of fix range.
  100. * @param {boolean} spacing The spacing style. `true` if there should be a space.
  101. * @returns {Fix|null} The fix object.
  102. */
  103. function fix(fixer, left, right, spacing) {
  104. if (commentsExistBetween(left, right)) {
  105. return null;
  106. }
  107. if (spacing) {
  108. return fixer.insertTextAfter(left, " ");
  109. }
  110. return fixer.removeRange([left.range[1], right.range[0]]);
  111. }
  112. return {
  113. SwitchCase(node) {
  114. const colonToken = astUtils.getSwitchCaseColonToken(
  115. node,
  116. sourceCode,
  117. );
  118. const beforeToken = sourceCode.getTokenBefore(colonToken);
  119. const afterToken = sourceCode.getTokenAfter(colonToken);
  120. if (!isValidSpacing(beforeToken, colonToken, beforeSpacing)) {
  121. context.report({
  122. node,
  123. loc: colonToken.loc,
  124. messageId: beforeSpacing
  125. ? "expectedBefore"
  126. : "unexpectedBefore",
  127. fix: fixer =>
  128. fix(fixer, beforeToken, colonToken, beforeSpacing),
  129. });
  130. }
  131. if (!isValidSpacing(colonToken, afterToken, afterSpacing)) {
  132. context.report({
  133. node,
  134. loc: colonToken.loc,
  135. messageId: afterSpacing
  136. ? "expectedAfter"
  137. : "unexpectedAfter",
  138. fix: fixer =>
  139. fix(fixer, colonToken, afterToken, afterSpacing),
  140. });
  141. }
  142. },
  143. };
  144. },
  145. };