]>
Commit | Line | Data |
---|---|---|
8f9d1d4d DC |
1 | --- |
2 | title: array-bracket-spacing | |
8f9d1d4d DC |
3 | rule_type: layout |
4 | related_rules: | |
5 | - space-in-parens | |
6 | - object-curly-spacing | |
7 | - computed-property-spacing | |
8 | --- | |
9 | ||
10 | ||
eb39fafa DC |
11 | |
12 | A number of style guides require or disallow spaces between array brackets and other tokens. This rule | |
13 | applies to both array literals and destructuring assignments (ECMAScript 6). | |
14 | ||
15 | ```js | |
16 | /*eslint-env es6*/ | |
17 | ||
18 | var arr = [ 'foo', 'bar' ]; | |
19 | var [ x, y ] = z; | |
20 | ||
21 | var arr = ['foo', 'bar']; | |
22 | var [x,y] = z; | |
23 | ``` | |
24 | ||
25 | ## Rule Details | |
26 | ||
27 | This rule enforces consistent spacing inside array brackets. | |
28 | ||
29 | ## Options | |
30 | ||
31 | This 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 | ||
36 | This 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 | ||
42 | This 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 | ||
48 | This 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 | ||
55 | Examples 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 | ||
63 | var arr = [ 'foo', 'bar' ]; | |
64 | var arr = ['foo', 'bar' ]; | |
65 | var arr = [ ['foo'], 'bar']; | |
66 | var arr = [[ 'foo' ], 'bar']; | |
67 | var arr = [ 'foo', | |
68 | 'bar' | |
69 | ]; | |
70 | var [ x, y ] = z; | |
71 | var [ x,y ] = z; | |
72 | var [ x, ...y ] = z; | |
73 | var [ ,,x, ] = z; | |
74 | ``` | |
75 | ||
8f9d1d4d DC |
76 | ::: |
77 | ||
eb39fafa DC |
78 | Examples 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 | ||
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 | var arr = ['foo', | |
95 | 'bar' | |
96 | ]; | |
97 | var arr = [ | |
98 | 'foo', | |
99 | 'bar']; | |
100 | ||
101 | var [x, y] = z; | |
102 | var [x,y] = z; | |
103 | var [x, ...y] = z; | |
104 | var [,,x,] = z; | |
105 | ``` | |
106 | ||
8f9d1d4d DC |
107 | ::: |
108 | ||
eb39fafa DC |
109 | ### always |
110 | ||
111 | Examples 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 | ||
119 | var arr = ['foo', 'bar']; | |
120 | var arr = ['foo', 'bar' ]; | |
121 | var arr = [ ['foo'], 'bar' ]; | |
122 | var arr = ['foo', | |
123 | 'bar' | |
124 | ]; | |
125 | var arr = [ | |
126 | 'foo', | |
127 | 'bar']; | |
128 | ||
129 | var [x, y] = z; | |
130 | var [x,y] = z; | |
131 | var [x, ...y] = z; | |
132 | var [,,x,] = z; | |
133 | ``` | |
134 | ||
8f9d1d4d DC |
135 | ::: |
136 | ||
eb39fafa DC |
137 | Examples 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 | ||
145 | var arr = []; | |
146 | var arr = [ 'foo', 'bar', 'baz' ]; | |
147 | var arr = [ [ 'foo' ], 'bar', 'baz' ]; | |
148 | var arr = [ 'foo', | |
149 | 'bar' | |
150 | ]; | |
151 | var arr = [ | |
152 | 'foo', | |
153 | 'bar' ]; | |
154 | var arr = [ | |
155 | 'foo', | |
156 | 'bar', | |
157 | 'baz' | |
158 | ]; | |
159 | ||
160 | var [ x, y ] = z; | |
161 | var [ x,y ] = z; | |
162 | var [ x, ...y ] = z; | |
163 | var [ ,,x, ] = z; | |
164 | ``` | |
165 | ||
8f9d1d4d DC |
166 | ::: |
167 | ||
eb39fafa DC |
168 | ### singleValue |
169 | ||
170 | Examples 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 | ||
177 | var foo = [ 'foo' ]; | |
178 | var foo = [ 'foo']; | |
179 | var foo = ['foo' ]; | |
180 | var foo = [ 1 ]; | |
181 | var foo = [ 1]; | |
182 | var foo = [1 ]; | |
183 | var foo = [ [ 1, 2 ] ]; | |
184 | var foo = [ { 'foo': 'bar' } ]; | |
185 | ``` | |
186 | ||
8f9d1d4d DC |
187 | ::: |
188 | ||
eb39fafa DC |
189 | Examples 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 | ||
196 | var foo = ['foo']; | |
197 | var foo = [1]; | |
198 | var foo = [[ 1, 1 ]]; | |
199 | var foo = [{ 'foo': 'bar' }]; | |
200 | ``` | |
201 | ||
8f9d1d4d DC |
202 | ::: |
203 | ||
eb39fafa DC |
204 | ### objectsInArrays |
205 | ||
206 | Examples 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 | ||
213 | var arr = [ { 'foo': 'bar' } ]; | |
214 | var arr = [ { | |
215 | 'foo': 'bar' | |
216 | } ] | |
217 | ``` | |
218 | ||
8f9d1d4d DC |
219 | ::: |
220 | ||
eb39fafa DC |
221 | Examples 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 | ||
228 | var arr = [{ 'foo': 'bar' }]; | |
229 | var arr = [{ | |
230 | 'foo': 'bar' | |
231 | }]; | |
232 | ``` | |
233 | ||
8f9d1d4d DC |
234 | ::: |
235 | ||
eb39fafa DC |
236 | ### arraysInArrays |
237 | ||
238 | Examples 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 | ||
245 | var arr = [ [ 1, 2 ], 2, 3, 4 ]; | |
246 | var arr = [ [ 1, 2 ], 2, [ 3, 4 ] ]; | |
247 | ``` | |
248 | ||
8f9d1d4d DC |
249 | ::: |
250 | ||
eb39fafa DC |
251 | Examples 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 | ||
258 | var arr = [[ 1, 2 ], 2, 3, 4 ]; | |
259 | var arr = [[ 1, 2 ], 2, [ 3, 4 ]]; | |
260 | ``` | |
261 | ||
8f9d1d4d DC |
262 | ::: |
263 | ||
eb39fafa DC |
264 | ## When Not To Use It |
265 | ||
266 | You can turn this rule off if you are not concerned with the consistency of spacing between array brackets. |