]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/nonblock-statement-body-position.md
import 8.41.0 source
[pve-eslint.git] / eslint / docs / src / rules / nonblock-statement-body-position.md
1 ---
2 title: nonblock-statement-body-position
3 rule_type: layout
4 further_reading:
5 - https://jscs-dev.github.io/rule/requireNewlineBeforeSingleStatementsInIf
6 ---
7
8
9
10 When 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.
11
12 For example, some developers avoid writing code like this:
13
14 ```js
15 if (foo)
16 bar();
17 ```
18
19 If another developer attempts to add `baz();` to the `if` statement, they might mistakenly change the code to
20
21 ```js
22 if (foo)
23 bar();
24 baz(); // this line is not in the `if` statement!
25 ```
26
27 To avoid this issue, one might require all single-line `if` statements to appear directly after the conditional, without a linebreak:
28
29 ```js
30 if (foo) bar();
31 ```
32
33 ## Rule Details
34
35 This rule aims to enforce a consistent location for single-line statements.
36
37 Note 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`](curly) rule instead.
38
39 ### Options
40
41 This rule accepts a string option:
42
43 * `"beside"` (default) disallows a newline before a single-line statement.
44 * `"below"` requires a newline before a single-line statement.
45 * `"any"` does not enforce the position of a single-line statement.
46
47 Additionally, 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:
48
49 * `"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.
50 * `"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.
51
52 Examples of **incorrect** code for this rule with the default `"beside"` option:
53
54 ::: incorrect
55
56 ```js
57 /* eslint nonblock-statement-body-position: ["error", "beside"] */
58
59 if (foo)
60 bar();
61 else
62 baz();
63
64 while (foo)
65 bar();
66
67 for (let i = 1; i < foo; i++)
68 bar();
69
70 do
71 bar();
72 while (foo)
73
74 ```
75
76 :::
77
78 Examples of **correct** code for this rule with the default `"beside"` option:
79
80 ::: correct
81
82 ```js
83 /* eslint nonblock-statement-body-position: ["error", "beside"] */
84
85 if (foo) bar();
86 else baz();
87
88 while (foo) bar();
89
90 for (let i = 1; i < foo; i++) bar();
91
92 do bar(); while (foo)
93
94 if (foo) { // block statements are always allowed with this rule
95 bar();
96 } else {
97 baz();
98 }
99 ```
100
101 :::
102
103 Examples of **incorrect** code for this rule with the `"below"` option:
104
105 ::: incorrect
106
107 ```js
108 /* eslint nonblock-statement-body-position: ["error", "below"] */
109
110 if (foo) bar();
111 else baz();
112
113 while (foo) bar();
114
115 for (let i = 1; i < foo; i++) bar();
116
117 do bar(); while (foo)
118 ```
119
120 :::
121
122 Examples of **correct** code for this rule with the `"below"` option:
123
124 ::: correct
125
126 ```js
127 /* eslint nonblock-statement-body-position: ["error", "below"] */
128
129 if (foo)
130 bar();
131 else
132 baz();
133
134 while (foo)
135 bar();
136
137 for (let i = 1; i < foo; i++)
138 bar();
139
140 do
141 bar();
142 while (foo)
143
144 if (foo) {
145 // Although the second `if` statement is on the same line as the `else`, this is a very common
146 // pattern, so it's not checked by this rule.
147 } else if (bar) {
148 }
149 ```
150
151 :::
152
153 Examples of **incorrect** code for this rule with the `"beside", { "overrides": { "while": "below" } }` rule:
154
155 ::: incorrect
156
157 ```js
158 /* eslint nonblock-statement-body-position: ["error", "beside", { "overrides": { "while": "below" } }] */
159
160 if (foo)
161 bar();
162
163 while (foo) bar();
164 ```
165
166 :::
167
168 Examples of **correct** code for this rule with the `"beside", { "overrides": { "while": "below" } }` rule:
169
170 ::: correct
171
172 ```js
173 /* eslint nonblock-statement-body-position: ["error", "beside", { "overrides": { "while": "below" } }] */
174
175 if (foo) bar();
176
177 while (foo)
178 bar();
179 ```
180
181 :::
182
183 ## When Not To Use It
184
185 If 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`](curly) rule, because this will disallow single-line statements entirely.