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.

padded-blocks.js 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /**
  2. * @fileoverview A rule to ensure blank lines within blocks.
  3. * @author Mathias Schreck <https://github.com/lo1tuma>
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. module.exports = {
  10. meta: {
  11. type: "layout",
  12. docs: {
  13. description: "require or disallow padding within blocks",
  14. category: "Stylistic Issues",
  15. recommended: false,
  16. url: "https://eslint.org/docs/rules/padded-blocks"
  17. },
  18. fixable: "whitespace",
  19. schema: [
  20. {
  21. oneOf: [
  22. {
  23. enum: ["always", "never"]
  24. },
  25. {
  26. type: "object",
  27. properties: {
  28. blocks: {
  29. enum: ["always", "never"]
  30. },
  31. switches: {
  32. enum: ["always", "never"]
  33. },
  34. classes: {
  35. enum: ["always", "never"]
  36. }
  37. },
  38. additionalProperties: false,
  39. minProperties: 1
  40. }
  41. ]
  42. }
  43. ]
  44. },
  45. create(context) {
  46. const options = {};
  47. const config = context.options[0] || "always";
  48. if (typeof config === "string") {
  49. const shouldHavePadding = config === "always";
  50. options.blocks = shouldHavePadding;
  51. options.switches = shouldHavePadding;
  52. options.classes = shouldHavePadding;
  53. } else {
  54. if (Object.prototype.hasOwnProperty.call(config, "blocks")) {
  55. options.blocks = config.blocks === "always";
  56. }
  57. if (Object.prototype.hasOwnProperty.call(config, "switches")) {
  58. options.switches = config.switches === "always";
  59. }
  60. if (Object.prototype.hasOwnProperty.call(config, "classes")) {
  61. options.classes = config.classes === "always";
  62. }
  63. }
  64. const ALWAYS_MESSAGE = "Block must be padded by blank lines.",
  65. NEVER_MESSAGE = "Block must not be padded by blank lines.";
  66. const sourceCode = context.getSourceCode();
  67. /**
  68. * Gets the open brace token from a given node.
  69. * @param {ASTNode} node - A BlockStatement or SwitchStatement node from which to get the open brace.
  70. * @returns {Token} The token of the open brace.
  71. */
  72. function getOpenBrace(node) {
  73. if (node.type === "SwitchStatement") {
  74. return sourceCode.getTokenBefore(node.cases[0]);
  75. }
  76. return sourceCode.getFirstToken(node);
  77. }
  78. /**
  79. * Checks if the given parameter is a comment node
  80. * @param {ASTNode|Token} node An AST node or token
  81. * @returns {boolean} True if node is a comment
  82. */
  83. function isComment(node) {
  84. return node.type === "Line" || node.type === "Block";
  85. }
  86. /**
  87. * Checks if there is padding between two tokens
  88. * @param {Token} first The first token
  89. * @param {Token} second The second token
  90. * @returns {boolean} True if there is at least a line between the tokens
  91. */
  92. function isPaddingBetweenTokens(first, second) {
  93. return second.loc.start.line - first.loc.end.line >= 2;
  94. }
  95. /**
  96. * Checks if the given token has a blank line after it.
  97. * @param {Token} token The token to check.
  98. * @returns {boolean} Whether or not the token is followed by a blank line.
  99. */
  100. function getFirstBlockToken(token) {
  101. let prev,
  102. first = token;
  103. do {
  104. prev = first;
  105. first = sourceCode.getTokenAfter(first, { includeComments: true });
  106. } while (isComment(first) && first.loc.start.line === prev.loc.end.line);
  107. return first;
  108. }
  109. /**
  110. * Checks if the given token is preceeded by a blank line.
  111. * @param {Token} token The token to check
  112. * @returns {boolean} Whether or not the token is preceeded by a blank line
  113. */
  114. function getLastBlockToken(token) {
  115. let last = token,
  116. next;
  117. do {
  118. next = last;
  119. last = sourceCode.getTokenBefore(last, { includeComments: true });
  120. } while (isComment(last) && last.loc.end.line === next.loc.start.line);
  121. return last;
  122. }
  123. /**
  124. * Checks if a node should be padded, according to the rule config.
  125. * @param {ASTNode} node The AST node to check.
  126. * @returns {boolean} True if the node should be padded, false otherwise.
  127. */
  128. function requirePaddingFor(node) {
  129. switch (node.type) {
  130. case "BlockStatement":
  131. return options.blocks;
  132. case "SwitchStatement":
  133. return options.switches;
  134. case "ClassBody":
  135. return options.classes;
  136. /* istanbul ignore next */
  137. default:
  138. throw new Error("unreachable");
  139. }
  140. }
  141. /**
  142. * Checks the given BlockStatement node to be padded if the block is not empty.
  143. * @param {ASTNode} node The AST node of a BlockStatement.
  144. * @returns {void} undefined.
  145. */
  146. function checkPadding(node) {
  147. const openBrace = getOpenBrace(node),
  148. firstBlockToken = getFirstBlockToken(openBrace),
  149. tokenBeforeFirst = sourceCode.getTokenBefore(firstBlockToken, { includeComments: true }),
  150. closeBrace = sourceCode.getLastToken(node),
  151. lastBlockToken = getLastBlockToken(closeBrace),
  152. tokenAfterLast = sourceCode.getTokenAfter(lastBlockToken, { includeComments: true }),
  153. blockHasTopPadding = isPaddingBetweenTokens(tokenBeforeFirst, firstBlockToken),
  154. blockHasBottomPadding = isPaddingBetweenTokens(lastBlockToken, tokenAfterLast);
  155. if (requirePaddingFor(node)) {
  156. if (!blockHasTopPadding) {
  157. context.report({
  158. node,
  159. loc: { line: tokenBeforeFirst.loc.start.line, column: tokenBeforeFirst.loc.start.column },
  160. fix(fixer) {
  161. return fixer.insertTextAfter(tokenBeforeFirst, "\n");
  162. },
  163. message: ALWAYS_MESSAGE
  164. });
  165. }
  166. if (!blockHasBottomPadding) {
  167. context.report({
  168. node,
  169. loc: { line: tokenAfterLast.loc.end.line, column: tokenAfterLast.loc.end.column - 1 },
  170. fix(fixer) {
  171. return fixer.insertTextBefore(tokenAfterLast, "\n");
  172. },
  173. message: ALWAYS_MESSAGE
  174. });
  175. }
  176. } else {
  177. if (blockHasTopPadding) {
  178. context.report({
  179. node,
  180. loc: { line: tokenBeforeFirst.loc.start.line, column: tokenBeforeFirst.loc.start.column },
  181. fix(fixer) {
  182. return fixer.replaceTextRange([tokenBeforeFirst.range[1], firstBlockToken.range[0] - firstBlockToken.loc.start.column], "\n");
  183. },
  184. message: NEVER_MESSAGE
  185. });
  186. }
  187. if (blockHasBottomPadding) {
  188. context.report({
  189. node,
  190. loc: { line: tokenAfterLast.loc.end.line, column: tokenAfterLast.loc.end.column - 1 },
  191. message: NEVER_MESSAGE,
  192. fix(fixer) {
  193. return fixer.replaceTextRange([lastBlockToken.range[1], tokenAfterLast.range[0] - tokenAfterLast.loc.start.column], "\n");
  194. }
  195. });
  196. }
  197. }
  198. }
  199. const rule = {};
  200. if (Object.prototype.hasOwnProperty.call(options, "switches")) {
  201. rule.SwitchStatement = function(node) {
  202. if (node.cases.length === 0) {
  203. return;
  204. }
  205. checkPadding(node);
  206. };
  207. }
  208. if (Object.prototype.hasOwnProperty.call(options, "blocks")) {
  209. rule.BlockStatement = function(node) {
  210. if (node.body.length === 0) {
  211. return;
  212. }
  213. checkPadding(node);
  214. };
  215. }
  216. if (Object.prototype.hasOwnProperty.call(options, "classes")) {
  217. rule.ClassBody = function(node) {
  218. if (node.body.length === 0) {
  219. return;
  220. }
  221. checkPadding(node);
  222. };
  223. }
  224. return rule;
  225. }
  226. };