]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | # enforce a maximum number of statements allowed in function blocks (max-statements) |
2 | ||
3 | The `max-statements` rule allows you to specify the maximum number of statements allowed in a function. | |
4 | ||
5 | ```js | |
6 | function foo() { | |
7 | var bar = 1; // one statement | |
8 | var baz = 2; // two statements | |
9 | var qux = 3; // three statements | |
10 | } | |
11 | ``` | |
12 | ||
13 | ## Rule Details | |
14 | ||
15 | This rule enforces a maximum number of statements allowed in function blocks. | |
16 | ||
17 | ## Options | |
18 | ||
19 | This rule has a number or object option: | |
20 | ||
21 | * `"max"` (default `10`) enforces a maximum number of statements allows in function blocks | |
22 | ||
23 | **Deprecated:** The object property `maximum` is deprecated; please use the object property `max` instead. | |
24 | ||
25 | This rule has an object option: | |
26 | ||
27 | * `"ignoreTopLevelFunctions": true` ignores top-level functions | |
28 | ||
29 | ### max | |
30 | ||
31 | Examples of **incorrect** code for this rule with the default `{ "max": 10 }` option: | |
32 | ||
33 | ```js | |
34 | /*eslint max-statements: ["error", 10]*/ | |
35 | /*eslint-env es6*/ | |
36 | ||
37 | function foo() { | |
38 | var foo1 = 1; | |
39 | var foo2 = 2; | |
40 | var foo3 = 3; | |
41 | var foo4 = 4; | |
42 | var foo5 = 5; | |
43 | var foo6 = 6; | |
44 | var foo7 = 7; | |
45 | var foo8 = 8; | |
46 | var foo9 = 9; | |
47 | var foo10 = 10; | |
48 | ||
49 | var foo11 = 11; // Too many. | |
50 | } | |
51 | ||
52 | let 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 | ||
68 | Examples of **correct** code for this rule with the default `{ "max": 10 }` option: | |
69 | ||
70 | ```js | |
71 | /*eslint max-statements: ["error", 10]*/ | |
72 | /*eslint-env es6*/ | |
73 | ||
74 | function foo() { | |
75 | var foo1 = 1; | |
76 | var foo2 = 2; | |
77 | var foo3 = 3; | |
78 | var foo4 = 4; | |
79 | var foo5 = 5; | |
80 | var foo6 = 6; | |
81 | var foo7 = 7; | |
82 | var foo8 = 8; | |
83 | var foo9 = 9; | |
84 | var foo10 = 10; | |
85 | return function () { | |
86 | ||
87 | // The number of statements in the inner function does not count toward the | |
88 | // statement maximum. | |
89 | ||
90 | return 42; | |
91 | }; | |
92 | } | |
93 | ||
94 | let foo = () => { | |
95 | var foo1 = 1; | |
96 | var foo2 = 2; | |
97 | var foo3 = 3; | |
98 | var foo4 = 4; | |
99 | var foo5 = 5; | |
100 | var foo6 = 6; | |
101 | var foo7 = 7; | |
102 | var foo8 = 8; | |
103 | var foo9 = 9; | |
104 | var foo10 = 10; | |
105 | return function () { | |
106 | ||
107 | // The number of statements in the inner function does not count toward the | |
108 | // statement maximum. | |
109 | ||
110 | return 42; | |
111 | }; | |
112 | } | |
113 | ``` | |
114 | ||
609c276f TL |
115 | 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. |
116 | ||
117 | Examples of **correct** code for this rule with `{ "max": 2 }` option: | |
118 | ||
119 | ```js | |
120 | /*eslint max-statements: ["error", 2]*/ | |
121 | ||
122 | function foo() { | |
123 | let one; | |
124 | let two = class { | |
125 | static { | |
126 | let three; | |
127 | let four; | |
128 | let five; | |
129 | if (six) { | |
130 | let seven; | |
131 | let eight; | |
132 | let nine; | |
133 | } | |
134 | } | |
135 | }; | |
136 | } | |
137 | ``` | |
138 | ||
eb39fafa DC |
139 | ### ignoreTopLevelFunctions |
140 | ||
141 | Examples of additional **correct** code for this rule with the `{ "max": 10 }, { "ignoreTopLevelFunctions": true }` options: | |
142 | ||
143 | ```js | |
144 | /*eslint max-statements: ["error", 10, { "ignoreTopLevelFunctions": true }]*/ | |
145 | ||
146 | function foo() { | |
147 | var foo1 = 1; | |
148 | var foo2 = 2; | |
149 | var foo3 = 3; | |
150 | var foo4 = 4; | |
151 | var foo5 = 5; | |
152 | var foo6 = 6; | |
153 | var foo7 = 7; | |
154 | var foo8 = 8; | |
155 | var foo9 = 9; | |
156 | var foo10 = 10; | |
157 | var foo11 = 11; | |
158 | } | |
159 | ``` | |
160 | ||
161 | ## Related Rules | |
162 | ||
163 | * [complexity](complexity.md) | |
164 | * [max-depth](max-depth.md) | |
165 | * [max-len](max-len.md) | |
166 | * [max-lines](max-lines.md) | |
167 | * [max-lines-per-function](max-lines-per-function.md) | |
168 | * [max-nested-callbacks](max-nested-callbacks.md) | |
169 | * [max-params](max-params.md) |