Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

block-spacing.js 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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("../util/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. context.report({
  90. node,
  91. loc: openBrace.loc.start,
  92. messageId,
  93. data: {
  94. location: "after",
  95. token: openBrace.value
  96. },
  97. fix(fixer) {
  98. if (always) {
  99. return fixer.insertTextBefore(firstToken, " ");
  100. }
  101. return fixer.removeRange([openBrace.range[1], firstToken.range[0]]);
  102. }
  103. });
  104. }
  105. if (!isValid(lastToken, closeBrace)) {
  106. context.report({
  107. node,
  108. loc: closeBrace.loc.start,
  109. messageId,
  110. data: {
  111. location: "before",
  112. token: closeBrace.value
  113. },
  114. fix(fixer) {
  115. if (always) {
  116. return fixer.insertTextAfter(lastToken, " ");
  117. }
  118. return fixer.removeRange([lastToken.range[1], closeBrace.range[0]]);
  119. }
  120. });
  121. }
  122. }
  123. return {
  124. BlockStatement: checkSpacingInsideBraces,
  125. SwitchStatement: checkSpacingInsideBraces
  126. };
  127. }
  128. };