Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
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 6.6KB

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