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.js 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /**
  2. * @fileoverview A rule to set the maximum number of statements in a function.
  3. * @author Ian Christian Myers
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Requirements
  8. //------------------------------------------------------------------------------
  9. const lodash = require("lodash");
  10. const astUtils = require("../util/ast-utils");
  11. //------------------------------------------------------------------------------
  12. // Rule Definition
  13. //------------------------------------------------------------------------------
  14. module.exports = {
  15. meta: {
  16. type: "suggestion",
  17. docs: {
  18. description: "enforce a maximum number of statements allowed in function blocks",
  19. category: "Stylistic Issues",
  20. recommended: false,
  21. url: "https://eslint.org/docs/rules/max-statements"
  22. },
  23. schema: [
  24. {
  25. oneOf: [
  26. {
  27. type: "integer",
  28. minimum: 0
  29. },
  30. {
  31. type: "object",
  32. properties: {
  33. maximum: {
  34. type: "integer",
  35. minimum: 0
  36. },
  37. max: {
  38. type: "integer",
  39. minimum: 0
  40. }
  41. },
  42. additionalProperties: false
  43. }
  44. ]
  45. },
  46. {
  47. type: "object",
  48. properties: {
  49. ignoreTopLevelFunctions: {
  50. type: "boolean"
  51. }
  52. },
  53. additionalProperties: false
  54. }
  55. ]
  56. },
  57. create(context) {
  58. //--------------------------------------------------------------------------
  59. // Helpers
  60. //--------------------------------------------------------------------------
  61. const functionStack = [],
  62. option = context.options[0],
  63. ignoreTopLevelFunctions = context.options[1] && context.options[1].ignoreTopLevelFunctions || false,
  64. topLevelFunctions = [];
  65. let maxStatements = 10;
  66. if (typeof option === "object" && Object.prototype.hasOwnProperty.call(option, "maximum") && typeof option.maximum === "number") {
  67. maxStatements = option.maximum;
  68. }
  69. if (typeof option === "object" && Object.prototype.hasOwnProperty.call(option, "max") && typeof option.max === "number") {
  70. maxStatements = option.max;
  71. }
  72. if (typeof option === "number") {
  73. maxStatements = option;
  74. }
  75. /**
  76. * Reports a node if it has too many statements
  77. * @param {ASTNode} node node to evaluate
  78. * @param {int} count Number of statements in node
  79. * @param {int} max Maximum number of statements allowed
  80. * @returns {void}
  81. * @private
  82. */
  83. function reportIfTooManyStatements(node, count, max) {
  84. if (count > max) {
  85. const name = lodash.upperFirst(astUtils.getFunctionNameWithKind(node));
  86. context.report({
  87. node,
  88. message: "{{name}} has too many statements ({{count}}). Maximum allowed is {{max}}.",
  89. data: { name, count, max }
  90. });
  91. }
  92. }
  93. /**
  94. * When parsing a new function, store it in our function stack
  95. * @returns {void}
  96. * @private
  97. */
  98. function startFunction() {
  99. functionStack.push(0);
  100. }
  101. /**
  102. * Evaluate the node at the end of function
  103. * @param {ASTNode} node node to evaluate
  104. * @returns {void}
  105. * @private
  106. */
  107. function endFunction(node) {
  108. const count = functionStack.pop();
  109. if (ignoreTopLevelFunctions && functionStack.length === 0) {
  110. topLevelFunctions.push({ node, count });
  111. } else {
  112. reportIfTooManyStatements(node, count, maxStatements);
  113. }
  114. }
  115. /**
  116. * Increment the count of the functions
  117. * @param {ASTNode} node node to evaluate
  118. * @returns {void}
  119. * @private
  120. */
  121. function countStatements(node) {
  122. functionStack[functionStack.length - 1] += node.body.length;
  123. }
  124. //--------------------------------------------------------------------------
  125. // Public API
  126. //--------------------------------------------------------------------------
  127. return {
  128. FunctionDeclaration: startFunction,
  129. FunctionExpression: startFunction,
  130. ArrowFunctionExpression: startFunction,
  131. BlockStatement: countStatements,
  132. "FunctionDeclaration:exit": endFunction,
  133. "FunctionExpression:exit": endFunction,
  134. "ArrowFunctionExpression:exit": endFunction,
  135. "Program:exit"() {
  136. if (topLevelFunctions.length === 1) {
  137. return;
  138. }
  139. topLevelFunctions.forEach(element => {
  140. const count = element.count;
  141. const node = element.node;
  142. reportIfTooManyStatements(node, count, maxStatements);
  143. });
  144. }
  145. };
  146. }
  147. };