]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/rules/array-bracket-spacing.md
first commit
[pve-eslint.git] / eslint / docs / rules / array-bracket-spacing.md
CommitLineData
eb39fafa
DC
1# Disallow or enforce spaces inside of brackets (array-bracket-spacing)
2
3A number of style guides require or disallow spaces between array brackets and other tokens. This rule
4applies to both array literals and destructuring assignments (ECMAScript 6).
5
6```js
7/*eslint-env es6*/
8
9var arr = [ 'foo', 'bar' ];
10var [ x, y ] = z;
11
12var arr = ['foo', 'bar'];
13var [x,y] = z;
14```
15
16## Rule Details
17
18This rule enforces consistent spacing inside array brackets.
19
20## Options
21
22This rule has a string option:
23
24* `"never"` (default) disallows spaces inside array brackets
25* `"always"` requires one or more spaces or newlines inside array brackets
26
27This rule has an object option for exceptions to the `"never"` option:
28
29* `"singleValue": true` requires one or more spaces or newlines inside brackets of array literals that contain a single element
30* `"objectsInArrays": true` requires one or more spaces or newlines between brackets of array literals and braces of their object literal elements `[ {` or `} ]`
31* `"arraysInArrays": true` requires one or more spaces or newlines between brackets of array literals and brackets of their array literal elements `[ [` or `] ]`
32
33This rule has an object option for exceptions to the `"always"` option:
34
35* `"singleValue": false` disallows spaces inside brackets of array literals that contain a single element
36* `"objectsInArrays": false` disallows spaces between brackets of array literals and braces of their object literal elements `[{` or `}]`
37* `"arraysInArrays": false` disallows spaces between brackets of array literals and brackets of their array literal elements `[[` or `]]`
38
39This rule has built-in exceptions:
40
41* `"never"` (and also the exceptions to the `"always"` option) allows newlines inside array brackets, because this is a common pattern
42* `"always"` does not require spaces or newlines in empty array literals `[]`
43
44### never
45
46Examples of **incorrect** code for this rule with the default `"never"` option:
47
48```js
49/*eslint array-bracket-spacing: ["error", "never"]*/
50/*eslint-env es6*/
51
52var arr = [ 'foo', 'bar' ];
53var arr = ['foo', 'bar' ];
54var arr = [ ['foo'], 'bar'];
55var arr = [[ 'foo' ], 'bar'];
56var arr = [ 'foo',
57 'bar'
58];
59var [ x, y ] = z;
60var [ x,y ] = z;
61var [ x, ...y ] = z;
62var [ ,,x, ] = z;
63```
64
65Examples of **correct** code for this rule with the default `"never"` option:
66
67```js
68/*eslint array-bracket-spacing: ["error", "never"]*/
69/*eslint-env es6*/
70
71var arr = [];
72var arr = ['foo', 'bar', 'baz'];
73var arr = [['foo'], 'bar', 'baz'];
74var arr = [
75 'foo',
76 'bar',
77 'baz'
78];
79var arr = ['foo',
80 'bar'
81];
82var arr = [
83 'foo',
84 'bar'];
85
86var [x, y] = z;
87var [x,y] = z;
88var [x, ...y] = z;
89var [,,x,] = z;
90```
91
92### always
93
94Examples of **incorrect** code for this rule with the `"always"` option:
95
96```js
97/*eslint array-bracket-spacing: ["error", "always"]*/
98/*eslint-env es6*/
99
100var arr = ['foo', 'bar'];
101var arr = ['foo', 'bar' ];
102var arr = [ ['foo'], 'bar' ];
103var arr = ['foo',
104 'bar'
105];
106var arr = [
107 'foo',
108 'bar'];
109
110var [x, y] = z;
111var [x,y] = z;
112var [x, ...y] = z;
113var [,,x,] = z;
114```
115
116Examples of **correct** code for this rule with the `"always"` option:
117
118```js
119/*eslint array-bracket-spacing: ["error", "always"]*/
120/*eslint-env es6*/
121
122var arr = [];
123var arr = [ 'foo', 'bar', 'baz' ];
124var arr = [ [ 'foo' ], 'bar', 'baz' ];
125var arr = [ 'foo',
126 'bar'
127];
128var arr = [
129 'foo',
130 'bar' ];
131var arr = [
132 'foo',
133 'bar',
134 'baz'
135];
136
137var [ x, y ] = z;
138var [ x,y ] = z;
139var [ x, ...y ] = z;
140var [ ,,x, ] = z;
141```
142
143### singleValue
144
145Examples of **incorrect** code for this rule with the `"always", { "singleValue": false }` options:
146
147```js
148/*eslint array-bracket-spacing: ["error", "always", { "singleValue": false }]*/
149
150var foo = [ 'foo' ];
151var foo = [ 'foo'];
152var foo = ['foo' ];
153var foo = [ 1 ];
154var foo = [ 1];
155var foo = [1 ];
156var foo = [ [ 1, 2 ] ];
157var foo = [ { 'foo': 'bar' } ];
158```
159
160Examples of **correct** code for this rule with the `"always", { "singleValue": false }` options:
161
162```js
163/*eslint array-bracket-spacing: ["error", "always", { "singleValue": false }]*/
164
165var foo = ['foo'];
166var foo = [1];
167var foo = [[ 1, 1 ]];
168var foo = [{ 'foo': 'bar' }];
169```
170
171### objectsInArrays
172
173Examples of **incorrect** code for this rule with the `"always", { "objectsInArrays": false }` options:
174
175```js
176/*eslint array-bracket-spacing: ["error", "always", { "objectsInArrays": false }]*/
177
178var arr = [ { 'foo': 'bar' } ];
179var arr = [ {
180 'foo': 'bar'
181} ]
182```
183
184Examples of **correct** code for this rule with the `"always", { "objectsInArrays": false }` options:
185
186```js
187/*eslint array-bracket-spacing: ["error", "always", { "objectsInArrays": false }]*/
188
189var arr = [{ 'foo': 'bar' }];
190var arr = [{
191 'foo': 'bar'
192}];
193```
194
195### arraysInArrays
196
197Examples of **incorrect** code for this rule with the `"always", { "arraysInArrays": false }` options:
198
199```js
200/*eslint array-bracket-spacing: ["error", "always", { "arraysInArrays": false }]*/
201
202var arr = [ [ 1, 2 ], 2, 3, 4 ];
203var arr = [ [ 1, 2 ], 2, [ 3, 4 ] ];
204```
205
206Examples of **correct** code for this rule with the `"always", { "arraysInArrays": false }` options:
207
208```js
209/*eslint array-bracket-spacing: ["error", "always", { "arraysInArrays": false }]*/
210
211var arr = [[ 1, 2 ], 2, 3, 4 ];
212var arr = [[ 1, 2 ], 2, [ 3, 4 ]];
213```
214
215## When Not To Use It
216
217You can turn this rule off if you are not concerned with the consistency of spacing between array brackets.
218
219## Related Rules
220
221* [space-in-parens](space-in-parens.md)
222* [object-curly-spacing](object-curly-spacing.md)
223* [computed-property-spacing](computed-property-spacing.md)