]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/src/rules/space-in-brackets.md
import 8.41.0 source
[pve-eslint.git] / eslint / docs / src / rules / space-in-brackets.md
CommitLineData
8f9d1d4d
DC
1---
2title: space-in-brackets
eb39fafa 3
8f9d1d4d
DC
4related_rules:
5- array-bracket-spacing
6- object-curly-spacing
7- space-in-parens
8- computed-property-spacing
9---
10
11Enforces 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
15While formatting preferences are very personal, a number of style guides require or disallow spaces between brackets:
16
17```js
18var obj = { foo: 'bar' };
19var arr = [ 'foo', 'bar' ];
20foo[ 'bar' ];
21
22var obj = {foo: 'bar'};
23var arr = ['foo', 'bar'];
24foo['bar'];
25```
26
27## Rule Details
28
29This 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
33There 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
38Depending 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
46Examples 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
53foo[ 'bar' ];
54foo['bar' ];
55
56var arr = [ 'foo', 'bar' ];
57var arr = ['foo', 'bar' ];
58var arr = [ ['foo'], 'bar'];
59var arr = [[ 'foo' ], 'bar'];
60var arr = ['foo',
61 'bar'
62];
63
64var obj = { 'foo': 'bar' };
65var obj = {'foo': 'bar' };
66var obj = { baz: {'foo': 'qux'}, bar};
67var obj = {baz: { 'foo': 'qux' }, bar};
68```
69
8f9d1d4d
DC
70:::
71
eb39fafa
DC
72Examples 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
79foo['bar'];
80foo[
81 'bar'
82];
83foo[
84 'bar'];
85
86var arr = [];
87var arr = ['foo', 'bar', 'baz'];
88var arr = [['foo'], 'bar', 'baz'];
89var arr = [
90 'foo',
91 'bar',
92 'baz'
93];
94
95var arr = [
96 'foo',
97 'bar'];
98
99var obj = {'foo': 'bar'};
100
101var obj = {'foo': {'bar': 'baz'}, 'qux': 'quxx'};
102
103var obj = {
104 'foo': 'bar'
105};
106var obj = {'foo': 'bar'
107};
108var obj = {
109 'foo':'bar'};
110
111var obj = {};
112```
113
8f9d1d4d
DC
114:::
115
eb39fafa
DC
116### "always"
117
118Examples 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
125foo['bar'];
126foo['bar' ];
127foo[ 'bar'];
128
129var arr = ['foo', 'bar'];
130var arr = ['foo', 'bar' ];
131var arr = [ ['foo'], 'bar' ];
132var arr = ['foo',
133 'bar'
134];
135
136var arr = [
137 'foo',
138 'bar'];
139
140var obj = {'foo': 'bar'};
141var obj = {'foo': 'bar' };
142var obj = { baz: {'foo': 'qux'}, bar};
143var obj = {baz: { 'foo': 'qux' }, bar};
144var obj = {'foo': 'bar'
145};
146
147var obj = {
148 'foo':'bar'};
149```
150
8f9d1d4d
DC
151:::
152
eb39fafa
DC
153Examples of **correct** code for this rule with the `"always"` option:
154
8f9d1d4d
DC
155::: correct
156
eb39fafa
DC
157```js
158foo[ 'bar' ];
159foo[
160 'bar'
161];
162
163var arr = [];
164var arr = [ 'foo', 'bar', 'baz' ];
165var arr = [ [ 'foo' ], 'bar', 'baz' ];
166
167var arr = [
168 'foo',
169 'bar',
170 'baz'
171];
172
173var obj = {};
174var obj = { 'foo': 'bar' };
175var obj = { 'foo': { 'bar': 'baz' }, 'qux': 'quxx' };
176var obj = {
177 'foo': 'bar'
178};
179```
180
8f9d1d4d
DC
181:::
182
eb39fafa
DC
183Note that `"always"` has a special case where `{}` and `[]` are not considered problems.
184
185### Exceptions
186
187An 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
189You can add exceptions like so:
190
191In 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
204In 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
217The 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
226In each of the following examples, the `"always"` option is assumed.
227
228Examples of **incorrect** code for this rule when `"singleValue"` is set to `false`:
229
8f9d1d4d
DC
230::: incorrect
231
eb39fafa
DC
232```js
233var foo = [ 'foo' ];
234var foo = [ 'foo'];
235var foo = ['foo' ];
236var foo = [ 1 ];
237var foo = [ 1];
238var foo = [1 ];
239var foo = [ [ 1, 2 ] ];
240var foo = [ { 'foo': 'bar' } ];
241```
242
8f9d1d4d
DC
243:::
244
eb39fafa
DC
245Examples of **correct** code for this rule when `"singleValue"` is set to `false`:
246
8f9d1d4d
DC
247::: correct
248
eb39fafa
DC
249```js
250var foo = ['foo'];
251var foo = [1];
252var foo = [[ 1, 1 ]];
253var foo = [{ 'foo': 'bar' }];
254```
255
8f9d1d4d
DC
256:::
257
eb39fafa
DC
258Examples of **incorrect** code when `"objectsInArrays"` is set to `false`:
259
8f9d1d4d
DC
260::: incorrect
261
eb39fafa
DC
262```js
263var arr = [ { 'foo': 'bar' } ];
264var arr = [ {
265 'foo': 'bar'
266} ]
267```
268
8f9d1d4d
DC
269:::
270
eb39fafa
DC
271Examples of **correct** code when `"objectsInArrays"` is set to `false`:
272
8f9d1d4d
DC
273::: correct
274
eb39fafa
DC
275```js
276var arr = [{ 'foo': 'bar' }];
277var arr = [{
278 'foo': 'bar'
279}];
280```
281
8f9d1d4d
DC
282:::
283
eb39fafa
DC
284Examples of **incorrect** code when `"arraysInArrays"` is set to `false`:
285
8f9d1d4d
DC
286::: incorrect
287
eb39fafa
DC
288```js
289var arr = [ [ 1, 2 ], 2, 3, 4 ];
290var arr = [ [ 1, 2 ], 2, [ 3, 4 ] ];
291```
292
8f9d1d4d
DC
293:::
294
eb39fafa
DC
295Examples of **correct** code when `"arraysInArrays"` is set to `false`:
296
8f9d1d4d
DC
297::: correct
298
eb39fafa
DC
299```js
300var arr = [[ 1, 2 ], 2, 3, 4 ];
301var arr = [[ 1, 2 ], 2, [ 3, 4 ]];
302```
303
8f9d1d4d
DC
304:::
305
eb39fafa
DC
306Examples of **incorrect** code when `"arraysInObjects"` is set to `false`:
307
8f9d1d4d
DC
308::: incorrect
309
eb39fafa
DC
310```js
311var obj = { "foo": [ 1, 2 ] };
312var obj = { "foo": [ "baz", "bar" ] };
313```
314
8f9d1d4d
DC
315:::
316
eb39fafa
DC
317Examples of **correct** code when `"arraysInObjects"` is set to `false`:
318
8f9d1d4d
DC
319::: correct
320
eb39fafa
DC
321```js
322var obj = { "foo": [ 1, 2 ]};
323var obj = { "foo": [ "baz", "bar" ]};
324```
325
8f9d1d4d
DC
326:::
327
eb39fafa
DC
328Examples of **incorrect** code when `"objectsInObjects"` is set to `false`:
329
8f9d1d4d
DC
330::: incorrect
331
eb39fafa
DC
332```js
333var obj = { "foo": { "baz": 1, "bar": 2 } };
334var obj = { "foo": [ "baz", "bar" ], "qux": { "baz": 1, "bar": 2 } };
335```
336
8f9d1d4d
DC
337:::
338
eb39fafa
DC
339Examples of **correct** code when `"objectsInObjects"` is set to `false`:
340
8f9d1d4d
DC
341::: correct
342
eb39fafa
DC
343```js
344var obj = { "foo": { "baz": 1, "bar": 2 }};
345var obj = { "foo": [ "baz", "bar" ], "qux": { "baz": 1, "bar": 2 }};
346```
347
8f9d1d4d
DC
348:::
349
eb39fafa
DC
350Examples of **incorrect** code when `"propertyName"` is set to `false`:
351
8f9d1d4d
DC
352::: incorrect
353
eb39fafa
DC
354```js
355var foo = obj[ 1 ];
356var foo = obj[ bar ];
357```
358
8f9d1d4d
DC
359:::
360
eb39fafa
DC
361Examples of **correct** code when `"propertyName"` is set to `false`:
362
8f9d1d4d
DC
363::: correct
364
eb39fafa
DC
365```js
366var foo = obj[bar];
367var foo = obj[0, 1];
368```
369
8f9d1d4d
DC
370:::
371
eb39fafa
DC
372## When Not To Use It
373
374You can turn this rule off if you are not concerned with the consistency of spacing between brackets.