]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/rules/space-in-brackets.md
330609acd25d7e684f204075b81c513588c55080
[pve-eslint.git] / eslint / docs / rules / space-in-brackets.md
1 # space-in-brackets: enforce consistent spacing inside braces of object literals and brackets of array literals
2
3 (removed) This rule was **removed** in ESLint v1.0 and **replaced** by the [object-curly-spacing](object-curly-spacing.md) and [array-bracket-spacing](array-bracket-spacing.md) rules.
4
5 While formatting preferences are very personal, a number of style guides require or disallow spaces between brackets:
6
7 ```js
8 var obj = { foo: 'bar' };
9 var arr = [ 'foo', 'bar' ];
10 foo[ 'bar' ];
11
12 var obj = {foo: 'bar'};
13 var arr = ['foo', 'bar'];
14 foo['bar'];
15 ```
16
17 ## Rule Details
18
19 This 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.
20
21 ## Options
22
23 There are two options for this rule:
24
25 * `"always"` enforces a space inside of object and array literals
26 * `"never"` enforces zero spaces inside of object and array literals (default)
27
28 Depending on your coding conventions, you can choose either option by specifying it in your configuration:
29
30 ```json
31 "space-in-brackets": ["error", "always"]
32 ```
33
34 ### "never"
35
36 Examples of **incorrect** code for this rule with the default `"never"` option:
37
38 ```js
39 /*eslint-env es6*/
40
41 foo[ 'bar' ];
42 foo['bar' ];
43
44 var arr = [ 'foo', 'bar' ];
45 var arr = ['foo', 'bar' ];
46 var arr = [ ['foo'], 'bar'];
47 var arr = [[ 'foo' ], 'bar'];
48 var arr = ['foo',
49 'bar'
50 ];
51
52 var obj = { 'foo': 'bar' };
53 var obj = {'foo': 'bar' };
54 var obj = { baz: {'foo': 'qux'}, bar};
55 var obj = {baz: { 'foo': 'qux' }, bar};
56 ```
57
58 Examples of **correct** code for this rule with the default `"never"` option:
59
60 ```js
61 // When options are ["error", "never"]
62
63 foo['bar'];
64 foo[
65 'bar'
66 ];
67 foo[
68 'bar'];
69
70 var arr = [];
71 var arr = ['foo', 'bar', 'baz'];
72 var arr = [['foo'], 'bar', 'baz'];
73 var arr = [
74 'foo',
75 'bar',
76 'baz'
77 ];
78
79 var arr = [
80 'foo',
81 'bar'];
82
83 var obj = {'foo': 'bar'};
84
85 var obj = {'foo': {'bar': 'baz'}, 'qux': 'quxx'};
86
87 var obj = {
88 'foo': 'bar'
89 };
90 var obj = {'foo': 'bar'
91 };
92 var obj = {
93 'foo':'bar'};
94
95 var obj = {};
96 ```
97
98 ### "always"
99
100 Examples of **incorrect** code for this rule with the `"always"` option:
101
102 ```js
103 /*eslint-env es6*/
104
105 foo['bar'];
106 foo['bar' ];
107 foo[ 'bar'];
108
109 var arr = ['foo', 'bar'];
110 var arr = ['foo', 'bar' ];
111 var arr = [ ['foo'], 'bar' ];
112 var arr = ['foo',
113 'bar'
114 ];
115
116 var arr = [
117 'foo',
118 'bar'];
119
120 var obj = {'foo': 'bar'};
121 var obj = {'foo': 'bar' };
122 var obj = { baz: {'foo': 'qux'}, bar};
123 var obj = {baz: { 'foo': 'qux' }, bar};
124 var obj = {'foo': 'bar'
125 };
126
127 var obj = {
128 'foo':'bar'};
129 ```
130
131 Examples of **correct** code for this rule with the `"always"` option:
132
133 ```js
134 foo[ 'bar' ];
135 foo[
136 'bar'
137 ];
138
139 var arr = [];
140 var arr = [ 'foo', 'bar', 'baz' ];
141 var arr = [ [ 'foo' ], 'bar', 'baz' ];
142
143 var arr = [
144 'foo',
145 'bar',
146 'baz'
147 ];
148
149 var obj = {};
150 var obj = { 'foo': 'bar' };
151 var obj = { 'foo': { 'bar': 'baz' }, 'qux': 'quxx' };
152 var obj = {
153 'foo': 'bar'
154 };
155 ```
156
157 Note that `"always"` has a special case where `{}` and `[]` are not considered problems.
158
159 ### Exceptions
160
161 An 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.
162
163 You can add exceptions like so:
164
165 In case of `"always"` option, set an exception to `false` to enable it:
166
167 ```json
168 "space-in-brackets": ["error", "always", {
169 "singleValue": false,
170 "objectsInArrays": false,
171 "arraysInArrays": false,
172 "arraysInObjects": false,
173 "objectsInObjects": false,
174 "propertyName": false
175 }]
176 ```
177
178 In case of `"never"` option, set an exception to `true` to enable it:
179
180 ```json
181 "space-in-brackets": ["error", "never", {
182 "singleValue": true,
183 "objectsInArrays": true,
184 "arraysInArrays": true,
185 "arraysInObjects": true,
186 "objectsInObjects": true,
187 "propertyName": true
188 }]
189 ```
190
191 The following exceptions are available:
192
193 * `singleValue` sets the spacing of a single value inside of square brackets of an array.
194 * `objectsInArrays` sets the spacings between the curly braces and square brackets of object literals that are the first or last element in an array.
195 * `arraysInArrays` sets the spacing between the square brackets of array literals that are the first or last element in an array.
196 * `arraysInObjects` sets the spacing between the square bracket and the curly brace of an array literal that is the last element in an object.
197 * `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.
198 * `propertyName` sets the spacing in square brackets of computed member expressions.
199
200 In each of the following examples, the `"always"` option is assumed.
201
202 Examples of **incorrect** code for this rule when `"singleValue"` is set to `false`:
203
204 ```js
205 var foo = [ 'foo' ];
206 var foo = [ 'foo'];
207 var foo = ['foo' ];
208 var foo = [ 1 ];
209 var foo = [ 1];
210 var foo = [1 ];
211 var foo = [ [ 1, 2 ] ];
212 var foo = [ { 'foo': 'bar' } ];
213 ```
214
215 Examples of **correct** code for this rule when `"singleValue"` is set to `false`:
216
217 ```js
218 var foo = ['foo'];
219 var foo = [1];
220 var foo = [[ 1, 1 ]];
221 var foo = [{ 'foo': 'bar' }];
222 ```
223
224 Examples of **incorrect** code when `"objectsInArrays"` is set to `false`:
225
226 ```js
227 var arr = [ { 'foo': 'bar' } ];
228 var arr = [ {
229 'foo': 'bar'
230 } ]
231 ```
232
233 Examples of **correct** code when `"objectsInArrays"` is set to `false`:
234
235 ```js
236 var arr = [{ 'foo': 'bar' }];
237 var arr = [{
238 'foo': 'bar'
239 }];
240 ```
241
242 Examples of **incorrect** code when `"arraysInArrays"` is set to `false`:
243
244 ```js
245 var arr = [ [ 1, 2 ], 2, 3, 4 ];
246 var arr = [ [ 1, 2 ], 2, [ 3, 4 ] ];
247 ```
248
249 Examples of **correct** code when `"arraysInArrays"` is set to `false`:
250
251 ```js
252 var arr = [[ 1, 2 ], 2, 3, 4 ];
253 var arr = [[ 1, 2 ], 2, [ 3, 4 ]];
254 ```
255
256 Examples of **incorrect** code when `"arraysInObjects"` is set to `false`:
257
258 ```js
259 var obj = { "foo": [ 1, 2 ] };
260 var obj = { "foo": [ "baz", "bar" ] };
261 ```
262
263 Examples of **correct** code when `"arraysInObjects"` is set to `false`:
264
265 ```js
266 var obj = { "foo": [ 1, 2 ]};
267 var obj = { "foo": [ "baz", "bar" ]};
268 ```
269
270 Examples of **incorrect** code when `"objectsInObjects"` is set to `false`:
271
272 ```js
273 var obj = { "foo": { "baz": 1, "bar": 2 } };
274 var obj = { "foo": [ "baz", "bar" ], "qux": { "baz": 1, "bar": 2 } };
275 ```
276
277 Examples of **correct** code when `"objectsInObjects"` is set to `false`:
278
279 ```js
280 var obj = { "foo": { "baz": 1, "bar": 2 }};
281 var obj = { "foo": [ "baz", "bar" ], "qux": { "baz": 1, "bar": 2 }};
282 ```
283
284 Examples of **incorrect** code when `"propertyName"` is set to `false`:
285
286 ```js
287 var foo = obj[ 1 ];
288 var foo = obj[ bar ];
289 ```
290
291 Examples of **correct** code when `"propertyName"` is set to `false`:
292
293 ```js
294 var foo = obj[bar];
295 var foo = obj[0, 1];
296 ```
297
298 ## When Not To Use It
299
300 You can turn this rule off if you are not concerned with the consistency of spacing between brackets.
301
302 ## Related Rules
303
304 * [array-bracket-spacing](array-bracket-spacing.md)
305 * [object-curly-spacing](object-curly-spacing.md)
306 * [space-in-parens](space-in-parens.md)
307 * [computed-property-spacing](computed-property-spacing.md)