]>
Commit | Line | Data |
---|---|---|
8f9d1d4d DC |
1 | --- |
2 | title: curly | |
8f9d1d4d DC |
3 | rule_type: suggestion |
4 | --- | |
5 | ||
6 | ||
eb39fafa DC |
7 | |
8 | JavaScript allows the omission of curly braces when a block contains only one statement. However, it is considered by many to be best practice to _never_ omit curly braces around blocks, even when they are optional, because it can lead to bugs and reduces code clarity. So the following: | |
9 | ||
10 | ```js | |
11 | if (foo) foo++; | |
12 | ``` | |
13 | ||
14 | Can be rewritten as: | |
15 | ||
16 | ```js | |
17 | if (foo) { | |
18 | foo++; | |
19 | } | |
20 | ``` | |
21 | ||
22 | There are, however, some who prefer to only use braces when there is more than one statement to be executed. | |
23 | ||
24 | ## Rule Details | |
25 | ||
26 | This rule is aimed at preventing bugs and increasing code clarity by ensuring that block statements are wrapped in curly braces. It will warn when it encounters blocks that omit curly braces. | |
27 | ||
28 | ## Options | |
29 | ||
30 | ### all | |
31 | ||
32 | Examples of **incorrect** code for the default `"all"` option: | |
33 | ||
8f9d1d4d DC |
34 | ::: incorrect |
35 | ||
eb39fafa DC |
36 | ```js |
37 | /*eslint curly: "error"*/ | |
38 | ||
39 | if (foo) foo++; | |
40 | ||
41 | while (bar) | |
42 | baz(); | |
43 | ||
44 | if (foo) { | |
45 | baz(); | |
46 | } else qux(); | |
47 | ``` | |
48 | ||
8f9d1d4d DC |
49 | ::: |
50 | ||
eb39fafa DC |
51 | Examples of **correct** code for the default `"all"` option: |
52 | ||
8f9d1d4d DC |
53 | ::: correct |
54 | ||
eb39fafa DC |
55 | ```js |
56 | /*eslint curly: "error"*/ | |
57 | ||
58 | if (foo) { | |
59 | foo++; | |
60 | } | |
61 | ||
62 | while (bar) { | |
63 | baz(); | |
64 | } | |
65 | ||
66 | if (foo) { | |
67 | baz(); | |
68 | } else { | |
69 | qux(); | |
70 | } | |
71 | ``` | |
72 | ||
8f9d1d4d DC |
73 | ::: |
74 | ||
eb39fafa DC |
75 | ### multi |
76 | ||
77 | By default, this rule warns whenever `if`, `else`, `for`, `while`, or `do` are used without block statements as their body. However, you can specify that block statements should be used only when there are multiple statements in the block and warn when there is only one statement in the block. | |
78 | ||
79 | Examples of **incorrect** code for the `"multi"` option: | |
80 | ||
8f9d1d4d DC |
81 | ::: incorrect |
82 | ||
eb39fafa DC |
83 | ```js |
84 | /*eslint curly: ["error", "multi"]*/ | |
85 | ||
86 | if (foo) { | |
87 | foo++; | |
88 | } | |
89 | ||
90 | if (foo) bar(); | |
91 | else { | |
92 | foo++; | |
93 | } | |
94 | ||
95 | while (true) { | |
96 | doSomething(); | |
97 | } | |
98 | ||
99 | for (var i=0; i < items.length; i++) { | |
100 | doSomething(); | |
101 | } | |
102 | ``` | |
103 | ||
8f9d1d4d DC |
104 | ::: |
105 | ||
eb39fafa DC |
106 | Examples of **correct** code for the `"multi"` option: |
107 | ||
8f9d1d4d DC |
108 | ::: correct |
109 | ||
eb39fafa DC |
110 | ```js |
111 | /*eslint curly: ["error", "multi"]*/ | |
112 | ||
113 | if (foo) foo++; | |
114 | ||
115 | else foo(); | |
116 | ||
117 | while (true) { | |
118 | doSomething(); | |
119 | doSomethingElse(); | |
120 | } | |
121 | ``` | |
122 | ||
8f9d1d4d DC |
123 | ::: |
124 | ||
eb39fafa DC |
125 | ### multi-line |
126 | ||
127 | Alternatively, you can relax the rule to allow brace-less single-line `if`, `else if`, `else`, `for`, `while`, or `do`, while still enforcing the use of curly braces for other instances. | |
128 | ||
129 | Examples of **incorrect** code for the `"multi-line"` option: | |
130 | ||
8f9d1d4d DC |
131 | ::: incorrect |
132 | ||
eb39fafa DC |
133 | ```js |
134 | /*eslint curly: ["error", "multi-line"]*/ | |
135 | ||
136 | if (foo) | |
137 | doSomething(); | |
138 | else | |
139 | doSomethingElse(); | |
140 | ||
141 | if (foo) foo( | |
142 | bar, | |
143 | baz); | |
144 | ``` | |
145 | ||
8f9d1d4d DC |
146 | ::: |
147 | ||
eb39fafa DC |
148 | Examples of **correct** code for the `"multi-line"` option: |
149 | ||
8f9d1d4d DC |
150 | ::: correct |
151 | ||
eb39fafa DC |
152 | ```js |
153 | /*eslint curly: ["error", "multi-line"]*/ | |
154 | ||
155 | if (foo) foo++; else doSomething(); | |
156 | ||
157 | if (foo) foo++; | |
158 | else if (bar) baz() | |
159 | else doSomething(); | |
160 | ||
161 | do something(); | |
162 | while (foo); | |
163 | ||
164 | while (foo | |
165 | && bar) baz(); | |
166 | ||
167 | if (foo) { | |
168 | foo++; | |
169 | } | |
170 | ||
171 | if (foo) { foo++; } | |
172 | ||
173 | while (true) { | |
174 | doSomething(); | |
175 | doSomethingElse(); | |
176 | } | |
177 | ``` | |
178 | ||
8f9d1d4d DC |
179 | ::: |
180 | ||
eb39fafa DC |
181 | ### multi-or-nest |
182 | ||
183 | You can use another configuration that forces brace-less `if`, `else if`, `else`, `for`, `while`, or `do` if their body contains only one single-line statement. And forces braces in all other cases. | |
184 | ||
185 | Examples of **incorrect** code for the `"multi-or-nest"` option: | |
186 | ||
8f9d1d4d DC |
187 | ::: incorrect |
188 | ||
eb39fafa DC |
189 | ```js |
190 | /*eslint curly: ["error", "multi-or-nest"]*/ | |
191 | ||
192 | if (!foo) | |
193 | foo = { | |
194 | bar: baz, | |
195 | qux: foo | |
196 | }; | |
197 | ||
198 | while (true) | |
199 | if(foo) | |
200 | doSomething(); | |
201 | else | |
202 | doSomethingElse(); | |
203 | ||
204 | if (foo) { | |
205 | foo++; | |
206 | } | |
207 | ||
208 | while (true) { | |
209 | doSomething(); | |
210 | } | |
211 | ||
212 | for (var i = 0; foo; i++) { | |
213 | doSomething(); | |
214 | } | |
eb39fafa DC |
215 | ``` |
216 | ||
8f9d1d4d DC |
217 | ::: |
218 | ||
eb39fafa DC |
219 | Examples of **correct** code for the `"multi-or-nest"` option: |
220 | ||
8f9d1d4d DC |
221 | ::: correct |
222 | ||
eb39fafa DC |
223 | ```js |
224 | /*eslint curly: ["error", "multi-or-nest"]*/ | |
225 | ||
226 | if (!foo) { | |
227 | foo = { | |
228 | bar: baz, | |
229 | qux: foo | |
230 | }; | |
231 | } | |
232 | ||
233 | while (true) { | |
234 | if(foo) | |
235 | doSomething(); | |
236 | else | |
237 | doSomethingElse(); | |
238 | } | |
239 | ||
240 | if (foo) | |
241 | foo++; | |
242 | ||
243 | while (true) | |
244 | doSomething(); | |
245 | ||
246 | for (var i = 0; foo; i++) | |
247 | doSomething(); | |
56c4a2cb DC |
248 | ``` |
249 | ||
8f9d1d4d DC |
250 | ::: |
251 | ||
56c4a2cb DC |
252 | For single-line statements preceded by a comment, braces are allowed but not required. |
253 | ||
254 | Examples of additional **correct** code for the `"multi-or-nest"` option: | |
255 | ||
8f9d1d4d DC |
256 | ::: correct |
257 | ||
56c4a2cb DC |
258 | ```js |
259 | /*eslint curly: ["error", "multi-or-nest"]*/ | |
260 | ||
261 | if (foo) | |
262 | // some comment | |
263 | bar(); | |
eb39fafa DC |
264 | |
265 | if (foo) { | |
266 | // some comment | |
267 | bar(); | |
268 | } | |
269 | ``` | |
270 | ||
8f9d1d4d DC |
271 | ::: |
272 | ||
eb39fafa DC |
273 | ### consistent |
274 | ||
275 | When using any of the `multi*` options, you can add an option to enforce all bodies of a `if`, | |
276 | `else if` and `else` chain to be with or without braces. | |
277 | ||
278 | Examples of **incorrect** code for the `"multi", "consistent"` options: | |
279 | ||
8f9d1d4d DC |
280 | ::: incorrect |
281 | ||
eb39fafa DC |
282 | ```js |
283 | /*eslint curly: ["error", "multi", "consistent"]*/ | |
284 | ||
285 | if (foo) { | |
286 | bar(); | |
287 | baz(); | |
288 | } else | |
289 | buz(); | |
290 | ||
291 | if (foo) | |
292 | bar(); | |
293 | else if (faa) | |
294 | bor(); | |
295 | else { | |
296 | other(); | |
297 | things(); | |
298 | } | |
299 | ||
300 | if (true) | |
301 | foo(); | |
302 | else { | |
303 | baz(); | |
304 | } | |
305 | ||
306 | if (foo) { | |
307 | foo++; | |
308 | } | |
309 | ``` | |
310 | ||
8f9d1d4d DC |
311 | ::: |
312 | ||
eb39fafa DC |
313 | Examples of **correct** code for the `"multi", "consistent"` options: |
314 | ||
8f9d1d4d DC |
315 | ::: correct |
316 | ||
eb39fafa DC |
317 | ```js |
318 | /*eslint curly: ["error", "multi", "consistent"]*/ | |
319 | ||
320 | if (foo) { | |
321 | bar(); | |
322 | baz(); | |
323 | } else { | |
324 | buz(); | |
325 | } | |
326 | ||
327 | if (foo) { | |
328 | bar(); | |
329 | } else if (faa) { | |
330 | bor(); | |
331 | } else { | |
332 | other(); | |
333 | things(); | |
334 | } | |
335 | ||
336 | if (true) | |
337 | foo(); | |
338 | else | |
339 | baz(); | |
340 | ||
341 | if (foo) | |
342 | foo++; | |
343 | ||
344 | ``` | |
345 | ||
8f9d1d4d DC |
346 | ::: |
347 | ||
eb39fafa DC |
348 | ## When Not To Use It |
349 | ||
350 | If you have no strict conventions about when to use block statements and when not to, you can safely disable this rule. |