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.

complexity.js 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /**
  2. * @fileoverview Counts the cyclomatic complexity of each function of the script. See http://en.wikipedia.org/wiki/Cyclomatic_complexity.
  3. * Counts the number of if, conditional, for, whilte, try, switch/case,
  4. * @author Patrick Brosset
  5. */
  6. "use strict";
  7. //------------------------------------------------------------------------------
  8. // Requirements
  9. //------------------------------------------------------------------------------
  10. const lodash = require("lodash");
  11. const astUtils = require("../util/ast-utils");
  12. //------------------------------------------------------------------------------
  13. // Rule Definition
  14. //------------------------------------------------------------------------------
  15. module.exports = {
  16. meta: {
  17. type: "suggestion",
  18. docs: {
  19. description: "enforce a maximum cyclomatic complexity allowed in a program",
  20. category: "Best Practices",
  21. recommended: false,
  22. url: "https://eslint.org/docs/rules/complexity"
  23. },
  24. schema: [
  25. {
  26. oneOf: [
  27. {
  28. type: "integer",
  29. minimum: 0
  30. },
  31. {
  32. type: "object",
  33. properties: {
  34. maximum: {
  35. type: "integer",
  36. minimum: 0
  37. },
  38. max: {
  39. type: "integer",
  40. minimum: 0
  41. }
  42. },
  43. additionalProperties: false
  44. }
  45. ]
  46. }
  47. ],
  48. messages: {
  49. complex: "{{name}} has a complexity of {{complexity}}."
  50. }
  51. },
  52. create(context) {
  53. const option = context.options[0];
  54. let THRESHOLD = 20;
  55. if (typeof option === "object" && Object.prototype.hasOwnProperty.call(option, "maximum") && typeof option.maximum === "number") {
  56. THRESHOLD = option.maximum;
  57. }
  58. if (typeof option === "object" && Object.prototype.hasOwnProperty.call(option, "max") && typeof option.max === "number") {
  59. THRESHOLD = option.max;
  60. }
  61. if (typeof option === "number") {
  62. THRESHOLD = option;
  63. }
  64. //--------------------------------------------------------------------------
  65. // Helpers
  66. //--------------------------------------------------------------------------
  67. // Using a stack to store complexity (handling nested functions)
  68. const fns = [];
  69. /**
  70. * When parsing a new function, store it in our function stack
  71. * @returns {void}
  72. * @private
  73. */
  74. function startFunction() {
  75. fns.push(1);
  76. }
  77. /**
  78. * Evaluate the node at the end of function
  79. * @param {ASTNode} node node to evaluate
  80. * @returns {void}
  81. * @private
  82. */
  83. function endFunction(node) {
  84. const name = lodash.upperFirst(astUtils.getFunctionNameWithKind(node));
  85. const complexity = fns.pop();
  86. if (complexity > THRESHOLD) {
  87. context.report({
  88. node,
  89. messageId: "complex",
  90. data: { name, complexity }
  91. });
  92. }
  93. }
  94. /**
  95. * Increase the complexity of the function in context
  96. * @returns {void}
  97. * @private
  98. */
  99. function increaseComplexity() {
  100. if (fns.length) {
  101. fns[fns.length - 1]++;
  102. }
  103. }
  104. /**
  105. * Increase the switch complexity in context
  106. * @param {ASTNode} node node to evaluate
  107. * @returns {void}
  108. * @private
  109. */
  110. function increaseSwitchComplexity(node) {
  111. // Avoiding `default`
  112. if (node.test) {
  113. increaseComplexity();
  114. }
  115. }
  116. //--------------------------------------------------------------------------
  117. // Public API
  118. //--------------------------------------------------------------------------
  119. return {
  120. FunctionDeclaration: startFunction,
  121. FunctionExpression: startFunction,
  122. ArrowFunctionExpression: startFunction,
  123. "FunctionDeclaration:exit": endFunction,
  124. "FunctionExpression:exit": endFunction,
  125. "ArrowFunctionExpression:exit": endFunction,
  126. CatchClause: increaseComplexity,
  127. ConditionalExpression: increaseComplexity,
  128. LogicalExpression: increaseComplexity,
  129. ForStatement: increaseComplexity,
  130. ForInStatement: increaseComplexity,
  131. ForOfStatement: increaseComplexity,
  132. IfStatement: increaseComplexity,
  133. SwitchCase: increaseSwitchComplexity,
  134. WhileStatement: increaseComplexity,
  135. DoWhileStatement: increaseComplexity
  136. };
  137. }
  138. };