]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/max-statements.md
import 8.23.1 source
[pve-eslint.git] / eslint / docs / src / rules / max-statements.md
1 ---
2 title: max-statements
3 layout: doc
4 rule_type: suggestion
5 related_rules:
6 - complexity
7 - max-depth
8 - max-len
9 - max-lines
10 - max-lines-per-function
11 - max-nested-callbacks
12 - max-params
13 ---
14
15
16 The `max-statements` rule allows you to specify the maximum number of statements allowed in a function.
17
18 ```js
19 function foo() {
20 var bar = 1; // one statement
21 var baz = 2; // two statements
22 var qux = 3; // three statements
23 }
24 ```
25
26 ## Rule Details
27
28 This rule enforces a maximum number of statements allowed in function blocks.
29
30 ## Options
31
32 This rule has a number or object option:
33
34 * `"max"` (default `10`) enforces a maximum number of statements allows in function blocks
35
36 **Deprecated:** The object property `maximum` is deprecated; please use the object property `max` instead.
37
38 This rule has an object option:
39
40 * `"ignoreTopLevelFunctions": true` ignores top-level functions
41
42 ### max
43
44 Examples of **incorrect** code for this rule with the default `{ "max": 10 }` option:
45
46 ::: incorrect
47
48 ```js
49 /*eslint max-statements: ["error", 10]*/
50 /*eslint-env es6*/
51
52 function foo() {
53 var foo1 = 1;
54 var foo2 = 2;
55 var foo3 = 3;
56 var foo4 = 4;
57 var foo5 = 5;
58 var foo6 = 6;
59 var foo7 = 7;
60 var foo8 = 8;
61 var foo9 = 9;
62 var foo10 = 10;
63
64 var foo11 = 11; // Too many.
65 }
66
67 let foo = () => {
68 var foo1 = 1;
69 var foo2 = 2;
70 var foo3 = 3;
71 var foo4 = 4;
72 var foo5 = 5;
73 var foo6 = 6;
74 var foo7 = 7;
75 var foo8 = 8;
76 var foo9 = 9;
77 var foo10 = 10;
78
79 var foo11 = 11; // Too many.
80 };
81 ```
82
83 :::
84
85 Examples of **correct** code for this rule with the default `{ "max": 10 }` option:
86
87 ::: correct
88
89 ```js
90 /*eslint max-statements: ["error", 10]*/
91 /*eslint-env es6*/
92
93 function foo() {
94 var foo1 = 1;
95 var foo2 = 2;
96 var foo3 = 3;
97 var foo4 = 4;
98 var foo5 = 5;
99 var foo6 = 6;
100 var foo7 = 7;
101 var foo8 = 8;
102 var foo9 = 9;
103 var foo10 = 10;
104 return function () {
105
106 // The number of statements in the inner function does not count toward the
107 // statement maximum.
108
109 return 42;
110 };
111 }
112
113 let foo = () => {
114 var foo1 = 1;
115 var foo2 = 2;
116 var foo3 = 3;
117 var foo4 = 4;
118 var foo5 = 5;
119 var foo6 = 6;
120 var foo7 = 7;
121 var foo8 = 8;
122 var foo9 = 9;
123 var foo10 = 10;
124 return function () {
125
126 // The number of statements in the inner function does not count toward the
127 // statement maximum.
128
129 return 42;
130 };
131 }
132 ```
133
134 :::
135
136 Note that this rule does not apply to class static blocks, and that statements in class static blocks do not count as statements in the enclosing function.
137
138 Examples of **correct** code for this rule with `{ "max": 2 }` option:
139
140 ::: correct
141
142 ```js
143 /*eslint max-statements: ["error", 2]*/
144
145 function foo() {
146 let one;
147 let two = class {
148 static {
149 let three;
150 let four;
151 let five;
152 if (six) {
153 let seven;
154 let eight;
155 let nine;
156 }
157 }
158 };
159 }
160 ```
161
162 :::
163
164 ### ignoreTopLevelFunctions
165
166 Examples of additional **correct** code for this rule with the `{ "max": 10 }, { "ignoreTopLevelFunctions": true }` options:
167
168 ::: correct
169
170 ```js
171 /*eslint max-statements: ["error", 10, { "ignoreTopLevelFunctions": true }]*/
172
173 function foo() {
174 var foo1 = 1;
175 var foo2 = 2;
176 var foo3 = 3;
177 var foo4 = 4;
178 var foo5 = 5;
179 var foo6 = 6;
180 var foo7 = 7;
181 var foo8 = 8;
182 var foo9 = 9;
183 var foo10 = 10;
184 var foo11 = 11;
185 }
186 ```
187
188 :::