]>
Commit | Line | Data |
---|---|---|
8f9d1d4d DC |
1 | --- |
2 | title: brace-style | |
8f9d1d4d DC |
3 | rule_type: layout |
4 | related_rules: | |
5 | - block-spacing | |
6 | - space-before-blocks | |
7 | further_reading: | |
8 | - https://en.wikipedia.org/wiki/Indent_style | |
9 | --- | |
10 | ||
11 | ||
eb39fafa DC |
12 | |
13 | 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. | |
14 | ||
15 | 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: | |
16 | ||
17 | ```js | |
18 | if (foo) { | |
19 | bar(); | |
20 | } else { | |
21 | baz(); | |
22 | } | |
23 | ``` | |
24 | ||
25 | 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: | |
26 | ||
27 | ```js | |
28 | if (foo) { | |
29 | bar(); | |
30 | } | |
31 | else { | |
32 | baz(); | |
33 | } | |
34 | ``` | |
35 | ||
36 | 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: | |
37 | ||
38 | ```js | |
39 | if (foo) | |
40 | { | |
41 | bar(); | |
42 | } | |
43 | else | |
44 | { | |
45 | baz(); | |
46 | } | |
47 | ``` | |
48 | ||
49 | 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. | |
50 | ||
51 | ## Rule Details | |
52 | ||
53 | This rule enforces consistent brace style for blocks. | |
54 | ||
55 | ## Options | |
56 | ||
57 | This 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 | ||
63 | This 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 | ||
69 | Examples 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 | ||
76 | function foo() | |
77 | { | |
78 | return true; | |
79 | } | |
80 | ||
81 | if (foo) | |
82 | { | |
83 | bar(); | |
84 | } | |
85 | ||
86 | try | |
87 | { | |
88 | somethingRisky(); | |
89 | } catch(e) | |
90 | { | |
91 | handleError(); | |
92 | } | |
93 | ||
94 | if (foo) { | |
95 | bar(); | |
96 | } | |
97 | else { | |
98 | baz(); | |
99 | } | |
609c276f TL |
100 | |
101 | class C | |
102 | { | |
103 | static | |
104 | { | |
105 | foo(); | |
106 | } | |
107 | } | |
eb39fafa DC |
108 | ``` |
109 | ||
8f9d1d4d DC |
110 | ::: |
111 | ||
eb39fafa DC |
112 | Examples 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 | ||
119 | function foo() { | |
120 | return true; | |
121 | } | |
122 | ||
123 | if (foo) { | |
124 | bar(); | |
125 | } | |
126 | ||
127 | if (foo) { | |
128 | bar(); | |
129 | } else { | |
130 | baz(); | |
131 | } | |
132 | ||
133 | try { | |
134 | somethingRisky(); | |
135 | } catch(e) { | |
136 | handleError(); | |
137 | } | |
138 | ||
609c276f TL |
139 | class C { |
140 | static { | |
141 | foo(); | |
142 | } | |
143 | } | |
144 | ||
eb39fafa DC |
145 | // when there are no braces, there are no problems |
146 | if (foo) bar(); | |
147 | else if (baz) boom(); | |
148 | ``` | |
149 | ||
8f9d1d4d DC |
150 | ::: |
151 | ||
eb39fafa DC |
152 | Examples 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 | ||
159 | function nop() { return; } | |
160 | ||
161 | if (foo) { bar(); } | |
162 | ||
163 | if (foo) { bar(); } else { baz(); } | |
164 | ||
165 | try { somethingRisky(); } catch(e) { handleError(); } | |
166 | ||
167 | if (foo) { baz(); } else { | |
168 | boom(); | |
169 | } | |
170 | ||
171 | if (foo) { baz(); } else if (bar) { | |
172 | boom(); | |
173 | } | |
174 | ||
175 | if (foo) { baz(); } else | |
176 | if (bar) { | |
177 | boom(); | |
178 | } | |
179 | ||
180 | if (foo) { baz(); } else if (bar) { | |
181 | boom(); | |
182 | } | |
183 | ||
184 | try { somethingRisky(); } catch(e) { | |
185 | handleError(); | |
186 | } | |
609c276f TL |
187 | |
188 | class C { | |
189 | static { foo(); } | |
190 | } | |
191 | ||
192 | class D { static { foo(); } } | |
eb39fafa DC |
193 | ``` |
194 | ||
8f9d1d4d DC |
195 | ::: |
196 | ||
eb39fafa DC |
197 | ### stroustrup |
198 | ||
199 | Examples 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 | ||
206 | function foo() | |
207 | { | |
208 | return true; | |
209 | } | |
210 | ||
211 | if (foo) | |
212 | { | |
213 | bar(); | |
214 | } | |
215 | ||
216 | try | |
217 | { | |
218 | somethingRisky(); | |
219 | } catch(e) | |
220 | { | |
221 | handleError(); | |
222 | } | |
223 | ||
609c276f TL |
224 | class C |
225 | { | |
226 | static | |
227 | { | |
228 | foo(); | |
229 | } | |
230 | } | |
231 | ||
eb39fafa DC |
232 | if (foo) { |
233 | bar(); | |
234 | } else { | |
235 | baz(); | |
236 | } | |
237 | ``` | |
238 | ||
8f9d1d4d DC |
239 | ::: |
240 | ||
eb39fafa DC |
241 | Examples 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 | ||
248 | function foo() { | |
249 | return true; | |
250 | } | |
251 | ||
252 | if (foo) { | |
253 | bar(); | |
254 | } | |
255 | ||
256 | if (foo) { | |
257 | bar(); | |
258 | } | |
259 | else { | |
260 | baz(); | |
261 | } | |
262 | ||
263 | try { | |
264 | somethingRisky(); | |
265 | } | |
266 | catch(e) { | |
267 | handleError(); | |
268 | } | |
269 | ||
609c276f TL |
270 | class C { |
271 | static { | |
272 | foo(); | |
273 | } | |
274 | } | |
275 | ||
eb39fafa DC |
276 | // when there are no braces, there are no problems |
277 | if (foo) bar(); | |
278 | else if (baz) boom(); | |
279 | ``` | |
280 | ||
8f9d1d4d DC |
281 | ::: |
282 | ||
eb39fafa DC |
283 | Examples 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 | ||
290 | function nop() { return; } | |
291 | ||
292 | if (foo) { bar(); } | |
293 | ||
294 | if (foo) { bar(); } | |
295 | else { baz(); } | |
296 | ||
297 | try { somethingRisky(); } | |
298 | catch(e) { handleError(); } | |
609c276f TL |
299 | |
300 | class C { | |
301 | static { foo(); } | |
302 | } | |
303 | ||
304 | class D { static { foo(); } } | |
eb39fafa DC |
305 | ``` |
306 | ||
8f9d1d4d DC |
307 | ::: |
308 | ||
eb39fafa DC |
309 | ### allman |
310 | ||
311 | Examples 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 | ||
318 | function foo() { | |
319 | return true; | |
320 | } | |
321 | ||
322 | if (foo) | |
323 | { | |
324 | bar(); } | |
325 | ||
326 | try | |
327 | { | |
328 | somethingRisky(); | |
329 | } catch(e) | |
330 | { | |
331 | handleError(); | |
332 | } | |
333 | ||
609c276f TL |
334 | class C { |
335 | static { | |
336 | foo(); | |
337 | } | |
338 | } | |
339 | ||
eb39fafa DC |
340 | if (foo) { |
341 | bar(); | |
342 | } else { | |
343 | baz(); | |
344 | } | |
345 | ``` | |
346 | ||
8f9d1d4d DC |
347 | ::: |
348 | ||
eb39fafa DC |
349 | Examples 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 | ||
356 | function foo() | |
357 | { | |
358 | return true; | |
359 | } | |
360 | ||
361 | if (foo) | |
362 | { | |
363 | bar(); | |
364 | } | |
365 | ||
366 | if (foo) | |
367 | { | |
368 | bar(); | |
369 | } | |
370 | else | |
371 | { | |
372 | baz(); | |
373 | } | |
374 | ||
375 | try | |
376 | { | |
377 | somethingRisky(); | |
378 | } | |
379 | catch(e) | |
380 | { | |
381 | handleError(); | |
382 | } | |
383 | ||
609c276f TL |
384 | class C |
385 | { | |
386 | static | |
387 | { | |
388 | foo(); | |
389 | } | |
390 | } | |
391 | ||
eb39fafa DC |
392 | // when there are no braces, there are no problems |
393 | if (foo) bar(); | |
394 | else if (baz) boom(); | |
395 | ``` | |
396 | ||
8f9d1d4d DC |
397 | ::: |
398 | ||
eb39fafa DC |
399 | Examples 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 | ||
406 | function nop() { return; } | |
407 | ||
408 | if (foo) { bar(); } | |
409 | ||
410 | if (foo) { bar(); } | |
411 | else { baz(); } | |
412 | ||
413 | try { somethingRisky(); } | |
414 | catch(e) { handleError(); } | |
609c276f TL |
415 | |
416 | class C | |
417 | { | |
418 | static { foo(); } | |
419 | ||
420 | static | |
421 | { foo(); } | |
422 | } | |
423 | ||
424 | class D { static { foo(); } } | |
eb39fafa DC |
425 | ``` |
426 | ||
8f9d1d4d DC |
427 | ::: |
428 | ||
eb39fafa DC |
429 | ## When Not To Use It |
430 | ||
431 | If you don't want to enforce a particular brace style, don't enable this rule. |