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.

max-statements-per-line.js 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /**
  2. * @fileoverview Specify the maximum number of statements allowed per line.
  3. * @author Kenneth Williams
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const astUtils = require("../util/ast-utils");
  10. //------------------------------------------------------------------------------
  11. // Rule Definition
  12. //------------------------------------------------------------------------------
  13. module.exports = {
  14. meta: {
  15. type: "layout",
  16. docs: {
  17. description: "enforce a maximum number of statements allowed per line",
  18. category: "Stylistic Issues",
  19. recommended: false,
  20. url: "https://eslint.org/docs/rules/max-statements-per-line"
  21. },
  22. schema: [
  23. {
  24. type: "object",
  25. properties: {
  26. max: {
  27. type: "integer",
  28. minimum: 1
  29. }
  30. },
  31. additionalProperties: false
  32. }
  33. ]
  34. },
  35. create(context) {
  36. const sourceCode = context.getSourceCode(),
  37. options = context.options[0] || {},
  38. maxStatementsPerLine = typeof options.max !== "undefined" ? options.max : 1,
  39. message = "This line has {{numberOfStatementsOnThisLine}} {{statements}}. Maximum allowed is {{maxStatementsPerLine}}.";
  40. let lastStatementLine = 0,
  41. numberOfStatementsOnThisLine = 0,
  42. firstExtraStatement;
  43. //--------------------------------------------------------------------------
  44. // Helpers
  45. //--------------------------------------------------------------------------
  46. const SINGLE_CHILD_ALLOWED = /^(?:(?:DoWhile|For|ForIn|ForOf|If|Labeled|While)Statement|Export(?:Default|Named)Declaration)$/;
  47. /**
  48. * Reports with the first extra statement, and clears it.
  49. *
  50. * @returns {void}
  51. */
  52. function reportFirstExtraStatementAndClear() {
  53. if (firstExtraStatement) {
  54. context.report({
  55. node: firstExtraStatement,
  56. message,
  57. data: {
  58. numberOfStatementsOnThisLine,
  59. maxStatementsPerLine,
  60. statements: numberOfStatementsOnThisLine === 1 ? "statement" : "statements"
  61. }
  62. });
  63. }
  64. firstExtraStatement = null;
  65. }
  66. /**
  67. * Gets the actual last token of a given node.
  68. *
  69. * @param {ASTNode} node - A node to get. This is a node except EmptyStatement.
  70. * @returns {Token} The actual last token.
  71. */
  72. function getActualLastToken(node) {
  73. return sourceCode.getLastToken(node, astUtils.isNotSemicolonToken);
  74. }
  75. /**
  76. * Addresses a given node.
  77. * It updates the state of this rule, then reports the node if the node violated this rule.
  78. *
  79. * @param {ASTNode} node - A node to check.
  80. * @returns {void}
  81. */
  82. function enterStatement(node) {
  83. const line = node.loc.start.line;
  84. /*
  85. * Skip to allow non-block statements if this is direct child of control statements.
  86. * `if (a) foo();` is counted as 1.
  87. * But `if (a) foo(); else foo();` should be counted as 2.
  88. */
  89. if (SINGLE_CHILD_ALLOWED.test(node.parent.type) &&
  90. node.parent.alternate !== node
  91. ) {
  92. return;
  93. }
  94. // Update state.
  95. if (line === lastStatementLine) {
  96. numberOfStatementsOnThisLine += 1;
  97. } else {
  98. reportFirstExtraStatementAndClear();
  99. numberOfStatementsOnThisLine = 1;
  100. lastStatementLine = line;
  101. }
  102. // Reports if the node violated this rule.
  103. if (numberOfStatementsOnThisLine === maxStatementsPerLine + 1) {
  104. firstExtraStatement = firstExtraStatement || node;
  105. }
  106. }
  107. /**
  108. * Updates the state of this rule with the end line of leaving node to check with the next statement.
  109. *
  110. * @param {ASTNode} node - A node to check.
  111. * @returns {void}
  112. */
  113. function leaveStatement(node) {
  114. const line = getActualLastToken(node).loc.end.line;
  115. // Update state.
  116. if (line !== lastStatementLine) {
  117. reportFirstExtraStatementAndClear();
  118. numberOfStatementsOnThisLine = 1;
  119. lastStatementLine = line;
  120. }
  121. }
  122. //--------------------------------------------------------------------------
  123. // Public API
  124. //--------------------------------------------------------------------------
  125. return {
  126. BreakStatement: enterStatement,
  127. ClassDeclaration: enterStatement,
  128. ContinueStatement: enterStatement,
  129. DebuggerStatement: enterStatement,
  130. DoWhileStatement: enterStatement,
  131. ExpressionStatement: enterStatement,
  132. ForInStatement: enterStatement,
  133. ForOfStatement: enterStatement,
  134. ForStatement: enterStatement,
  135. FunctionDeclaration: enterStatement,
  136. IfStatement: enterStatement,
  137. ImportDeclaration: enterStatement,
  138. LabeledStatement: enterStatement,
  139. ReturnStatement: enterStatement,
  140. SwitchStatement: enterStatement,
  141. ThrowStatement: enterStatement,
  142. TryStatement: enterStatement,
  143. VariableDeclaration: enterStatement,
  144. WhileStatement: enterStatement,
  145. WithStatement: enterStatement,
  146. ExportNamedDeclaration: enterStatement,
  147. ExportDefaultDeclaration: enterStatement,
  148. ExportAllDeclaration: enterStatement,
  149. "BreakStatement:exit": leaveStatement,
  150. "ClassDeclaration:exit": leaveStatement,
  151. "ContinueStatement:exit": leaveStatement,
  152. "DebuggerStatement:exit": leaveStatement,
  153. "DoWhileStatement:exit": leaveStatement,
  154. "ExpressionStatement:exit": leaveStatement,
  155. "ForInStatement:exit": leaveStatement,
  156. "ForOfStatement:exit": leaveStatement,
  157. "ForStatement:exit": leaveStatement,
  158. "FunctionDeclaration:exit": leaveStatement,
  159. "IfStatement:exit": leaveStatement,
  160. "ImportDeclaration:exit": leaveStatement,
  161. "LabeledStatement:exit": leaveStatement,
  162. "ReturnStatement:exit": leaveStatement,
  163. "SwitchStatement:exit": leaveStatement,
  164. "ThrowStatement:exit": leaveStatement,
  165. "TryStatement:exit": leaveStatement,
  166. "VariableDeclaration:exit": leaveStatement,
  167. "WhileStatement:exit": leaveStatement,
  168. "WithStatement:exit": leaveStatement,
  169. "ExportNamedDeclaration:exit": leaveStatement,
  170. "ExportDefaultDeclaration:exit": leaveStatement,
  171. "ExportAllDeclaration:exit": leaveStatement,
  172. "Program:exit": reportFirstExtraStatementAndClear
  173. };
  174. }
  175. };