blob: a247039760e9a056de2155e2ad8f45e15362045f [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
20module.exports = {
21 meta: {
22 type: "suggestion",
23
24 docs: {
25 description: "enforce a maximum cyclomatic complexity allowed in a program",
Yang Guo4fd355c2019-09-19 10:59:03 +020026 recommended: false,
27 url: "https://eslint.org/docs/rules/complexity"
28 },
29
30 schema: [
31 {
32 oneOf: [
33 {
34 type: "integer",
35 minimum: 0
36 },
37 {
38 type: "object",
39 properties: {
40 maximum: {
41 type: "integer",
42 minimum: 0
43 },
44 max: {
45 type: "integer",
46 minimum: 0
47 }
48 },
49 additionalProperties: false
50 }
51 ]
52 }
53 ],
54
55 messages: {
56 complex: "{{name}} has a complexity of {{complexity}}. Maximum allowed is {{max}}."
57 }
58 },
59
60 create(context) {
61 const option = context.options[0];
62 let THRESHOLD = 20;
63
64 if (
65 typeof option === "object" &&
66 (Object.prototype.hasOwnProperty.call(option, "maximum") || Object.prototype.hasOwnProperty.call(option, "max"))
67 ) {
68 THRESHOLD = option.maximum || option.max;
69 } else if (typeof option === "number") {
70 THRESHOLD = option;
71 }
72
73 //--------------------------------------------------------------------------
74 // Helpers
75 //--------------------------------------------------------------------------
76
Tim van der Lippe0fb47802021-11-08 16:23:10 +000077 // Using a stack to store complexity per code path
78 const complexities = [];
Yang Guo4fd355c2019-09-19 10:59:03 +020079
80 /**
Tim van der Lippe0fb47802021-11-08 16:23:10 +000081 * Increase the complexity of the code path in context
Yang Guo4fd355c2019-09-19 10:59:03 +020082 * @returns {void}
83 * @private
84 */
85 function increaseComplexity() {
Tim van der Lippe0fb47802021-11-08 16:23:10 +000086 complexities[complexities.length - 1]++;
Yang Guo4fd355c2019-09-19 10:59:03 +020087 }
88
89 //--------------------------------------------------------------------------
90 // Public API
91 //--------------------------------------------------------------------------
92
93 return {
Yang Guo4fd355c2019-09-19 10:59:03 +020094
Tim van der Lippe0fb47802021-11-08 16:23:10 +000095 onCodePathStart() {
96
97 // The initial complexity is 1, representing one execution path in the CodePath
98 complexities.push(1);
99 },
100
101 // Each branching in the code adds 1 to the complexity
Yang Guo4fd355c2019-09-19 10:59:03 +0200102 CatchClause: increaseComplexity,
103 ConditionalExpression: increaseComplexity,
104 LogicalExpression: increaseComplexity,
105 ForStatement: increaseComplexity,
106 ForInStatement: increaseComplexity,
107 ForOfStatement: increaseComplexity,
108 IfStatement: increaseComplexity,
Yang Guo4fd355c2019-09-19 10:59:03 +0200109 WhileStatement: increaseComplexity,
Tim van der Lippeb97da6b2021-02-12 14:32:53 +0000110 DoWhileStatement: increaseComplexity,
111
Tim van der Lippe0fb47802021-11-08 16:23:10 +0000112 // Avoid `default`
113 "SwitchCase[test]": increaseComplexity,
114
115 // Logical assignment operators have short-circuiting behavior
Tim van der Lippeb97da6b2021-02-12 14:32:53 +0000116 AssignmentExpression(node) {
117 if (astUtils.isLogicalAssignmentOperator(node.operator)) {
118 increaseComplexity();
119 }
Tim van der Lippe0fb47802021-11-08 16:23:10 +0000120 },
121
122 onCodePathEnd(codePath, node) {
123 const complexity = complexities.pop();
124
125 /*
126 * This rule only evaluates complexity of functions, so "program" is excluded.
Tim van der Lippe0124c682021-11-23 15:12:10 +0000127 * Class field initializers and class static blocks are implicit functions. Therefore,
128 * they shouldn't contribute to the enclosing function's complexity, but their
129 * own complexity should be evaluated.
Tim van der Lippe0fb47802021-11-08 16:23:10 +0000130 */
131 if (
132 codePath.origin !== "function" &&
Tim van der Lippe0124c682021-11-23 15:12:10 +0000133 codePath.origin !== "class-field-initializer" &&
134 codePath.origin !== "class-static-block"
Tim van der Lippe0fb47802021-11-08 16:23:10 +0000135 ) {
136 return;
137 }
138
139 if (complexity > THRESHOLD) {
Tim van der Lippe0124c682021-11-23 15:12:10 +0000140 let name;
141
142 if (codePath.origin === "class-field-initializer") {
143 name = "class field initializer";
144 } else if (codePath.origin === "class-static-block") {
145 name = "class static block";
146 } else {
147 name = astUtils.getFunctionNameWithKind(node);
148 }
Tim van der Lippe0fb47802021-11-08 16:23:10 +0000149
150 context.report({
151 node,
152 messageId: "complex",
153 data: {
154 name: upperCaseFirst(name),
155 complexity,
156 max: THRESHOLD
157 }
158 });
159 }
Tim van der Lippeb97da6b2021-02-12 14:32:53 +0000160 }
Yang Guo4fd355c2019-09-19 10:59:03 +0200161 };
162
163 }
164};