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.

space-before-blocks.js 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /**
  2. * @fileoverview A rule to ensure whitespace before blocks.
  3. * @author Mathias Schreck <https://github.com/lo1tuma>
  4. */
  5. "use strict";
  6. const astUtils = require("../util/ast-utils");
  7. //------------------------------------------------------------------------------
  8. // Rule Definition
  9. //------------------------------------------------------------------------------
  10. module.exports = {
  11. meta: {
  12. type: "layout",
  13. docs: {
  14. description: "enforce consistent spacing before blocks",
  15. category: "Stylistic Issues",
  16. recommended: false,
  17. url: "https://eslint.org/docs/rules/space-before-blocks"
  18. },
  19. fixable: "whitespace",
  20. schema: [
  21. {
  22. oneOf: [
  23. {
  24. enum: ["always", "never"]
  25. },
  26. {
  27. type: "object",
  28. properties: {
  29. keywords: {
  30. enum: ["always", "never", "off"]
  31. },
  32. functions: {
  33. enum: ["always", "never", "off"]
  34. },
  35. classes: {
  36. enum: ["always", "never", "off"]
  37. }
  38. },
  39. additionalProperties: false
  40. }
  41. ]
  42. }
  43. ]
  44. },
  45. create(context) {
  46. const config = context.options[0],
  47. sourceCode = context.getSourceCode();
  48. let alwaysFunctions = true,
  49. alwaysKeywords = true,
  50. alwaysClasses = true,
  51. neverFunctions = false,
  52. neverKeywords = false,
  53. neverClasses = false;
  54. if (typeof config === "object") {
  55. alwaysFunctions = config.functions === "always";
  56. alwaysKeywords = config.keywords === "always";
  57. alwaysClasses = config.classes === "always";
  58. neverFunctions = config.functions === "never";
  59. neverKeywords = config.keywords === "never";
  60. neverClasses = config.classes === "never";
  61. } else if (config === "never") {
  62. alwaysFunctions = false;
  63. alwaysKeywords = false;
  64. alwaysClasses = false;
  65. neverFunctions = true;
  66. neverKeywords = true;
  67. neverClasses = true;
  68. }
  69. /**
  70. * Checks whether or not a given token is an arrow operator (=>) or a keyword
  71. * in order to avoid to conflict with `arrow-spacing` and `keyword-spacing`.
  72. *
  73. * @param {Token} token - A token to check.
  74. * @returns {boolean} `true` if the token is an arrow operator.
  75. */
  76. function isConflicted(token) {
  77. return (token.type === "Punctuator" && token.value === "=>") || token.type === "Keyword";
  78. }
  79. /**
  80. * Checks the given BlockStatement node has a preceding space if it doesn’t start on a new line.
  81. * @param {ASTNode|Token} node The AST node of a BlockStatement.
  82. * @returns {void} undefined.
  83. */
  84. function checkPrecedingSpace(node) {
  85. const precedingToken = sourceCode.getTokenBefore(node);
  86. if (precedingToken && !isConflicted(precedingToken) && astUtils.isTokenOnSameLine(precedingToken, node)) {
  87. const hasSpace = sourceCode.isSpaceBetweenTokens(precedingToken, node);
  88. const parent = context.getAncestors().pop();
  89. let requireSpace;
  90. let requireNoSpace;
  91. if (parent.type === "FunctionExpression" || parent.type === "FunctionDeclaration") {
  92. requireSpace = alwaysFunctions;
  93. requireNoSpace = neverFunctions;
  94. } else if (node.type === "ClassBody") {
  95. requireSpace = alwaysClasses;
  96. requireNoSpace = neverClasses;
  97. } else {
  98. requireSpace = alwaysKeywords;
  99. requireNoSpace = neverKeywords;
  100. }
  101. if (requireSpace && !hasSpace) {
  102. context.report({
  103. node,
  104. message: "Missing space before opening brace.",
  105. fix(fixer) {
  106. return fixer.insertTextBefore(node, " ");
  107. }
  108. });
  109. } else if (requireNoSpace && hasSpace) {
  110. context.report({
  111. node,
  112. message: "Unexpected space before opening brace.",
  113. fix(fixer) {
  114. return fixer.removeRange([precedingToken.range[1], node.range[0]]);
  115. }
  116. });
  117. }
  118. }
  119. }
  120. /**
  121. * Checks if the CaseBlock of an given SwitchStatement node has a preceding space.
  122. * @param {ASTNode} node The node of a SwitchStatement.
  123. * @returns {void} undefined.
  124. */
  125. function checkSpaceBeforeCaseBlock(node) {
  126. const cases = node.cases;
  127. let openingBrace;
  128. if (cases.length > 0) {
  129. openingBrace = sourceCode.getTokenBefore(cases[0]);
  130. } else {
  131. openingBrace = sourceCode.getLastToken(node, 1);
  132. }
  133. checkPrecedingSpace(openingBrace);
  134. }
  135. return {
  136. BlockStatement: checkPrecedingSpace,
  137. ClassBody: checkPrecedingSpace,
  138. SwitchStatement: checkSpaceBeforeCaseBlock
  139. };
  140. }
  141. };