block-spacing.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. /** @type {import('../shared/types').Rule} */
  11. module.exports = {
  12. meta: {
  13. type: "layout",
  14. docs: {
  15. description: "Disallow or enforce spaces inside of blocks after opening block and before closing block",
  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/StaticBlock/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. if (node.type === "StaticBlock") {
  45. return sourceCode.getFirstToken(node, { skip: 1 }); // skip the `static` token
  46. }
  47. // "BlockStatement"
  48. return sourceCode.getFirstToken(node);
  49. }
  50. /**
  51. * Checks whether or not:
  52. * - given tokens are on same line.
  53. * - there is/isn't a space between given tokens.
  54. * @param {Token} left A token to check.
  55. * @param {Token} right The token which is next to `left`.
  56. * @returns {boolean}
  57. * When the option is `"always"`, `true` if there are one or more spaces between given tokens.
  58. * When the option is `"never"`, `true` if there are not any spaces between given tokens.
  59. * If given tokens are not on same line, it's always `true`.
  60. */
  61. function isValid(left, right) {
  62. return (
  63. !util.isTokenOnSameLine(left, right) ||
  64. sourceCode.isSpaceBetweenTokens(left, right) === always
  65. );
  66. }
  67. /**
  68. * Checks and reports invalid spacing style inside braces.
  69. * @param {ASTNode} node A BlockStatement/StaticBlock/SwitchStatement node to check.
  70. * @returns {void}
  71. */
  72. function checkSpacingInsideBraces(node) {
  73. // Gets braces and the first/last token of content.
  74. const openBrace = getOpenBrace(node);
  75. const closeBrace = sourceCode.getLastToken(node);
  76. const firstToken = sourceCode.getTokenAfter(openBrace, { includeComments: true });
  77. const lastToken = sourceCode.getTokenBefore(closeBrace, { includeComments: true });
  78. // Skip if the node is invalid or empty.
  79. if (openBrace.type !== "Punctuator" ||
  80. openBrace.value !== "{" ||
  81. closeBrace.type !== "Punctuator" ||
  82. closeBrace.value !== "}" ||
  83. firstToken === closeBrace
  84. ) {
  85. return;
  86. }
  87. // Skip line comments for option never
  88. if (!always && firstToken.type === "Line") {
  89. return;
  90. }
  91. // Check.
  92. if (!isValid(openBrace, firstToken)) {
  93. let loc = openBrace.loc;
  94. if (messageId === "extra") {
  95. loc = {
  96. start: openBrace.loc.end,
  97. end: firstToken.loc.start
  98. };
  99. }
  100. context.report({
  101. node,
  102. loc,
  103. messageId,
  104. data: {
  105. location: "after",
  106. token: openBrace.value
  107. },
  108. fix(fixer) {
  109. if (always) {
  110. return fixer.insertTextBefore(firstToken, " ");
  111. }
  112. return fixer.removeRange([openBrace.range[1], firstToken.range[0]]);
  113. }
  114. });
  115. }
  116. if (!isValid(lastToken, closeBrace)) {
  117. let loc = closeBrace.loc;
  118. if (messageId === "extra") {
  119. loc = {
  120. start: lastToken.loc.end,
  121. end: closeBrace.loc.start
  122. };
  123. }
  124. context.report({
  125. node,
  126. loc,
  127. messageId,
  128. data: {
  129. location: "before",
  130. token: closeBrace.value
  131. },
  132. fix(fixer) {
  133. if (always) {
  134. return fixer.insertTextAfter(lastToken, " ");
  135. }
  136. return fixer.removeRange([lastToken.range[1], closeBrace.range[0]]);
  137. }
  138. });
  139. }
  140. }
  141. return {
  142. BlockStatement: checkSpacingInsideBraces,
  143. StaticBlock: checkSpacingInsideBraces,
  144. SwitchStatement: checkSpacingInsideBraces
  145. };
  146. }
  147. };