]>
Commit | Line | Data |
---|---|---|
eb39fafa DC |
1 | # Disallow or enforce spaces inside of brackets (array-bracket-spacing) |
2 | ||
3 | A number of style guides require or disallow spaces between array brackets and other tokens. This rule | |
4 | applies to both array literals and destructuring assignments (ECMAScript 6). | |
5 | ||
6 | ```js | |
7 | /*eslint-env es6*/ | |
8 | ||
9 | var arr = [ 'foo', 'bar' ]; | |
10 | var [ x, y ] = z; | |
11 | ||
12 | var arr = ['foo', 'bar']; | |
13 | var [x,y] = z; | |
14 | ``` | |
15 | ||
16 | ## Rule Details | |
17 | ||
18 | This rule enforces consistent spacing inside array brackets. | |
19 | ||
20 | ## Options | |
21 | ||
22 | This 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 | ||
27 | This 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 | ||
33 | This 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 | ||
39 | This 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 | ||
46 | Examples 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 | ||
52 | var arr = [ 'foo', 'bar' ]; | |
53 | var arr = ['foo', 'bar' ]; | |
54 | var arr = [ ['foo'], 'bar']; | |
55 | var arr = [[ 'foo' ], 'bar']; | |
56 | var arr = [ 'foo', | |
57 | 'bar' | |
58 | ]; | |
59 | var [ x, y ] = z; | |
60 | var [ x,y ] = z; | |
61 | var [ x, ...y ] = z; | |
62 | var [ ,,x, ] = z; | |
63 | ``` | |
64 | ||
65 | Examples 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 | ||
71 | var arr = []; | |
72 | var arr = ['foo', 'bar', 'baz']; | |
73 | var arr = [['foo'], 'bar', 'baz']; | |
74 | var arr = [ | |
75 | 'foo', | |
76 | 'bar', | |
77 | 'baz' | |
78 | ]; | |
79 | var arr = ['foo', | |
80 | 'bar' | |
81 | ]; | |
82 | var arr = [ | |
83 | 'foo', | |
84 | 'bar']; | |
85 | ||
86 | var [x, y] = z; | |
87 | var [x,y] = z; | |
88 | var [x, ...y] = z; | |
89 | var [,,x,] = z; | |
90 | ``` | |
91 | ||
92 | ### always | |
93 | ||
94 | Examples 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 | ||
100 | var arr = ['foo', 'bar']; | |
101 | var arr = ['foo', 'bar' ]; | |
102 | var arr = [ ['foo'], 'bar' ]; | |
103 | var arr = ['foo', | |
104 | 'bar' | |
105 | ]; | |
106 | var arr = [ | |
107 | 'foo', | |
108 | 'bar']; | |
109 | ||
110 | var [x, y] = z; | |
111 | var [x,y] = z; | |
112 | var [x, ...y] = z; | |
113 | var [,,x,] = z; | |
114 | ``` | |
115 | ||
116 | Examples 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 | ||
122 | var arr = []; | |
123 | var arr = [ 'foo', 'bar', 'baz' ]; | |
124 | var arr = [ [ 'foo' ], 'bar', 'baz' ]; | |
125 | var arr = [ 'foo', | |
126 | 'bar' | |
127 | ]; | |
128 | var arr = [ | |
129 | 'foo', | |
130 | 'bar' ]; | |
131 | var arr = [ | |
132 | 'foo', | |
133 | 'bar', | |
134 | 'baz' | |
135 | ]; | |
136 | ||
137 | var [ x, y ] = z; | |
138 | var [ x,y ] = z; | |
139 | var [ x, ...y ] = z; | |
140 | var [ ,,x, ] = z; | |
141 | ``` | |
142 | ||
143 | ### singleValue | |
144 | ||
145 | Examples 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 | ||
150 | var foo = [ 'foo' ]; | |
151 | var foo = [ 'foo']; | |
152 | var foo = ['foo' ]; | |
153 | var foo = [ 1 ]; | |
154 | var foo = [ 1]; | |
155 | var foo = [1 ]; | |
156 | var foo = [ [ 1, 2 ] ]; | |
157 | var foo = [ { 'foo': 'bar' } ]; | |
158 | ``` | |
159 | ||
160 | Examples 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 | ||
165 | var foo = ['foo']; | |
166 | var foo = [1]; | |
167 | var foo = [[ 1, 1 ]]; | |
168 | var foo = [{ 'foo': 'bar' }]; | |
169 | ``` | |
170 | ||
171 | ### objectsInArrays | |
172 | ||
173 | Examples 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 | ||
178 | var arr = [ { 'foo': 'bar' } ]; | |
179 | var arr = [ { | |
180 | 'foo': 'bar' | |
181 | } ] | |
182 | ``` | |
183 | ||
184 | Examples 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 | ||
189 | var arr = [{ 'foo': 'bar' }]; | |
190 | var arr = [{ | |
191 | 'foo': 'bar' | |
192 | }]; | |
193 | ``` | |
194 | ||
195 | ### arraysInArrays | |
196 | ||
197 | Examples 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 | ||
202 | var arr = [ [ 1, 2 ], 2, 3, 4 ]; | |
203 | var arr = [ [ 1, 2 ], 2, [ 3, 4 ] ]; | |
204 | ``` | |
205 | ||
206 | Examples 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 | ||
211 | var arr = [[ 1, 2 ], 2, 3, 4 ]; | |
212 | var arr = [[ 1, 2 ], 2, [ 3, 4 ]]; | |
213 | ``` | |
214 | ||
215 | ## When Not To Use It | |
216 | ||
217 | You 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) |