complexity.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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, while, try, switch/case,
  4. * @author Patrick Brosset
  5. */
  6. "use strict";
  7. //------------------------------------------------------------------------------
  8. // Requirements
  9. //------------------------------------------------------------------------------
  10. const astUtils = require("./utils/ast-utils");
  11. const { upperCaseFirst } = require("../shared/string-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}}. Maximum allowed is {{max}}."
  50. }
  51. },
  52. create(context) {
  53. const option = context.options[0];
  54. let THRESHOLD = 20;
  55. if (
  56. typeof option === "object" &&
  57. (Object.prototype.hasOwnProperty.call(option, "maximum") || Object.prototype.hasOwnProperty.call(option, "max"))
  58. ) {
  59. THRESHOLD = option.maximum || option.max;
  60. } else if (typeof option === "number") {
  61. THRESHOLD = option;
  62. }
  63. //--------------------------------------------------------------------------
  64. // Helpers
  65. //--------------------------------------------------------------------------
  66. // Using a stack to store complexity (handling nested functions)
  67. const fns = [];
  68. /**
  69. * When parsing a new function, store it in our function stack
  70. * @returns {void}
  71. * @private
  72. */
  73. function startFunction() {
  74. fns.push(1);
  75. }
  76. /**
  77. * Evaluate the node at the end of function
  78. * @param {ASTNode} node node to evaluate
  79. * @returns {void}
  80. * @private
  81. */
  82. function endFunction(node) {
  83. const name = upperCaseFirst(astUtils.getFunctionNameWithKind(node));
  84. const complexity = fns.pop();
  85. if (complexity > THRESHOLD) {
  86. context.report({
  87. node,
  88. messageId: "complex",
  89. data: { name, complexity, max: THRESHOLD }
  90. });
  91. }
  92. }
  93. /**
  94. * Increase the complexity of the function in context
  95. * @returns {void}
  96. * @private
  97. */
  98. function increaseComplexity() {
  99. if (fns.length) {
  100. fns[fns.length - 1]++;
  101. }
  102. }
  103. /**
  104. * Increase the switch complexity in context
  105. * @param {ASTNode} node node to evaluate
  106. * @returns {void}
  107. * @private
  108. */
  109. function increaseSwitchComplexity(node) {
  110. // Avoiding `default`
  111. if (node.test) {
  112. increaseComplexity();
  113. }
  114. }
  115. //--------------------------------------------------------------------------
  116. // Public API
  117. //--------------------------------------------------------------------------
  118. return {
  119. FunctionDeclaration: startFunction,
  120. FunctionExpression: startFunction,
  121. ArrowFunctionExpression: startFunction,
  122. "FunctionDeclaration:exit": endFunction,
  123. "FunctionExpression:exit": endFunction,
  124. "ArrowFunctionExpression:exit": endFunction,
  125. CatchClause: increaseComplexity,
  126. ConditionalExpression: increaseComplexity,
  127. LogicalExpression: increaseComplexity,
  128. ForStatement: increaseComplexity,
  129. ForInStatement: increaseComplexity,
  130. ForOfStatement: increaseComplexity,
  131. IfStatement: increaseComplexity,
  132. SwitchCase: increaseSwitchComplexity,
  133. WhileStatement: increaseComplexity,
  134. DoWhileStatement: increaseComplexity,
  135. AssignmentExpression(node) {
  136. if (astUtils.isLogicalAssignmentOperator(node.operator)) {
  137. increaseComplexity();
  138. }
  139. }
  140. };
  141. }
  142. };