]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/rules/max-statements-per-line.md
bump version to 8.4.0-3
[pve-eslint.git] / eslint / docs / rules / max-statements-per-line.md
CommitLineData
eb39fafa
DC
1# enforce a maximum number of statements allowed per line (max-statements-per-line)
2
3A 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.
4
5```js
6function foo () { var bar; if (condition) { bar = 1; } else { bar = 2; } return true; } // too many statements
7```
8
9## Rule Details
10
11This rule enforces a maximum number of statements allowed per line.
12
13## Options
14
15### max
16
17The "max" object property is optional (default: 1).
18
19Examples of **incorrect** code for this rule with the default `{ "max": 1 }` option:
20
21```js
22/*eslint max-statements-per-line: ["error", { "max": 1 }]*/
23
24var bar; var baz;
25if (condition) { bar = 1; }
26for (var i = 0; i < length; ++i) { bar = 1; }
27switch (discriminant) { default: break; }
28function foo() { bar = 1; }
29var foo = function foo() { bar = 1; };
30(function foo() { bar = 1; })();
31```
32
33Examples of **correct** code for this rule with the default `{ "max": 1 }` option:
34
35```js
36/*eslint max-statements-per-line: ["error", { "max": 1 }]*/
37
38var bar, baz;
39if (condition) bar = 1;
40for (var i = 0; i < length; ++i);
41switch (discriminant) { default: }
42function foo() { }
43var foo = function foo() { };
44(function foo() { })();
45```
46
47Examples of **incorrect** code for this rule with the `{ "max": 2 }` option:
48
49```js
50/*eslint max-statements-per-line: ["error", { "max": 2 }]*/
51
52var bar; var baz; var qux;
53if (condition) { bar = 1; } else { baz = 2; }
54for (var i = 0; i < length; ++i) { bar = 1; baz = 2; }
55switch (discriminant) { case 'test': break; default: break; }
56function foo() { bar = 1; baz = 2; }
57var foo = function foo() { bar = 1; };
58(function foo() { bar = 1; baz = 2; })();
59```
60
61Examples of **correct** code for this rule with the `{ "max": 2 }` option:
62
63```js
64/*eslint max-statements-per-line: ["error", { "max": 2 }]*/
65
66var bar; var baz;
67if (condition) bar = 1; if (condition) baz = 2;
68for (var i = 0; i < length; ++i) { bar = 1; }
69switch (discriminant) { default: break; }
70function foo() { bar = 1; }
71var foo = function foo() { bar = 1; };
72(function foo() { var bar = 1; })();
73```
74
75## When Not To Use It
76
77You can turn this rule off if you are not concerned with the number of statements on each line.
78
79## Related Rules
80
81* [max-depth](max-depth.md)
82* [max-len](max-len.md)
83* [max-lines](max-lines.md)
84* [max-lines-per-function](max-lines-per-function.md)
85* [max-nested-callbacks](max-nested-callbacks.md)
86* [max-params](max-params.md)
87* [max-statements](max-statements.md)