]>
Commit | Line | Data |
---|---|---|
8f9d1d4d DC |
1 | --- |
2 | title: space-in-parens | |
8f9d1d4d DC |
3 | rule_type: layout |
4 | related_rules: | |
5 | - array-bracket-spacing | |
6 | - object-curly-spacing | |
7 | - computed-property-spacing | |
8 | --- | |
9 | ||
10 | ||
eb39fafa DC |
11 | |
12 | Some style guides require or disallow spaces inside of parentheses: | |
13 | ||
14 | ```js | |
15 | foo( 'bar' ); | |
16 | var x = ( 1 + 2 ) * 3; | |
17 | ||
18 | foo('bar'); | |
19 | var x = (1 + 2) * 3; | |
20 | ``` | |
21 | ||
22 | ## Rule Details | |
23 | ||
24 | This rule will enforce consistent spacing directly inside of parentheses, by disallowing or requiring one or more spaces to the right of `(` and to the left of `)`. | |
25 | ||
26 | As long as you do not explicitly disallow empty parentheses using the `"empty"` exception , `()` will be allowed. | |
27 | ||
28 | ## Options | |
29 | ||
30 | There are two options for this rule: | |
31 | ||
32 | * `"never"` (default) enforces zero spaces inside of parentheses | |
33 | * `"always"` enforces a space inside of parentheses | |
34 | ||
35 | Depending on your coding conventions, you can choose either option by specifying it in your configuration: | |
36 | ||
37 | ```json | |
38 | "space-in-parens": ["error", "always"] | |
39 | ``` | |
40 | ||
41 | ### "never" | |
42 | ||
43 | Examples of **incorrect** code for this rule with the default `"never"` option: | |
44 | ||
8f9d1d4d DC |
45 | ::: incorrect |
46 | ||
eb39fafa DC |
47 | ```js |
48 | /*eslint space-in-parens: ["error", "never"]*/ | |
49 | ||
456be15e TL |
50 | foo( ); |
51 | ||
eb39fafa DC |
52 | foo( 'bar'); |
53 | foo('bar' ); | |
54 | foo( 'bar' ); | |
55 | ||
456be15e TL |
56 | foo( /* bar */ ); |
57 | ||
eb39fafa DC |
58 | var foo = ( 1 + 2 ) * 3; |
59 | ( function () { return 'bar'; }() ); | |
60 | ``` | |
61 | ||
8f9d1d4d DC |
62 | ::: |
63 | ||
eb39fafa DC |
64 | Examples of **correct** code for this rule with the default `"never"` option: |
65 | ||
8f9d1d4d DC |
66 | ::: correct |
67 | ||
eb39fafa DC |
68 | ```js |
69 | /*eslint space-in-parens: ["error", "never"]*/ | |
70 | ||
71 | foo(); | |
72 | ||
73 | foo('bar'); | |
74 | ||
456be15e TL |
75 | foo(/* bar */); |
76 | ||
eb39fafa DC |
77 | var foo = (1 + 2) * 3; |
78 | (function () { return 'bar'; }()); | |
79 | ``` | |
80 | ||
8f9d1d4d DC |
81 | ::: |
82 | ||
eb39fafa DC |
83 | ### "always" |
84 | ||
85 | Examples of **incorrect** code for this rule with the `"always"` option: | |
86 | ||
8f9d1d4d DC |
87 | ::: incorrect |
88 | ||
eb39fafa DC |
89 | ```js |
90 | /*eslint space-in-parens: ["error", "always"]*/ | |
91 | ||
92 | foo( 'bar'); | |
93 | foo('bar' ); | |
94 | foo('bar'); | |
95 | ||
456be15e TL |
96 | foo(/* bar */); |
97 | ||
eb39fafa DC |
98 | var foo = (1 + 2) * 3; |
99 | (function () { return 'bar'; }()); | |
100 | ``` | |
101 | ||
8f9d1d4d DC |
102 | ::: |
103 | ||
eb39fafa DC |
104 | Examples of **correct** code for this rule with the `"always"` option: |
105 | ||
8f9d1d4d DC |
106 | ::: correct |
107 | ||
eb39fafa DC |
108 | ```js |
109 | /*eslint space-in-parens: ["error", "always"]*/ | |
110 | ||
111 | foo(); | |
456be15e | 112 | foo( ); |
eb39fafa DC |
113 | |
114 | foo( 'bar' ); | |
115 | ||
456be15e TL |
116 | foo( /* bar */ ); |
117 | ||
eb39fafa DC |
118 | var foo = ( 1 + 2 ) * 3; |
119 | ( function () { return 'bar'; }() ); | |
120 | ``` | |
121 | ||
8f9d1d4d DC |
122 | ::: |
123 | ||
eb39fafa DC |
124 | ### Exceptions |
125 | ||
126 | An object literal may be used as a third array item to specify exceptions, with the key `"exceptions"` and an array as the value. These exceptions work in the context of the first option. That is, if `"always"` is set to enforce spacing, then any "exception" will *disallow* spacing. Conversely, if `"never"` is set to disallow spacing, then any "exception" will *enforce* spacing. | |
127 | ||
128 | Note that this rule only enforces spacing within parentheses; it does not check spacing within curly or square brackets, but will enforce or disallow spacing of those brackets if and only if they are adjacent to an opening or closing parenthesis. | |
129 | ||
130 | The following exceptions are available: `["{}", "[]", "()", "empty"]`. | |
131 | ||
132 | ### Empty Exception | |
133 | ||
134 | Empty parens exception and behavior: | |
135 | ||
136 | * `always` allows for both `()` and `( )` | |
137 | * `never` (default) requires `()` | |
138 | * `always` excepting `empty` requires `()` | |
139 | * `never` excepting `empty` requires `( )` (empty parens without a space is here forbidden) | |
140 | ||
141 | ### Examples | |
142 | ||
143 | Examples of **incorrect** code for this rule with the `"never", { "exceptions": ["{}"] }` option: | |
144 | ||
8f9d1d4d DC |
145 | ::: incorrect |
146 | ||
eb39fafa DC |
147 | ```js |
148 | /*eslint space-in-parens: ["error", "never", { "exceptions": ["{}"] }]*/ | |
149 | ||
150 | foo({bar: 'baz'}); | |
151 | foo(1, {bar: 'baz'}); | |
152 | ``` | |
153 | ||
8f9d1d4d DC |
154 | ::: |
155 | ||
eb39fafa DC |
156 | Examples of **correct** code for this rule with the `"never", { "exceptions": ["{}"] }` option: |
157 | ||
8f9d1d4d DC |
158 | ::: correct |
159 | ||
eb39fafa DC |
160 | ```js |
161 | /*eslint space-in-parens: ["error", "never", { "exceptions": ["{}"] }]*/ | |
162 | ||
163 | foo( {bar: 'baz'} ); | |
164 | foo(1, {bar: 'baz'} ); | |
165 | ``` | |
166 | ||
8f9d1d4d DC |
167 | ::: |
168 | ||
eb39fafa DC |
169 | Examples of **incorrect** code for this rule with the `"always", { "exceptions": ["{}"] }` option: |
170 | ||
8f9d1d4d DC |
171 | ::: incorrect |
172 | ||
eb39fafa DC |
173 | ```js |
174 | /*eslint space-in-parens: ["error", "always", { "exceptions": ["{}"] }]*/ | |
175 | ||
176 | foo( {bar: 'baz'} ); | |
177 | foo( 1, {bar: 'baz'} ); | |
178 | ``` | |
179 | ||
8f9d1d4d DC |
180 | ::: |
181 | ||
eb39fafa DC |
182 | Examples of **correct** code for this rule with the `"always", { "exceptions": ["{}"] }` option: |
183 | ||
8f9d1d4d DC |
184 | ::: correct |
185 | ||
eb39fafa DC |
186 | ```js |
187 | /*eslint space-in-parens: ["error", "always", { "exceptions": ["{}"] }]*/ | |
188 | ||
189 | foo({bar: 'baz'}); | |
190 | foo( 1, {bar: 'baz'}); | |
191 | ``` | |
192 | ||
8f9d1d4d DC |
193 | ::: |
194 | ||
eb39fafa DC |
195 | Examples of **incorrect** code for this rule with the `"never", { "exceptions": ["[]"] }` option: |
196 | ||
8f9d1d4d DC |
197 | ::: incorrect |
198 | ||
eb39fafa DC |
199 | ```js |
200 | /*eslint space-in-parens: ["error", "never", { "exceptions": ["[]"] }]*/ | |
201 | ||
202 | foo([bar, baz]); | |
203 | foo([bar, baz], 1); | |
204 | ``` | |
205 | ||
8f9d1d4d DC |
206 | ::: |
207 | ||
eb39fafa DC |
208 | Examples of **correct** code for this rule with the `"never", { "exceptions": ["[]"] }` option: |
209 | ||
8f9d1d4d DC |
210 | ::: correct |
211 | ||
eb39fafa DC |
212 | ```js |
213 | /*eslint space-in-parens: ["error", "never", { "exceptions": ["[]"] }]*/ | |
214 | ||
215 | foo( [bar, baz] ); | |
216 | foo( [bar, baz], 1); | |
217 | ``` | |
218 | ||
8f9d1d4d DC |
219 | ::: |
220 | ||
eb39fafa DC |
221 | Examples of **incorrect** code for this rule with the `"always", { "exceptions": ["[]"] }` option: |
222 | ||
8f9d1d4d DC |
223 | ::: incorrect |
224 | ||
eb39fafa DC |
225 | ```js |
226 | /*eslint space-in-parens: ["error", "always", { "exceptions": ["[]"] }]*/ | |
227 | ||
228 | foo( [bar, baz] ); | |
229 | foo( [bar, baz], 1 ); | |
230 | ``` | |
231 | ||
8f9d1d4d DC |
232 | ::: |
233 | ||
eb39fafa DC |
234 | Examples of **correct** code for this rule with the `"always", { "exceptions": ["[]"] }` option: |
235 | ||
8f9d1d4d DC |
236 | ::: correct |
237 | ||
eb39fafa DC |
238 | ```js |
239 | /*eslint space-in-parens: ["error", "always", { "exceptions": ["[]"] }]*/ | |
240 | ||
241 | foo([bar, baz]); | |
242 | foo([bar, baz], 1 ); | |
243 | ``` | |
244 | ||
8f9d1d4d DC |
245 | ::: |
246 | ||
eb39fafa DC |
247 | Examples of **incorrect** code for this rule with the `"never", { "exceptions": ["()"] }]` option: |
248 | ||
8f9d1d4d DC |
249 | ::: incorrect |
250 | ||
eb39fafa DC |
251 | ```js |
252 | /*eslint space-in-parens: ["error", "never", { "exceptions": ["()"] }]*/ | |
253 | ||
254 | foo((1 + 2)); | |
255 | foo((1 + 2), 1); | |
256 | foo(bar()); | |
257 | ``` | |
258 | ||
8f9d1d4d DC |
259 | ::: |
260 | ||
eb39fafa DC |
261 | Examples of **correct** code for this rule with the `"never", { "exceptions": ["()"] }]` option: |
262 | ||
8f9d1d4d DC |
263 | ::: correct |
264 | ||
eb39fafa DC |
265 | ```js |
266 | /*eslint space-in-parens: ["error", "never", { "exceptions": ["()"] }]*/ | |
267 | ||
268 | foo( (1 + 2) ); | |
269 | foo( (1 + 2), 1); | |
270 | foo(bar() ); | |
271 | ``` | |
272 | ||
8f9d1d4d DC |
273 | ::: |
274 | ||
eb39fafa DC |
275 | Examples of **incorrect** code for this rule with the `"always", { "exceptions": ["()"] }]` option: |
276 | ||
8f9d1d4d DC |
277 | ::: incorrect |
278 | ||
eb39fafa DC |
279 | ```js |
280 | /*eslint space-in-parens: ["error", "always", { "exceptions": ["()"] }]*/ | |
281 | ||
282 | foo( ( 1 + 2 ) ); | |
283 | foo( ( 1 + 2 ), 1 ); | |
284 | ``` | |
285 | ||
8f9d1d4d DC |
286 | ::: |
287 | ||
eb39fafa DC |
288 | Examples of **correct** code for this rule with the `"always", { "exceptions": ["()"] }]` option: |
289 | ||
8f9d1d4d DC |
290 | ::: correct |
291 | ||
eb39fafa DC |
292 | ```js |
293 | /*eslint space-in-parens: ["error", "always", { "exceptions": ["()"] }]*/ | |
294 | ||
295 | foo(( 1 + 2 )); | |
296 | foo(( 1 + 2 ), 1 ); | |
297 | ``` | |
298 | ||
8f9d1d4d DC |
299 | ::: |
300 | ||
eb39fafa DC |
301 | The `"empty"` exception concerns empty parentheses, and works the same way as the other exceptions, inverting the first option. |
302 | ||
303 | Example of **incorrect** code for this rule with the `"never", { "exceptions": ["empty"] }]` option: | |
304 | ||
8f9d1d4d DC |
305 | ::: incorrect |
306 | ||
eb39fafa DC |
307 | ```js |
308 | /*eslint space-in-parens: ["error", "never", { "exceptions": ["empty"] }]*/ | |
309 | ||
310 | foo(); | |
311 | ``` | |
312 | ||
8f9d1d4d DC |
313 | ::: |
314 | ||
eb39fafa DC |
315 | Example of **correct** code for this rule with the `"never", { "exceptions": ["empty"] }]` option: |
316 | ||
8f9d1d4d DC |
317 | ::: correct |
318 | ||
eb39fafa DC |
319 | ```js |
320 | /*eslint space-in-parens: ["error", "never", { "exceptions": ["empty"] }]*/ | |
321 | ||
322 | foo( ); | |
323 | ``` | |
324 | ||
8f9d1d4d DC |
325 | ::: |
326 | ||
eb39fafa DC |
327 | Example of **incorrect** code for this rule with the `"always", { "exceptions": ["empty"] }]` option: |
328 | ||
8f9d1d4d DC |
329 | ::: incorrect |
330 | ||
eb39fafa DC |
331 | ```js |
332 | /*eslint space-in-parens: ["error", "always", { "exceptions": ["empty"] }]*/ | |
333 | ||
334 | foo( ); | |
335 | ``` | |
336 | ||
8f9d1d4d DC |
337 | ::: |
338 | ||
eb39fafa DC |
339 | Example of **correct** code for this rule with the `"always", { "exceptions": ["empty"] }]` option: |
340 | ||
8f9d1d4d DC |
341 | ::: correct |
342 | ||
eb39fafa DC |
343 | ```js |
344 | /*eslint space-in-parens: ["error", "always", { "exceptions": ["empty"] }]*/ | |
345 | ||
346 | foo(); | |
347 | ``` | |
348 | ||
8f9d1d4d DC |
349 | ::: |
350 | ||
eb39fafa DC |
351 | You can include multiple entries in the `"exceptions"` array. |
352 | ||
353 | Examples of **incorrect** code for this rule with the `"always", { "exceptions": ["{}", "[]"] }]` option: | |
354 | ||
8f9d1d4d DC |
355 | ::: incorrect |
356 | ||
eb39fafa DC |
357 | ```js |
358 | /*eslint space-in-parens: ["error", "always", { "exceptions": ["{}", "[]"] }]*/ | |
359 | ||
360 | bar( {bar:'baz'} ); | |
361 | baz( 1, [1,2] ); | |
362 | foo( {bar: 'baz'}, [1, 2] ); | |
363 | ``` | |
364 | ||
8f9d1d4d DC |
365 | ::: |
366 | ||
eb39fafa DC |
367 | Examples of **correct** code for this rule with the `"always", { "exceptions": ["{}", "[]"] }]` option: |
368 | ||
8f9d1d4d DC |
369 | ::: correct |
370 | ||
eb39fafa DC |
371 | ```js |
372 | /*eslint space-in-parens: ["error", "always", { "exceptions": ["{}", "[]"] }]*/ | |
373 | ||
374 | bar({bar:'baz'}); | |
375 | baz( 1, [1,2]); | |
376 | foo({bar: 'baz'}, [1, 2]); | |
377 | ``` | |
378 | ||
8f9d1d4d DC |
379 | ::: |
380 | ||
eb39fafa DC |
381 | ## When Not To Use It |
382 | ||
383 | You can turn this rule off if you are not concerned with the consistency of spacing between parentheses. |