]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/src/rules/brace-style.md
import 8.23.1 source
[pve-eslint.git] / eslint / docs / src / rules / brace-style.md
1 ---
2 title: brace-style
3 layout: doc
4 rule_type: layout
5 related_rules:
6 - block-spacing
7 - space-before-blocks
8 further_reading:
9 - https://en.wikipedia.org/wiki/Indent_style
10 ---
11
12
13
14 Brace style is closely related to [indent style](https://en.wikipedia.org/wiki/Indent_style) in programming and describes the placement of braces relative to their control statement and body. There are probably a dozen, if not more, brace styles in the world.
15
16 The *one true brace style* is one of the most common brace styles in JavaScript, in which the opening brace of a block is placed on the same line as its corresponding statement or declaration. For example:
17
18 ```js
19 if (foo) {
20 bar();
21 } else {
22 baz();
23 }
24 ```
25
26 One common variant of one true brace style is called Stroustrup, in which the `else` statements in an `if-else` construct, as well as `catch` and `finally`, must be on its own line after the preceding closing brace. For example:
27
28 ```js
29 if (foo) {
30 bar();
31 }
32 else {
33 baz();
34 }
35 ```
36
37 Another style is called [Allman](https://en.wikipedia.org/wiki/Indent_style#Allman_style), in which all the braces are expected to be on their own lines without any extra indentation. For example:
38
39 ```js
40 if (foo)
41 {
42 bar();
43 }
44 else
45 {
46 baz();
47 }
48 ```
49
50 While no style is considered better than the other, most developers agree that having a consistent style throughout a project is important for its long-term maintainability.
51
52 ## Rule Details
53
54 This rule enforces consistent brace style for blocks.
55
56 ## Options
57
58 This rule has a string option:
59
60 * `"1tbs"` (default) enforces one true brace style
61 * `"stroustrup"` enforces Stroustrup style
62 * `"allman"` enforces Allman style
63
64 This rule has an object option for an exception:
65
66 * `"allowSingleLine": true` (default `false`) allows the opening and closing braces for a block to be on the *same* line
67
68 ### 1tbs
69
70 Examples of **incorrect** code for this rule with the default `"1tbs"` option:
71
72 :::incorrect
73
74 ```js
75 /*eslint brace-style: "error"*/
76
77 function foo()
78 {
79 return true;
80 }
81
82 if (foo)
83 {
84 bar();
85 }
86
87 try
88 {
89 somethingRisky();
90 } catch(e)
91 {
92 handleError();
93 }
94
95 if (foo) {
96 bar();
97 }
98 else {
99 baz();
100 }
101
102 class C
103 {
104 static
105 {
106 foo();
107 }
108 }
109 ```
110
111 :::
112
113 Examples of **correct** code for this rule with the default `"1tbs"` option:
114
115 :::correct
116
117 ```js
118 /*eslint brace-style: "error"*/
119
120 function foo() {
121 return true;
122 }
123
124 if (foo) {
125 bar();
126 }
127
128 if (foo) {
129 bar();
130 } else {
131 baz();
132 }
133
134 try {
135 somethingRisky();
136 } catch(e) {
137 handleError();
138 }
139
140 class C {
141 static {
142 foo();
143 }
144 }
145
146 // when there are no braces, there are no problems
147 if (foo) bar();
148 else if (baz) boom();
149 ```
150
151 :::
152
153 Examples of **correct** code for this rule with the `"1tbs", { "allowSingleLine": true }` options:
154
155 :::correct
156
157 ```js
158 /*eslint brace-style: ["error", "1tbs", { "allowSingleLine": true }]*/
159
160 function nop() { return; }
161
162 if (foo) { bar(); }
163
164 if (foo) { bar(); } else { baz(); }
165
166 try { somethingRisky(); } catch(e) { handleError(); }
167
168 if (foo) { baz(); } else {
169 boom();
170 }
171
172 if (foo) { baz(); } else if (bar) {
173 boom();
174 }
175
176 if (foo) { baz(); } else
177 if (bar) {
178 boom();
179 }
180
181 if (foo) { baz(); } else if (bar) {
182 boom();
183 }
184
185 try { somethingRisky(); } catch(e) {
186 handleError();
187 }
188
189 class C {
190 static { foo(); }
191 }
192
193 class D { static { foo(); } }
194 ```
195
196 :::
197
198 ### stroustrup
199
200 Examples of **incorrect** code for this rule with the `"stroustrup"` option:
201
202 :::incorrect
203
204 ```js
205 /*eslint brace-style: ["error", "stroustrup"]*/
206
207 function foo()
208 {
209 return true;
210 }
211
212 if (foo)
213 {
214 bar();
215 }
216
217 try
218 {
219 somethingRisky();
220 } catch(e)
221 {
222 handleError();
223 }
224
225 class C
226 {
227 static
228 {
229 foo();
230 }
231 }
232
233 if (foo) {
234 bar();
235 } else {
236 baz();
237 }
238 ```
239
240 :::
241
242 Examples of **correct** code for this rule with the `"stroustrup"` option:
243
244 :::correct
245
246 ```js
247 /*eslint brace-style: ["error", "stroustrup"]*/
248
249 function foo() {
250 return true;
251 }
252
253 if (foo) {
254 bar();
255 }
256
257 if (foo) {
258 bar();
259 }
260 else {
261 baz();
262 }
263
264 try {
265 somethingRisky();
266 }
267 catch(e) {
268 handleError();
269 }
270
271 class C {
272 static {
273 foo();
274 }
275 }
276
277 // when there are no braces, there are no problems
278 if (foo) bar();
279 else if (baz) boom();
280 ```
281
282 :::
283
284 Examples of **correct** code for this rule with the `"stroustrup", { "allowSingleLine": true }` options:
285
286 :::correct
287
288 ```js
289 /*eslint brace-style: ["error", "stroustrup", { "allowSingleLine": true }]*/
290
291 function nop() { return; }
292
293 if (foo) { bar(); }
294
295 if (foo) { bar(); }
296 else { baz(); }
297
298 try { somethingRisky(); }
299 catch(e) { handleError(); }
300
301 class C {
302 static { foo(); }
303 }
304
305 class D { static { foo(); } }
306 ```
307
308 :::
309
310 ### allman
311
312 Examples of **incorrect** code for this rule with the `"allman"` option:
313
314 :::incorrect
315
316 ```js
317 /*eslint brace-style: ["error", "allman"]*/
318
319 function foo() {
320 return true;
321 }
322
323 if (foo)
324 {
325 bar(); }
326
327 try
328 {
329 somethingRisky();
330 } catch(e)
331 {
332 handleError();
333 }
334
335 class C {
336 static {
337 foo();
338 }
339 }
340
341 if (foo) {
342 bar();
343 } else {
344 baz();
345 }
346 ```
347
348 :::
349
350 Examples of **correct** code for this rule with the `"allman"` option:
351
352 :::correct
353
354 ```js
355 /*eslint brace-style: ["error", "allman"]*/
356
357 function foo()
358 {
359 return true;
360 }
361
362 if (foo)
363 {
364 bar();
365 }
366
367 if (foo)
368 {
369 bar();
370 }
371 else
372 {
373 baz();
374 }
375
376 try
377 {
378 somethingRisky();
379 }
380 catch(e)
381 {
382 handleError();
383 }
384
385 class C
386 {
387 static
388 {
389 foo();
390 }
391 }
392
393 // when there are no braces, there are no problems
394 if (foo) bar();
395 else if (baz) boom();
396 ```
397
398 :::
399
400 Examples of **correct** code for this rule with the `"allman", { "allowSingleLine": true }` options:
401
402 :::correct
403
404 ```js
405 /*eslint brace-style: ["error", "allman", { "allowSingleLine": true }]*/
406
407 function nop() { return; }
408
409 if (foo) { bar(); }
410
411 if (foo) { bar(); }
412 else { baz(); }
413
414 try { somethingRisky(); }
415 catch(e) { handleError(); }
416
417 class C
418 {
419 static { foo(); }
420
421 static
422 { foo(); }
423 }
424
425 class D { static { foo(); } }
426 ```
427
428 :::
429
430 ## When Not To Use It
431
432 If you don't want to enforce a particular brace style, don't enable this rule.