blob: b833aafc0f7ac6761840f06330ac5cc1df006b1f [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.
127 * Class field initializers are implicit functions. Therefore, they shouldn't contribute
128 * to the enclosing function's complexity, but their own complexity should be evaluated.
129 */
130 if (
131 codePath.origin !== "function" &&
132 codePath.origin !== "class-field-initializer"
133 ) {
134 return;
135 }
136
137 if (complexity > THRESHOLD) {
138 const name = codePath.origin === "class-field-initializer"
139 ? "class field initializer"
140 : astUtils.getFunctionNameWithKind(node);
141
142 context.report({
143 node,
144 messageId: "complex",
145 data: {
146 name: upperCaseFirst(name),
147 complexity,
148 max: THRESHOLD
149 }
150 });
151 }
Tim van der Lippeb97da6b2021-02-12 14:32:53 +0000152 }
Yang Guo4fd355c2019-09-19 10:59:03 +0200153 };
154
155 }
156};