]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/max-statements-per-line.md
efa81608b62745d7f47833f9d7bbf207938110cc
[pve-eslint.git] / eslint / docs / src / rules / max-statements-per-line.md
1 ---
2 title: max-statements-per-line
3 layout: doc
4 rule_type: layout
5 related_rules:
6 - max-depth
7 - max-len
8 - max-lines
9 - max-lines-per-function
10 - max-nested-callbacks
11 - max-params
12 - max-statements
13 ---
14
15
16 A line of code containing too many statements can be difficult to read. Code is generally read from the top down, especially when scanning, so limiting the number of statements allowed on a single line can be very beneficial for readability and maintainability.
17
18 ```js
19 function foo () { var bar; if (condition) { bar = 1; } else { bar = 2; } return true; } // too many statements
20 ```
21
22 ## Rule Details
23
24 This rule enforces a maximum number of statements allowed per line.
25
26 ## Options
27
28 ### max
29
30 The "max" object property is optional (default: 1).
31
32 Examples of **incorrect** code for this rule with the default `{ "max": 1 }` option:
33
34 ::: incorrect
35
36 ```js
37 /*eslint max-statements-per-line: ["error", { "max": 1 }]*/
38
39 var bar; var baz;
40 if (condition) { bar = 1; }
41 for (var i = 0; i < length; ++i) { bar = 1; }
42 switch (discriminant) { default: break; }
43 function foo() { bar = 1; }
44 var foo = function foo() { bar = 1; };
45 (function foo() { bar = 1; })();
46 ```
47
48 :::
49
50 Examples of **correct** code for this rule with the default `{ "max": 1 }` option:
51
52 ::: correct
53
54 ```js
55 /*eslint max-statements-per-line: ["error", { "max": 1 }]*/
56
57 var bar, baz;
58 if (condition) bar = 1;
59 for (var i = 0; i < length; ++i);
60 switch (discriminant) { default: }
61 function foo() { }
62 var foo = function foo() { };
63 (function foo() { })();
64 ```
65
66 :::
67
68 Examples of **incorrect** code for this rule with the `{ "max": 2 }` option:
69
70 ::: incorrect
71
72 ```js
73 /*eslint max-statements-per-line: ["error", { "max": 2 }]*/
74
75 var bar; var baz; var qux;
76 if (condition) { bar = 1; } else { baz = 2; }
77 for (var i = 0; i < length; ++i) { bar = 1; baz = 2; }
78 switch (discriminant) { case 'test': break; default: break; }
79 function foo() { bar = 1; baz = 2; }
80 var foo = function foo() { bar = 1; };
81 (function foo() { bar = 1; baz = 2; })();
82 ```
83
84 :::
85
86 Examples of **correct** code for this rule with the `{ "max": 2 }` option:
87
88 ::: correct
89
90 ```js
91 /*eslint max-statements-per-line: ["error", { "max": 2 }]*/
92
93 var bar; var baz;
94 if (condition) bar = 1; if (condition) baz = 2;
95 for (var i = 0; i < length; ++i) { bar = 1; }
96 switch (discriminant) { default: break; }
97 function foo() { bar = 1; }
98 var foo = function foo() { bar = 1; };
99 (function foo() { var bar = 1; })();
100 ```
101
102 :::
103
104 ## When Not To Use It
105
106 You can turn this rule off if you are not concerned with the number of statements on each line.