]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/rules/space-in-parens.md
bfc0b46ed32e867a2c6d65259ccf6988dcd9d2a9
[pve-eslint.git] / eslint / docs / rules / space-in-parens.md
1 # Disallow or enforce spaces inside of parentheses (space-in-parens)
2
3 Some style guides require or disallow spaces inside of parentheses:
4
5 ```js
6 foo( 'bar' );
7 var x = ( 1 + 2 ) * 3;
8
9 foo('bar');
10 var x = (1 + 2) * 3;
11 ```
12
13 ## Rule Details
14
15 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 `)`.
16
17 As long as you do not explicitly disallow empty parentheses using the `"empty"` exception , `()` will be allowed.
18
19 ## Options
20
21 There are two options for this rule:
22
23 * `"never"` (default) enforces zero spaces inside of parentheses
24 * `"always"` enforces a space inside of parentheses
25
26 Depending on your coding conventions, you can choose either option by specifying it in your configuration:
27
28 ```json
29 "space-in-parens": ["error", "always"]
30 ```
31
32 ### "never"
33
34 Examples of **incorrect** code for this rule with the default `"never"` option:
35
36 ```js
37 /*eslint space-in-parens: ["error", "never"]*/
38
39 foo( 'bar');
40 foo('bar' );
41 foo( 'bar' );
42
43 var foo = ( 1 + 2 ) * 3;
44 ( function () { return 'bar'; }() );
45 ```
46
47 Examples of **correct** code for this rule with the default `"never"` option:
48
49 ```js
50 /*eslint space-in-parens: ["error", "never"]*/
51
52 foo();
53
54 foo('bar');
55
56 var foo = (1 + 2) * 3;
57 (function () { return 'bar'; }());
58 ```
59
60 ### "always"
61
62 Examples of **incorrect** code for this rule with the `"always"` option:
63
64 ```js
65 /*eslint space-in-parens: ["error", "always"]*/
66
67 foo( 'bar');
68 foo('bar' );
69 foo('bar');
70
71 var foo = (1 + 2) * 3;
72 (function () { return 'bar'; }());
73 ```
74
75 Examples of **correct** code for this rule with the `"always"` option:
76
77 ```js
78 /*eslint space-in-parens: ["error", "always"]*/
79
80 foo();
81
82 foo( 'bar' );
83
84 var foo = ( 1 + 2 ) * 3;
85 ( function () { return 'bar'; }() );
86 ```
87
88 ### Exceptions
89
90 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.
91
92 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.
93
94 The following exceptions are available: `["{}", "[]", "()", "empty"]`.
95
96 ### Empty Exception
97
98 Empty parens exception and behavior:
99
100 * `always` allows for both `()` and `( )`
101 * `never` (default) requires `()`
102 * `always` excepting `empty` requires `()`
103 * `never` excepting `empty` requires `( )` (empty parens without a space is here forbidden)
104
105 ### Examples
106
107 Examples of **incorrect** code for this rule with the `"never", { "exceptions": ["{}"] }` option:
108
109 ```js
110 /*eslint space-in-parens: ["error", "never", { "exceptions": ["{}"] }]*/
111
112 foo({bar: 'baz'});
113 foo(1, {bar: 'baz'});
114 ```
115
116 Examples of **correct** code for this rule with the `"never", { "exceptions": ["{}"] }` option:
117
118 ```js
119 /*eslint space-in-parens: ["error", "never", { "exceptions": ["{}"] }]*/
120
121 foo( {bar: 'baz'} );
122 foo(1, {bar: 'baz'} );
123 ```
124
125 Examples of **incorrect** code for this rule with the `"always", { "exceptions": ["{}"] }` option:
126
127 ```js
128 /*eslint space-in-parens: ["error", "always", { "exceptions": ["{}"] }]*/
129
130 foo( {bar: 'baz'} );
131 foo( 1, {bar: 'baz'} );
132 ```
133
134 Examples of **correct** code for this rule with the `"always", { "exceptions": ["{}"] }` option:
135
136 ```js
137 /*eslint space-in-parens: ["error", "always", { "exceptions": ["{}"] }]*/
138
139 foo({bar: 'baz'});
140 foo( 1, {bar: 'baz'});
141 ```
142
143 Examples of **incorrect** code for this rule with the `"never", { "exceptions": ["[]"] }` option:
144
145 ```js
146 /*eslint space-in-parens: ["error", "never", { "exceptions": ["[]"] }]*/
147
148 foo([bar, baz]);
149 foo([bar, baz], 1);
150 ```
151
152 Examples of **correct** code for this rule with the `"never", { "exceptions": ["[]"] }` option:
153
154 ```js
155 /*eslint space-in-parens: ["error", "never", { "exceptions": ["[]"] }]*/
156
157 foo( [bar, baz] );
158 foo( [bar, baz], 1);
159 ```
160
161 Examples of **incorrect** code for this rule with the `"always", { "exceptions": ["[]"] }` option:
162
163 ```js
164 /*eslint space-in-parens: ["error", "always", { "exceptions": ["[]"] }]*/
165
166 foo( [bar, baz] );
167 foo( [bar, baz], 1 );
168 ```
169
170 Examples of **correct** code for this rule with the `"always", { "exceptions": ["[]"] }` option:
171
172 ```js
173 /*eslint space-in-parens: ["error", "always", { "exceptions": ["[]"] }]*/
174
175 foo([bar, baz]);
176 foo([bar, baz], 1 );
177 ```
178
179 Examples of **incorrect** code for this rule with the `"never", { "exceptions": ["()"] }]` option:
180
181 ```js
182 /*eslint space-in-parens: ["error", "never", { "exceptions": ["()"] }]*/
183
184 foo((1 + 2));
185 foo((1 + 2), 1);
186 foo(bar());
187 ```
188
189 Examples of **correct** code for this rule with the `"never", { "exceptions": ["()"] }]` option:
190
191 ```js
192 /*eslint space-in-parens: ["error", "never", { "exceptions": ["()"] }]*/
193
194 foo( (1 + 2) );
195 foo( (1 + 2), 1);
196 foo(bar() );
197 ```
198
199 Examples of **incorrect** code for this rule with the `"always", { "exceptions": ["()"] }]` option:
200
201 ```js
202 /*eslint space-in-parens: ["error", "always", { "exceptions": ["()"] }]*/
203
204 foo( ( 1 + 2 ) );
205 foo( ( 1 + 2 ), 1 );
206 ```
207
208 Examples of **correct** code for this rule with the `"always", { "exceptions": ["()"] }]` option:
209
210 ```js
211 /*eslint space-in-parens: ["error", "always", { "exceptions": ["()"] }]*/
212
213 foo(( 1 + 2 ));
214 foo(( 1 + 2 ), 1 );
215 ```
216
217 The `"empty"` exception concerns empty parentheses, and works the same way as the other exceptions, inverting the first option.
218
219 Example of **incorrect** code for this rule with the `"never", { "exceptions": ["empty"] }]` option:
220
221 ```js
222 /*eslint space-in-parens: ["error", "never", { "exceptions": ["empty"] }]*/
223
224 foo();
225 ```
226
227 Example of **correct** code for this rule with the `"never", { "exceptions": ["empty"] }]` option:
228
229 ```js
230 /*eslint space-in-parens: ["error", "never", { "exceptions": ["empty"] }]*/
231
232 foo( );
233 ```
234
235 Example of **incorrect** code for this rule with the `"always", { "exceptions": ["empty"] }]` option:
236
237 ```js
238 /*eslint space-in-parens: ["error", "always", { "exceptions": ["empty"] }]*/
239
240 foo( );
241 ```
242
243 Example of **correct** code for this rule with the `"always", { "exceptions": ["empty"] }]` option:
244
245 ```js
246 /*eslint space-in-parens: ["error", "always", { "exceptions": ["empty"] }]*/
247
248 foo();
249 ```
250
251 You can include multiple entries in the `"exceptions"` array.
252
253 Examples of **incorrect** code for this rule with the `"always", { "exceptions": ["{}", "[]"] }]` option:
254
255 ```js
256 /*eslint space-in-parens: ["error", "always", { "exceptions": ["{}", "[]"] }]*/
257
258 bar( {bar:'baz'} );
259 baz( 1, [1,2] );
260 foo( {bar: 'baz'}, [1, 2] );
261 ```
262
263 Examples of **correct** code for this rule with the `"always", { "exceptions": ["{}", "[]"] }]` option:
264
265 ```js
266 /*eslint space-in-parens: ["error", "always", { "exceptions": ["{}", "[]"] }]*/
267
268 bar({bar:'baz'});
269 baz( 1, [1,2]);
270 foo({bar: 'baz'}, [1, 2]);
271 ```
272
273 ## When Not To Use It
274
275 You can turn this rule off if you are not concerned with the consistency of spacing between parentheses.
276
277 ## Related Rules
278
279 * [space-in-brackets](space-in-brackets.md) (deprecated)