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