]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/src/rules/space-in-parens.md
import 8.41.0 source
[pve-eslint.git] / eslint / docs / src / rules / space-in-parens.md
CommitLineData
8f9d1d4d
DC
1---
2title: space-in-parens
8f9d1d4d
DC
3rule_type: layout
4related_rules:
5- array-bracket-spacing
6- object-curly-spacing
7- computed-property-spacing
8---
9
10
eb39fafa
DC
11
12Some style guides require or disallow spaces inside of parentheses:
13
14```js
15foo( 'bar' );
16var x = ( 1 + 2 ) * 3;
17
18foo('bar');
19var x = (1 + 2) * 3;
20```
21
22## Rule Details
23
24This 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
26As long as you do not explicitly disallow empty parentheses using the `"empty"` exception , `()` will be allowed.
27
28## Options
29
30There 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
35Depending 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
43Examples 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
50foo( );
51
eb39fafa
DC
52foo( 'bar');
53foo('bar' );
54foo( 'bar' );
55
456be15e
TL
56foo( /* bar */ );
57
eb39fafa
DC
58var foo = ( 1 + 2 ) * 3;
59( function () { return 'bar'; }() );
60```
61
8f9d1d4d
DC
62:::
63
eb39fafa
DC
64Examples 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
71foo();
72
73foo('bar');
74
456be15e
TL
75foo(/* bar */);
76
eb39fafa
DC
77var foo = (1 + 2) * 3;
78(function () { return 'bar'; }());
79```
80
8f9d1d4d
DC
81:::
82
eb39fafa
DC
83### "always"
84
85Examples 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
92foo( 'bar');
93foo('bar' );
94foo('bar');
95
456be15e
TL
96foo(/* bar */);
97
eb39fafa
DC
98var foo = (1 + 2) * 3;
99(function () { return 'bar'; }());
100```
101
8f9d1d4d
DC
102:::
103
eb39fafa
DC
104Examples 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
111foo();
456be15e 112foo( );
eb39fafa
DC
113
114foo( 'bar' );
115
456be15e
TL
116foo( /* bar */ );
117
eb39fafa
DC
118var foo = ( 1 + 2 ) * 3;
119( function () { return 'bar'; }() );
120```
121
8f9d1d4d
DC
122:::
123
eb39fafa
DC
124### Exceptions
125
126An 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
128Note 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
130The following exceptions are available: `["{}", "[]", "()", "empty"]`.
131
132### Empty Exception
133
134Empty 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
143Examples 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
150foo({bar: 'baz'});
151foo(1, {bar: 'baz'});
152```
153
8f9d1d4d
DC
154:::
155
eb39fafa
DC
156Examples 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
163foo( {bar: 'baz'} );
164foo(1, {bar: 'baz'} );
165```
166
8f9d1d4d
DC
167:::
168
eb39fafa
DC
169Examples 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
176foo( {bar: 'baz'} );
177foo( 1, {bar: 'baz'} );
178```
179
8f9d1d4d
DC
180:::
181
eb39fafa
DC
182Examples 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
189foo({bar: 'baz'});
190foo( 1, {bar: 'baz'});
191```
192
8f9d1d4d
DC
193:::
194
eb39fafa
DC
195Examples 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
202foo([bar, baz]);
203foo([bar, baz], 1);
204```
205
8f9d1d4d
DC
206:::
207
eb39fafa
DC
208Examples 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
215foo( [bar, baz] );
216foo( [bar, baz], 1);
217```
218
8f9d1d4d
DC
219:::
220
eb39fafa
DC
221Examples 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
228foo( [bar, baz] );
229foo( [bar, baz], 1 );
230```
231
8f9d1d4d
DC
232:::
233
eb39fafa
DC
234Examples 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
241foo([bar, baz]);
242foo([bar, baz], 1 );
243```
244
8f9d1d4d
DC
245:::
246
eb39fafa
DC
247Examples 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
254foo((1 + 2));
255foo((1 + 2), 1);
256foo(bar());
257```
258
8f9d1d4d
DC
259:::
260
eb39fafa
DC
261Examples 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
268foo( (1 + 2) );
269foo( (1 + 2), 1);
270foo(bar() );
271```
272
8f9d1d4d
DC
273:::
274
eb39fafa
DC
275Examples 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
282foo( ( 1 + 2 ) );
283foo( ( 1 + 2 ), 1 );
284```
285
8f9d1d4d
DC
286:::
287
eb39fafa
DC
288Examples 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
295foo(( 1 + 2 ));
296foo(( 1 + 2 ), 1 );
297```
298
8f9d1d4d
DC
299:::
300
eb39fafa
DC
301The `"empty"` exception concerns empty parentheses, and works the same way as the other exceptions, inverting the first option.
302
303Example 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
310foo();
311```
312
8f9d1d4d
DC
313:::
314
eb39fafa
DC
315Example 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
322foo( );
323```
324
8f9d1d4d
DC
325:::
326
eb39fafa
DC
327Example 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
334foo( );
335```
336
8f9d1d4d
DC
337:::
338
eb39fafa
DC
339Example 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
346foo();
347```
348
8f9d1d4d
DC
349:::
350
eb39fafa
DC
351You can include multiple entries in the `"exceptions"` array.
352
353Examples 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
360bar( {bar:'baz'} );
361baz( 1, [1,2] );
362foo( {bar: 'baz'}, [1, 2] );
363```
364
8f9d1d4d
DC
365:::
366
eb39fafa
DC
367Examples 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
374bar({bar:'baz'});
375baz( 1, [1,2]);
376foo({bar: 'baz'}, [1, 2]);
377```
378
8f9d1d4d
DC
379:::
380
eb39fafa
DC
381## When Not To Use It
382
383You can turn this rule off if you are not concerned with the consistency of spacing between parentheses.