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