]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/rules/nonblock-statement-body-position.md
bump version to 8.4.0-3
[pve-eslint.git] / eslint / docs / rules / nonblock-statement-body-position.md
CommitLineData
eb39fafa
DC
1# enforce the location of single-line statements (nonblock-statement-body-position)
2
3When writing `if`, `else`, `while`, `do-while`, and `for` statements, the body can be a single statement instead of a block. It can be useful to enforce a consistent location for these single statements.
4
5For example, some developers avoid writing code like this:
6
7```js
8if (foo)
9 bar();
10```
11
12If another developer attempts to add `baz();` to the `if` statement, they might mistakenly change the code to
13
14```js
15if (foo)
16 bar();
17 baz(); // this line is not in the `if` statement!
18```
19
20To avoid this issue, one might require all single-line `if` statements to appear directly after the conditional, without a linebreak:
21
22```js
23if (foo) bar();
24```
25
26## Rule Details
27
28This rule aims to enforce a consistent location for single-line statements.
29
30Note that this rule does not enforce the usage of single-line statements in general. If you would like to disallow single-line statements, use the [`curly`](/docs/rules/curly.md) rule instead.
31
32### Options
33
34This rule accepts a string option:
35
36* `"beside"` (default) disallows a newline before a single-line statement.
37* `"below"` requires a newline before a single-line statement.
38* `"any"` does not enforce the position of a single-line statement.
39
40Additionally, the rule accepts an optional object option with an `"overrides"` key. This can be used to specify a location for particular statements that override the default. For example:
41
42* `"beside", { "overrides": { "while": "below" } }` requires all single-line statements to appear on the same line as their parent, unless the parent is a `while` statement, in which case the single-line statement must not be on the same line.
43* `"below", { "overrides": { "do": "any" } }` disallows all single-line statements from appearing on the same line as their parent, unless the parent is a `do-while` statement, in which case the position of the single-line statement is not enforced.
44
45Examples of **incorrect** code for this rule with the default `"beside"` option:
46
47```js
48/* eslint nonblock-statement-body-position: ["error", "beside"] */
49
50if (foo)
51 bar();
52else
53 baz();
54
55while (foo)
56 bar();
57
58for (let i = 1; i < foo; i++)
59 bar();
60
61do
62 bar();
63while (foo)
64
65```
66
67Examples of **correct** code for this rule with the default `"beside"` option:
68
69```js
70/* eslint nonblock-statement-body-position: ["error", "beside"] */
71
72if (foo) bar();
73else baz();
74
75while (foo) bar();
76
77for (let i = 1; i < foo; i++) bar();
78
79do bar(); while (foo)
80
81if (foo) { // block statements are always allowed with this rule
82 bar();
83} else {
84 baz();
85}
86```
87
88Examples of **incorrect** code for this rule with the `"below"` option:
89
90```js
91/* eslint nonblock-statement-body-position: ["error", "below"] */
92
93if (foo) bar();
94else baz();
95
96while (foo) bar();
97
98for (let i = 1; i < foo; i++) bar();
99
100do bar(); while (foo)
101```
102
103Examples of **correct** code for this rule with the `"below"` option:
104
105```js
106/* eslint nonblock-statement-body-position: ["error", "below"] */
107
108if (foo)
109 bar();
110else
111 baz();
112
113while (foo)
114 bar();
115
116for (let i = 1; i < foo; i++)
117 bar();
118
119do
120 bar();
121while (foo)
122
123if (foo) {
124 // Although the second `if` statement is on the same line as the `else`, this is a very common
125 // pattern, so it's not checked by this rule.
126} else if (bar) {
127}
128```
129
130Examples of **incorrect** code for this rule with the `"beside", { "overrides": { "while": "below" } }` rule:
131
132```js
133/* eslint nonblock-statement-body-position: ["error", "beside", { "overrides": { "while": "below" } }] */
134
135if (foo)
136 bar();
137
138while (foo) bar();
139```
140
141Examples of **correct** code for this rule with the `"beside", { "overrides": { "while": "below" } }` rule:
142
143```js
144/* eslint nonblock-statement-body-position: ["error", "beside", { "overrides": { "while": "below" } }] */
145
146if (foo) bar();
147
148while (foo)
149 bar();
150```
151
152## When Not To Use It
153
154If you're not concerned about consistent locations of single-line statements, you should not turn on this rule. You can also disable this rule if you're using the `"all"` option for the [`curly`](/docs/rules/curly.md) rule, because this will disallow single-line statements entirely.
155
156## Further Reading
157
158* JSCS: [requireNewlineBeforeSingleStatementsInIf](https://jscs-dev.github.io/rule/requireNewlineBeforeSingleStatementsInIf)