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