]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/rules/complexity.md
7955d1a285415a73e273deefa235affc9565f819
[pve-eslint.git] / eslint / docs / rules / complexity.md
1 # Limit Cyclomatic Complexity (complexity)
2
3 Cyclomatic complexity measures the number of linearly independent paths through a program's source code. This rule allows setting a cyclomatic complexity threshold.
4
5 ```js
6 function a(x) {
7 if (true) {
8 return x; // 1st path
9 } else if (false) {
10 return x+1; // 2nd path
11 } else {
12 return 4; // 3rd path
13 }
14 }
15 ```
16
17 ## Rule Details
18
19 This rule is aimed at reducing code complexity by capping the amount of cyclomatic complexity allowed in a program. As such, it will warn when the cyclomatic complexity crosses the configured threshold (default is `20`).
20
21 Examples of **incorrect** code for a maximum of 2:
22
23 ```js
24 /*eslint complexity: ["error", 2]*/
25
26 function a(x) {
27 if (true) {
28 return x;
29 } else if (false) {
30 return x+1;
31 } else {
32 return 4; // 3rd path
33 }
34 }
35
36 function b() {
37 foo ||= 1;
38 bar &&= 1;
39 }
40 ```
41
42 Examples of **correct** code for a maximum of 2:
43
44 ```js
45 /*eslint complexity: ["error", 2]*/
46
47 function a(x) {
48 if (true) {
49 return x;
50 } else {
51 return 4;
52 }
53 }
54
55 function b() {
56 foo ||= 1;
57 }
58 ```
59
60 Class field initializers and class static blocks are implicit functions. Therefore, their complexity is calculated separately for each initializer and each static block, and it doesn't contribute to the complexity of the enclosing code.
61
62 Examples of additional **incorrect** code for a maximum of 2:
63
64 ```js
65 /*eslint complexity: ["error", 2]*/
66
67 class C {
68 x = a || b || c; // this initializer has complexity = 3
69 }
70
71 class D { // this static block has complexity = 3
72 static {
73 if (foo) {
74 bar = baz || qux;
75 }
76 }
77 }
78 ```
79
80 Examples of additional **correct** code for a maximum of 2:
81
82 ```js
83 /*eslint complexity: ["error", 2]*/
84
85 function foo() { // this function has complexity = 1
86 class C {
87 x = a + b; // this initializer has complexity = 1
88 y = c || d; // this initializer has complexity = 2
89 z = e && f; // this initializer has complexity = 2
90
91 static p = g || h; // this initializer has complexity = 2
92 static q = i ? j : k; // this initializer has complexity = 2
93
94 static { // this static block has complexity = 2
95 if (foo) {
96 baz = bar;
97 }
98 }
99
100 static { // this static block has complexity = 2
101 qux = baz || quux;
102 }
103 }
104 }
105 ```
106
107 ## Options
108
109 Optionally, you may specify a `max` object property:
110
111 ```json
112 "complexity": ["error", 2]
113 ```
114
115 is equivalent to
116
117 ```json
118 "complexity": ["error", { "max": 2 }]
119 ```
120
121 **Deprecated:** the object property `maximum` is deprecated. Please use the property `max` instead.
122
123 ## When Not To Use It
124
125 If you can't determine an appropriate complexity limit for your code, then it's best to disable this rule.
126
127 ## Further Reading
128
129 * [Cyclomatic Complexity](https://en.wikipedia.org/wiki/Cyclomatic_complexity)
130 * [Complexity Analysis of JavaScript Code](https://ariya.io/2012/12/complexity-analysis-of-javascript-code)
131 * [More about Complexity in JavaScript](https://craftsmanshipforsoftware.com/2015/05/25/complexity-for-javascript/)
132 * [About Complexity](https://web.archive.org/web/20160808115119/http://jscomplexity.org/complexity)
133 * [Discussion about Complexity in ESLint and more links](https://github.com/eslint/eslint/issues/4808#issuecomment-167795140)
134
135 ## Related Rules
136
137 * [max-depth](max-depth.md)
138 * [max-len](max-len.md)
139 * [max-lines](max-lines.md)
140 * [max-lines-per-function](max-lines-per-function.md)
141 * [max-nested-callbacks](max-nested-callbacks.md)
142 * [max-params](max-params.md)
143 * [max-statements](max-statements.md)