项目原始demo,不改动
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
Den här utvecklingskatalogen är arkiverad. Du kan se filer och klona katalogen, men inte öppna ärenden eller genomföra push- eller pull-förfrågningar.
 
 
 
 

160 rader
4.9 KiB

  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("../ast-utils");
  12. //------------------------------------------------------------------------------
  13. // Rule Definition
  14. //------------------------------------------------------------------------------
  15. module.exports = {
  16. meta: {
  17. docs: {
  18. description: "enforce a maximum cyclomatic complexity allowed in a program",
  19. category: "Best Practices",
  20. recommended: false,
  21. url: "https://eslint.org/docs/rules/complexity"
  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. messages: {
  48. complex: "{{name}} has a complexity of {{complexity}}."
  49. }
  50. },
  51. create(context) {
  52. const option = context.options[0];
  53. let THRESHOLD = 20;
  54. if (typeof option === "object" && option.hasOwnProperty("maximum") && typeof option.maximum === "number") {
  55. THRESHOLD = option.maximum;
  56. }
  57. if (typeof option === "object" && option.hasOwnProperty("max") && typeof option.max === "number") {
  58. THRESHOLD = option.max;
  59. }
  60. 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 = lodash.upperFirst(astUtils.getFunctionNameWithKind(node));
  84. const complexity = fns.pop();
  85. if (complexity > THRESHOLD) {
  86. context.report({
  87. node,
  88. messageId: "complex",
  89. data: { name, complexity }
  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. };
  136. }
  137. };