blob: b2355556af9d24f824bd82de5211fa317c61675f [file] [log] [blame]
Yang Guo4fd355c2019-09-19 10:59:03 +02001/**
2 * @fileoverview Counts the cyclomatic complexity of each function of the script. See http://en.wikipedia.org/wiki/Cyclomatic_complexity.
Tim van der Lippe16aca392020-11-13 11:37:13 +00003 * Counts the number of if, conditional, for, while, try, switch/case,
Yang Guo4fd355c2019-09-19 10:59:03 +02004 * @author Patrick Brosset
5 */
6
7"use strict";
8
9//------------------------------------------------------------------------------
10// Requirements
11//------------------------------------------------------------------------------
12
Yang Guo4fd355c2019-09-19 10:59:03 +020013const astUtils = require("./utils/ast-utils");
Simon Zünd52e20202021-06-16 08:34:28 +020014const { upperCaseFirst } = require("../shared/string-utils");
Yang Guo4fd355c2019-09-19 10:59:03 +020015
16//------------------------------------------------------------------------------
17// Rule Definition
18//------------------------------------------------------------------------------
19
Tim van der Lippe0ceb4652022-01-06 14:23:36 +010020/** @type {import('../shared/types').Rule} */
Yang Guo4fd355c2019-09-19 10:59:03 +020021module.exports = {
22 meta: {
23 type: "suggestion",
24
25 docs: {
26 description: "enforce a maximum cyclomatic complexity allowed in a program",
Yang Guo4fd355c2019-09-19 10:59:03 +020027 recommended: false,
28 url: "https://eslint.org/docs/rules/complexity"
29 },
30
31 schema: [
32 {
33 oneOf: [
34 {
35 type: "integer",
36 minimum: 0
37 },
38 {
39 type: "object",
40 properties: {
41 maximum: {
42 type: "integer",
43 minimum: 0
44 },
45 max: {
46 type: "integer",
47 minimum: 0
48 }
49 },
50 additionalProperties: false
51 }
52 ]
53 }
54 ],
55
56 messages: {
57 complex: "{{name}} has a complexity of {{complexity}}. Maximum allowed is {{max}}."
58 }
59 },
60
61 create(context) {
62 const option = context.options[0];
63 let THRESHOLD = 20;
64
65 if (
66 typeof option === "object" &&
67 (Object.prototype.hasOwnProperty.call(option, "maximum") || Object.prototype.hasOwnProperty.call(option, "max"))
68 ) {
69 THRESHOLD = option.maximum || option.max;
70 } else if (typeof option === "number") {
71 THRESHOLD = option;
72 }
73
74 //--------------------------------------------------------------------------
75 // Helpers
76 //--------------------------------------------------------------------------
77
Tim van der Lippe0fb47802021-11-08 16:23:10 +000078 // Using a stack to store complexity per code path
79 const complexities = [];
Yang Guo4fd355c2019-09-19 10:59:03 +020080
81 /**
Tim van der Lippe0fb47802021-11-08 16:23:10 +000082 * Increase the complexity of the code path in context
Yang Guo4fd355c2019-09-19 10:59:03 +020083 * @returns {void}
84 * @private
85 */
86 function increaseComplexity() {
Tim van der Lippe0fb47802021-11-08 16:23:10 +000087 complexities[complexities.length - 1]++;
Yang Guo4fd355c2019-09-19 10:59:03 +020088 }
89
90 //--------------------------------------------------------------------------
91 // Public API
92 //--------------------------------------------------------------------------
93
94 return {
Yang Guo4fd355c2019-09-19 10:59:03 +020095
Tim van der Lippe0fb47802021-11-08 16:23:10 +000096 onCodePathStart() {
97
98 // The initial complexity is 1, representing one execution path in the CodePath
99 complexities.push(1);
100 },
101
102 // Each branching in the code adds 1 to the complexity
Yang Guo4fd355c2019-09-19 10:59:03 +0200103 CatchClause: increaseComplexity,
104 ConditionalExpression: increaseComplexity,
105 LogicalExpression: increaseComplexity,
106 ForStatement: increaseComplexity,
107 ForInStatement: increaseComplexity,
108 ForOfStatement: increaseComplexity,
109 IfStatement: increaseComplexity,
Yang Guo4fd355c2019-09-19 10:59:03 +0200110 WhileStatement: increaseComplexity,
Tim van der Lippeb97da6b2021-02-12 14:32:53 +0000111 DoWhileStatement: increaseComplexity,
112
Tim van der Lippe0fb47802021-11-08 16:23:10 +0000113 // Avoid `default`
114 "SwitchCase[test]": increaseComplexity,
115
116 // Logical assignment operators have short-circuiting behavior
Tim van der Lippeb97da6b2021-02-12 14:32:53 +0000117 AssignmentExpression(node) {
118 if (astUtils.isLogicalAssignmentOperator(node.operator)) {
119 increaseComplexity();
120 }
Tim van der Lippe0fb47802021-11-08 16:23:10 +0000121 },
122
123 onCodePathEnd(codePath, node) {
124 const complexity = complexities.pop();
125
126 /*
127 * This rule only evaluates complexity of functions, so "program" is excluded.
Tim van der Lippe0124c682021-11-23 15:12:10 +0000128 * Class field initializers and class static blocks are implicit functions. Therefore,
129 * they shouldn't contribute to the enclosing function's complexity, but their
130 * own complexity should be evaluated.
Tim van der Lippe0fb47802021-11-08 16:23:10 +0000131 */
132 if (
133 codePath.origin !== "function" &&
Tim van der Lippe0124c682021-11-23 15:12:10 +0000134 codePath.origin !== "class-field-initializer" &&
135 codePath.origin !== "class-static-block"
Tim van der Lippe0fb47802021-11-08 16:23:10 +0000136 ) {
137 return;
138 }
139
140 if (complexity > THRESHOLD) {
Tim van der Lippe0124c682021-11-23 15:12:10 +0000141 let name;
142
143 if (codePath.origin === "class-field-initializer") {
144 name = "class field initializer";
145 } else if (codePath.origin === "class-static-block") {
146 name = "class static block";
147 } else {
148 name = astUtils.getFunctionNameWithKind(node);
149 }
Tim van der Lippe0fb47802021-11-08 16:23:10 +0000150
151 context.report({
152 node,
153 messageId: "complex",
154 data: {
155 name: upperCaseFirst(name),
156 complexity,
157 max: THRESHOLD
158 }
159 });
160 }
Tim van der Lippeb97da6b2021-02-12 14:32:53 +0000161 }
Yang Guo4fd355c2019-09-19 10:59:03 +0200162 };
163
164 }
165};