complexity.js 5.6 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. /** @type {import('../shared/types').Rule} */
  16. module.exports = {
  17. meta: {
  18. type: "suggestion",
  19. docs: {
  20. description: "Enforce a maximum cyclomatic complexity allowed in a program",
  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 per code path
  67. const complexities = [];
  68. /**
  69. * Increase the complexity of the code path in context
  70. * @returns {void}
  71. * @private
  72. */
  73. function increaseComplexity() {
  74. complexities[complexities.length - 1]++;
  75. }
  76. //--------------------------------------------------------------------------
  77. // Public API
  78. //--------------------------------------------------------------------------
  79. return {
  80. onCodePathStart() {
  81. // The initial complexity is 1, representing one execution path in the CodePath
  82. complexities.push(1);
  83. },
  84. // Each branching in the code adds 1 to the complexity
  85. CatchClause: increaseComplexity,
  86. ConditionalExpression: increaseComplexity,
  87. LogicalExpression: increaseComplexity,
  88. ForStatement: increaseComplexity,
  89. ForInStatement: increaseComplexity,
  90. ForOfStatement: increaseComplexity,
  91. IfStatement: increaseComplexity,
  92. WhileStatement: increaseComplexity,
  93. DoWhileStatement: increaseComplexity,
  94. // Avoid `default`
  95. "SwitchCase[test]": increaseComplexity,
  96. // Logical assignment operators have short-circuiting behavior
  97. AssignmentExpression(node) {
  98. if (astUtils.isLogicalAssignmentOperator(node.operator)) {
  99. increaseComplexity();
  100. }
  101. },
  102. onCodePathEnd(codePath, node) {
  103. const complexity = complexities.pop();
  104. /*
  105. * This rule only evaluates complexity of functions, so "program" is excluded.
  106. * Class field initializers and class static blocks are implicit functions. Therefore,
  107. * they shouldn't contribute to the enclosing function's complexity, but their
  108. * own complexity should be evaluated.
  109. */
  110. if (
  111. codePath.origin !== "function" &&
  112. codePath.origin !== "class-field-initializer" &&
  113. codePath.origin !== "class-static-block"
  114. ) {
  115. return;
  116. }
  117. if (complexity > THRESHOLD) {
  118. let name;
  119. if (codePath.origin === "class-field-initializer") {
  120. name = "class field initializer";
  121. } else if (codePath.origin === "class-static-block") {
  122. name = "class static block";
  123. } else {
  124. name = astUtils.getFunctionNameWithKind(node);
  125. }
  126. context.report({
  127. node,
  128. messageId: "complex",
  129. data: {
  130. name: upperCaseFirst(name),
  131. complexity,
  132. max: THRESHOLD
  133. }
  134. });
  135. }
  136. }
  137. };
  138. }
  139. };