]>
Commit | Line | Data |
---|---|---|
8f9d1d4d DC |
1 | --- |
2 | title: space-in-brackets | |
eb39fafa | 3 | |
8f9d1d4d DC |
4 | related_rules: |
5 | - array-bracket-spacing | |
6 | - object-curly-spacing | |
7 | - space-in-parens | |
8 | - computed-property-spacing | |
9 | --- | |
10 | ||
11 | Enforces consistent spacing inside braces of object literals and brackets of array literals. | |
12 | ||
13 | (removed) This rule was **removed** in ESLint v1.0 and **replaced** by the [object-curly-spacing](object-curly-spacing) and [array-bracket-spacing](array-bracket-spacing) rules. | |
eb39fafa DC |
14 | |
15 | While formatting preferences are very personal, a number of style guides require or disallow spaces between brackets: | |
16 | ||
17 | ```js | |
18 | var obj = { foo: 'bar' }; | |
19 | var arr = [ 'foo', 'bar' ]; | |
20 | foo[ 'bar' ]; | |
21 | ||
22 | var obj = {foo: 'bar'}; | |
23 | var arr = ['foo', 'bar']; | |
24 | foo['bar']; | |
25 | ``` | |
26 | ||
27 | ## Rule Details | |
28 | ||
29 | This rule aims to maintain consistency around the spacing inside of square brackets, either by disallowing spaces inside of brackets between the brackets and other tokens or enforcing spaces. Brackets that are separated from the adjacent value by a new line are excepted from this rule, as this is a common pattern. Object literals that are used as the first or last element in an array are also ignored. | |
30 | ||
31 | ## Options | |
32 | ||
33 | There are two options for this rule: | |
34 | ||
35 | * `"always"` enforces a space inside of object and array literals | |
36 | * `"never"` enforces zero spaces inside of object and array literals (default) | |
37 | ||
38 | Depending on your coding conventions, you can choose either option by specifying it in your configuration: | |
39 | ||
40 | ```json | |
41 | "space-in-brackets": ["error", "always"] | |
42 | ``` | |
43 | ||
44 | ### "never" | |
45 | ||
46 | Examples of **incorrect** code for this rule with the default `"never"` option: | |
47 | ||
8f9d1d4d DC |
48 | ::: incorrect |
49 | ||
eb39fafa DC |
50 | ```js |
51 | /*eslint-env es6*/ | |
52 | ||
53 | foo[ 'bar' ]; | |
54 | foo['bar' ]; | |
55 | ||
56 | var arr = [ 'foo', 'bar' ]; | |
57 | var arr = ['foo', 'bar' ]; | |
58 | var arr = [ ['foo'], 'bar']; | |
59 | var arr = [[ 'foo' ], 'bar']; | |
60 | var arr = ['foo', | |
61 | 'bar' | |
62 | ]; | |
63 | ||
64 | var obj = { 'foo': 'bar' }; | |
65 | var obj = {'foo': 'bar' }; | |
66 | var obj = { baz: {'foo': 'qux'}, bar}; | |
67 | var obj = {baz: { 'foo': 'qux' }, bar}; | |
68 | ``` | |
69 | ||
8f9d1d4d DC |
70 | ::: |
71 | ||
eb39fafa DC |
72 | Examples of **correct** code for this rule with the default `"never"` option: |
73 | ||
8f9d1d4d DC |
74 | ::: correct |
75 | ||
eb39fafa DC |
76 | ```js |
77 | // When options are ["error", "never"] | |
78 | ||
79 | foo['bar']; | |
80 | foo[ | |
81 | 'bar' | |
82 | ]; | |
83 | foo[ | |
84 | 'bar']; | |
85 | ||
86 | var arr = []; | |
87 | var arr = ['foo', 'bar', 'baz']; | |
88 | var arr = [['foo'], 'bar', 'baz']; | |
89 | var arr = [ | |
90 | 'foo', | |
91 | 'bar', | |
92 | 'baz' | |
93 | ]; | |
94 | ||
95 | var arr = [ | |
96 | 'foo', | |
97 | 'bar']; | |
98 | ||
99 | var obj = {'foo': 'bar'}; | |
100 | ||
101 | var obj = {'foo': {'bar': 'baz'}, 'qux': 'quxx'}; | |
102 | ||
103 | var obj = { | |
104 | 'foo': 'bar' | |
105 | }; | |
106 | var obj = {'foo': 'bar' | |
107 | }; | |
108 | var obj = { | |
109 | 'foo':'bar'}; | |
110 | ||
111 | var obj = {}; | |
112 | ``` | |
113 | ||
8f9d1d4d DC |
114 | ::: |
115 | ||
eb39fafa DC |
116 | ### "always" |
117 | ||
118 | Examples of **incorrect** code for this rule with the `"always"` option: | |
119 | ||
8f9d1d4d DC |
120 | ::: incorrect |
121 | ||
eb39fafa DC |
122 | ```js |
123 | /*eslint-env es6*/ | |
124 | ||
125 | foo['bar']; | |
126 | foo['bar' ]; | |
127 | foo[ 'bar']; | |
128 | ||
129 | var arr = ['foo', 'bar']; | |
130 | var arr = ['foo', 'bar' ]; | |
131 | var arr = [ ['foo'], 'bar' ]; | |
132 | var arr = ['foo', | |
133 | 'bar' | |
134 | ]; | |
135 | ||
136 | var arr = [ | |
137 | 'foo', | |
138 | 'bar']; | |
139 | ||
140 | var obj = {'foo': 'bar'}; | |
141 | var obj = {'foo': 'bar' }; | |
142 | var obj = { baz: {'foo': 'qux'}, bar}; | |
143 | var obj = {baz: { 'foo': 'qux' }, bar}; | |
144 | var obj = {'foo': 'bar' | |
145 | }; | |
146 | ||
147 | var obj = { | |
148 | 'foo':'bar'}; | |
149 | ``` | |
150 | ||
8f9d1d4d DC |
151 | ::: |
152 | ||
eb39fafa DC |
153 | Examples of **correct** code for this rule with the `"always"` option: |
154 | ||
8f9d1d4d DC |
155 | ::: correct |
156 | ||
eb39fafa DC |
157 | ```js |
158 | foo[ 'bar' ]; | |
159 | foo[ | |
160 | 'bar' | |
161 | ]; | |
162 | ||
163 | var arr = []; | |
164 | var arr = [ 'foo', 'bar', 'baz' ]; | |
165 | var arr = [ [ 'foo' ], 'bar', 'baz' ]; | |
166 | ||
167 | var arr = [ | |
168 | 'foo', | |
169 | 'bar', | |
170 | 'baz' | |
171 | ]; | |
172 | ||
173 | var obj = {}; | |
174 | var obj = { 'foo': 'bar' }; | |
175 | var obj = { 'foo': { 'bar': 'baz' }, 'qux': 'quxx' }; | |
176 | var obj = { | |
177 | 'foo': 'bar' | |
178 | }; | |
179 | ``` | |
180 | ||
8f9d1d4d DC |
181 | ::: |
182 | ||
eb39fafa DC |
183 | Note that `"always"` has a special case where `{}` and `[]` are not considered problems. |
184 | ||
185 | ### Exceptions | |
186 | ||
187 | An object literal may be used as a third array item to specify spacing exceptions. These exceptions work in the context of the first option. That is, if `"always"` is set to enforce spacing and an exception is set to `false`, it will disallow spacing for cases matching the exception. Likewise, if `"never"` is set to disallow spacing and an exception is set to `true`, it will enforce spacing for cases matching the exception. | |
188 | ||
189 | You can add exceptions like so: | |
190 | ||
191 | In case of `"always"` option, set an exception to `false` to enable it: | |
192 | ||
193 | ```json | |
194 | "space-in-brackets": ["error", "always", { | |
195 | "singleValue": false, | |
196 | "objectsInArrays": false, | |
197 | "arraysInArrays": false, | |
198 | "arraysInObjects": false, | |
199 | "objectsInObjects": false, | |
200 | "propertyName": false | |
201 | }] | |
202 | ``` | |
203 | ||
204 | In case of `"never"` option, set an exception to `true` to enable it: | |
205 | ||
206 | ```json | |
207 | "space-in-brackets": ["error", "never", { | |
208 | "singleValue": true, | |
209 | "objectsInArrays": true, | |
210 | "arraysInArrays": true, | |
211 | "arraysInObjects": true, | |
212 | "objectsInObjects": true, | |
213 | "propertyName": true | |
214 | }] | |
215 | ``` | |
216 | ||
217 | The following exceptions are available: | |
218 | ||
219 | * `singleValue` sets the spacing of a single value inside of square brackets of an array. | |
220 | * `objectsInArrays` sets the spacings between the curly braces and square brackets of object literals that are the first or last element in an array. | |
221 | * `arraysInArrays` sets the spacing between the square brackets of array literals that are the first or last element in an array. | |
222 | * `arraysInObjects` sets the spacing between the square bracket and the curly brace of an array literal that is the last element in an object. | |
223 | * `objectsInObjects` sets the spacing between the curly brace of an object literal that is the last element in an object and the curly brace of the containing object. | |
224 | * `propertyName` sets the spacing in square brackets of computed member expressions. | |
225 | ||
226 | In each of the following examples, the `"always"` option is assumed. | |
227 | ||
228 | Examples of **incorrect** code for this rule when `"singleValue"` is set to `false`: | |
229 | ||
8f9d1d4d DC |
230 | ::: incorrect |
231 | ||
eb39fafa DC |
232 | ```js |
233 | var foo = [ 'foo' ]; | |
234 | var foo = [ 'foo']; | |
235 | var foo = ['foo' ]; | |
236 | var foo = [ 1 ]; | |
237 | var foo = [ 1]; | |
238 | var foo = [1 ]; | |
239 | var foo = [ [ 1, 2 ] ]; | |
240 | var foo = [ { 'foo': 'bar' } ]; | |
241 | ``` | |
242 | ||
8f9d1d4d DC |
243 | ::: |
244 | ||
eb39fafa DC |
245 | Examples of **correct** code for this rule when `"singleValue"` is set to `false`: |
246 | ||
8f9d1d4d DC |
247 | ::: correct |
248 | ||
eb39fafa DC |
249 | ```js |
250 | var foo = ['foo']; | |
251 | var foo = [1]; | |
252 | var foo = [[ 1, 1 ]]; | |
253 | var foo = [{ 'foo': 'bar' }]; | |
254 | ``` | |
255 | ||
8f9d1d4d DC |
256 | ::: |
257 | ||
eb39fafa DC |
258 | Examples of **incorrect** code when `"objectsInArrays"` is set to `false`: |
259 | ||
8f9d1d4d DC |
260 | ::: incorrect |
261 | ||
eb39fafa DC |
262 | ```js |
263 | var arr = [ { 'foo': 'bar' } ]; | |
264 | var arr = [ { | |
265 | 'foo': 'bar' | |
266 | } ] | |
267 | ``` | |
268 | ||
8f9d1d4d DC |
269 | ::: |
270 | ||
eb39fafa DC |
271 | Examples of **correct** code when `"objectsInArrays"` is set to `false`: |
272 | ||
8f9d1d4d DC |
273 | ::: correct |
274 | ||
eb39fafa DC |
275 | ```js |
276 | var arr = [{ 'foo': 'bar' }]; | |
277 | var arr = [{ | |
278 | 'foo': 'bar' | |
279 | }]; | |
280 | ``` | |
281 | ||
8f9d1d4d DC |
282 | ::: |
283 | ||
eb39fafa DC |
284 | Examples of **incorrect** code when `"arraysInArrays"` is set to `false`: |
285 | ||
8f9d1d4d DC |
286 | ::: incorrect |
287 | ||
eb39fafa DC |
288 | ```js |
289 | var arr = [ [ 1, 2 ], 2, 3, 4 ]; | |
290 | var arr = [ [ 1, 2 ], 2, [ 3, 4 ] ]; | |
291 | ``` | |
292 | ||
8f9d1d4d DC |
293 | ::: |
294 | ||
eb39fafa DC |
295 | Examples of **correct** code when `"arraysInArrays"` is set to `false`: |
296 | ||
8f9d1d4d DC |
297 | ::: correct |
298 | ||
eb39fafa DC |
299 | ```js |
300 | var arr = [[ 1, 2 ], 2, 3, 4 ]; | |
301 | var arr = [[ 1, 2 ], 2, [ 3, 4 ]]; | |
302 | ``` | |
303 | ||
8f9d1d4d DC |
304 | ::: |
305 | ||
eb39fafa DC |
306 | Examples of **incorrect** code when `"arraysInObjects"` is set to `false`: |
307 | ||
8f9d1d4d DC |
308 | ::: incorrect |
309 | ||
eb39fafa DC |
310 | ```js |
311 | var obj = { "foo": [ 1, 2 ] }; | |
312 | var obj = { "foo": [ "baz", "bar" ] }; | |
313 | ``` | |
314 | ||
8f9d1d4d DC |
315 | ::: |
316 | ||
eb39fafa DC |
317 | Examples of **correct** code when `"arraysInObjects"` is set to `false`: |
318 | ||
8f9d1d4d DC |
319 | ::: correct |
320 | ||
eb39fafa DC |
321 | ```js |
322 | var obj = { "foo": [ 1, 2 ]}; | |
323 | var obj = { "foo": [ "baz", "bar" ]}; | |
324 | ``` | |
325 | ||
8f9d1d4d DC |
326 | ::: |
327 | ||
eb39fafa DC |
328 | Examples of **incorrect** code when `"objectsInObjects"` is set to `false`: |
329 | ||
8f9d1d4d DC |
330 | ::: incorrect |
331 | ||
eb39fafa DC |
332 | ```js |
333 | var obj = { "foo": { "baz": 1, "bar": 2 } }; | |
334 | var obj = { "foo": [ "baz", "bar" ], "qux": { "baz": 1, "bar": 2 } }; | |
335 | ``` | |
336 | ||
8f9d1d4d DC |
337 | ::: |
338 | ||
eb39fafa DC |
339 | Examples of **correct** code when `"objectsInObjects"` is set to `false`: |
340 | ||
8f9d1d4d DC |
341 | ::: correct |
342 | ||
eb39fafa DC |
343 | ```js |
344 | var obj = { "foo": { "baz": 1, "bar": 2 }}; | |
345 | var obj = { "foo": [ "baz", "bar" ], "qux": { "baz": 1, "bar": 2 }}; | |
346 | ``` | |
347 | ||
8f9d1d4d DC |
348 | ::: |
349 | ||
eb39fafa DC |
350 | Examples of **incorrect** code when `"propertyName"` is set to `false`: |
351 | ||
8f9d1d4d DC |
352 | ::: incorrect |
353 | ||
eb39fafa DC |
354 | ```js |
355 | var foo = obj[ 1 ]; | |
356 | var foo = obj[ bar ]; | |
357 | ``` | |
358 | ||
8f9d1d4d DC |
359 | ::: |
360 | ||
eb39fafa DC |
361 | Examples of **correct** code when `"propertyName"` is set to `false`: |
362 | ||
8f9d1d4d DC |
363 | ::: correct |
364 | ||
eb39fafa DC |
365 | ```js |
366 | var foo = obj[bar]; | |
367 | var foo = obj[0, 1]; | |
368 | ``` | |
369 | ||
8f9d1d4d DC |
370 | ::: |
371 | ||
eb39fafa DC |
372 | ## When Not To Use It |
373 | ||
374 | You can turn this rule off if you are not concerned with the consistency of spacing between brackets. |