block-spacing.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /**
  2. * @fileoverview A rule to disallow or enforce spaces inside of single line blocks.
  3. * @author Toru Nagashima
  4. */
  5. "use strict";
  6. const util = require("./utils/ast-utils");
  7. //------------------------------------------------------------------------------
  8. // Rule Definition
  9. //------------------------------------------------------------------------------
  10. module.exports = {
  11. meta: {
  12. type: "layout",
  13. docs: {
  14. description: "disallow or enforce spaces inside of blocks after opening block and before closing block",
  15. category: "Stylistic Issues",
  16. recommended: false,
  17. url: "https://eslint.org/docs/rules/block-spacing"
  18. },
  19. fixable: "whitespace",
  20. schema: [
  21. { enum: ["always", "never"] }
  22. ],
  23. messages: {
  24. missing: "Requires a space {{location}} '{{token}}'.",
  25. extra: "Unexpected space(s) {{location}} '{{token}}'."
  26. }
  27. },
  28. create(context) {
  29. const always = (context.options[0] !== "never"),
  30. messageId = always ? "missing" : "extra",
  31. sourceCode = context.getSourceCode();
  32. /**
  33. * Gets the open brace token from a given node.
  34. * @param {ASTNode} node A BlockStatement/SwitchStatement node to get.
  35. * @returns {Token} The token of the open brace.
  36. */
  37. function getOpenBrace(node) {
  38. if (node.type === "SwitchStatement") {
  39. if (node.cases.length > 0) {
  40. return sourceCode.getTokenBefore(node.cases[0]);
  41. }
  42. return sourceCode.getLastToken(node, 1);
  43. }
  44. return sourceCode.getFirstToken(node);
  45. }
  46. /**
  47. * Checks whether or not:
  48. * - given tokens are on same line.
  49. * - there is/isn't a space between given tokens.
  50. * @param {Token} left A token to check.
  51. * @param {Token} right The token which is next to `left`.
  52. * @returns {boolean}
  53. * When the option is `"always"`, `true` if there are one or more spaces between given tokens.
  54. * When the option is `"never"`, `true` if there are not any spaces between given tokens.
  55. * If given tokens are not on same line, it's always `true`.
  56. */
  57. function isValid(left, right) {
  58. return (
  59. !util.isTokenOnSameLine(left, right) ||
  60. sourceCode.isSpaceBetweenTokens(left, right) === always
  61. );
  62. }
  63. /**
  64. * Reports invalid spacing style inside braces.
  65. * @param {ASTNode} node A BlockStatement/SwitchStatement node to get.
  66. * @returns {void}
  67. */
  68. function checkSpacingInsideBraces(node) {
  69. // Gets braces and the first/last token of content.
  70. const openBrace = getOpenBrace(node);
  71. const closeBrace = sourceCode.getLastToken(node);
  72. const firstToken = sourceCode.getTokenAfter(openBrace, { includeComments: true });
  73. const lastToken = sourceCode.getTokenBefore(closeBrace, { includeComments: true });
  74. // Skip if the node is invalid or empty.
  75. if (openBrace.type !== "Punctuator" ||
  76. openBrace.value !== "{" ||
  77. closeBrace.type !== "Punctuator" ||
  78. closeBrace.value !== "}" ||
  79. firstToken === closeBrace
  80. ) {
  81. return;
  82. }
  83. // Skip line comments for option never
  84. if (!always && firstToken.type === "Line") {
  85. return;
  86. }
  87. // Check.
  88. if (!isValid(openBrace, firstToken)) {
  89. let loc = openBrace.loc;
  90. if (messageId === "extra") {
  91. loc = {
  92. start: openBrace.loc.end,
  93. end: firstToken.loc.start
  94. };
  95. }
  96. context.report({
  97. node,
  98. loc,
  99. messageId,
  100. data: {
  101. location: "after",
  102. token: openBrace.value
  103. },
  104. fix(fixer) {
  105. if (always) {
  106. return fixer.insertTextBefore(firstToken, " ");
  107. }
  108. return fixer.removeRange([openBrace.range[1], firstToken.range[0]]);
  109. }
  110. });
  111. }
  112. if (!isValid(lastToken, closeBrace)) {
  113. let loc = closeBrace.loc;
  114. if (messageId === "extra") {
  115. loc = {
  116. start: lastToken.loc.end,
  117. end: closeBrace.loc.start
  118. };
  119. }
  120. context.report({
  121. node,
  122. loc,
  123. messageId,
  124. data: {
  125. location: "before",
  126. token: closeBrace.value
  127. },
  128. fix(fixer) {
  129. if (always) {
  130. return fixer.insertTextAfter(lastToken, " ");
  131. }
  132. return fixer.removeRange([lastToken.range[1], closeBrace.range[0]]);
  133. }
  134. });
  135. }
  136. }
  137. return {
  138. BlockStatement: checkSpacingInsideBraces,
  139. SwitchStatement: checkSpacingInsideBraces
  140. };
  141. }
  142. };