]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | # enforce a maximum number of statements allowed per line (max-statements-per-line) |
2 | ||
3 | 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. | |
4 | ||
5 | ```js | |
6 | function foo () { var bar; if (condition) { bar = 1; } else { bar = 2; } return true; } // too many statements | |
7 | ``` | |
8 | ||
9 | ## Rule Details | |
10 | ||
11 | This rule enforces a maximum number of statements allowed per line. | |
12 | ||
13 | ## Options | |
14 | ||
15 | ### max | |
16 | ||
17 | The "max" object property is optional (default: 1). | |
18 | ||
19 | Examples 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 | ||
24 | var bar; var baz; | |
25 | if (condition) { bar = 1; } | |
26 | for (var i = 0; i < length; ++i) { bar = 1; } | |
27 | switch (discriminant) { default: break; } | |
28 | function foo() { bar = 1; } | |
29 | var foo = function foo() { bar = 1; }; | |
30 | (function foo() { bar = 1; })(); | |
31 | ``` | |
32 | ||
33 | Examples 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 | ||
38 | var bar, baz; | |
39 | if (condition) bar = 1; | |
40 | for (var i = 0; i < length; ++i); | |
41 | switch (discriminant) { default: } | |
42 | function foo() { } | |
43 | var foo = function foo() { }; | |
44 | (function foo() { })(); | |
45 | ``` | |
46 | ||
47 | Examples of **incorrect** code for this rule with the `{ "max": 2 }` option: | |
48 | ||
49 | ```js | |
50 | /*eslint max-statements-per-line: ["error", { "max": 2 }]*/ | |
51 | ||
52 | var bar; var baz; var qux; | |
53 | if (condition) { bar = 1; } else { baz = 2; } | |
54 | for (var i = 0; i < length; ++i) { bar = 1; baz = 2; } | |
55 | switch (discriminant) { case 'test': break; default: break; } | |
56 | function foo() { bar = 1; baz = 2; } | |
57 | var foo = function foo() { bar = 1; }; | |
58 | (function foo() { bar = 1; baz = 2; })(); | |
59 | ``` | |
60 | ||
61 | Examples of **correct** code for this rule with the `{ "max": 2 }` option: | |
62 | ||
63 | ```js | |
64 | /*eslint max-statements-per-line: ["error", { "max": 2 }]*/ | |
65 | ||
66 | var bar; var baz; | |
67 | if (condition) bar = 1; if (condition) baz = 2; | |
68 | for (var i = 0; i < length; ++i) { bar = 1; } | |
69 | switch (discriminant) { default: break; } | |
70 | function foo() { bar = 1; } | |
71 | var foo = function foo() { bar = 1; }; | |
72 | (function foo() { var bar = 1; })(); | |
73 | ``` | |
74 | ||
75 | ## When Not To Use It | |
76 | ||
77 | You 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) |