1 # space-in-brackets: enforce consistent spacing inside braces of object literals and brackets of array literals
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.
5 While formatting preferences are very personal, a number of style guides require or disallow spaces between brackets:
8 var obj = { foo: 'bar' };
9 var arr = [ 'foo', 'bar' ];
12 var obj = {foo: 'bar'};
13 var arr = ['foo', 'bar'];
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.
23 There are two options for this rule:
25 * `"always"` enforces a space inside of object and array literals
26 * `"never"` enforces zero spaces inside of object and array literals (default)
28 Depending on your coding conventions, you can choose either option by specifying it in your configuration:
31 "space-in-brackets": ["error", "always"]
36 Examples of **incorrect** code for this rule with the default `"never"` option:
44 var arr = [ 'foo', 'bar' ];
45 var arr = ['foo', 'bar' ];
46 var arr = [ ['foo'], 'bar'];
47 var arr = [[ 'foo' ], 'bar'];
52 var obj = { 'foo': 'bar' };
53 var obj = {'foo': 'bar' };
54 var obj = { baz: {'foo': 'qux'}, bar};
55 var obj = {baz: { 'foo': 'qux' }, bar};
58 Examples of **correct** code for this rule with the default `"never"` option:
61 // When options are ["error", "never"]
71 var arr = ['foo', 'bar', 'baz'];
72 var arr = [['foo'], 'bar', 'baz'];
83 var obj = {'foo': 'bar'};
85 var obj = {'foo': {'bar': 'baz'}, 'qux': 'quxx'};
90 var obj = {'foo': 'bar'
100 Examples of **incorrect** code for this rule with the `"always"` option:
109 var arr = ['foo', 'bar'];
110 var arr = ['foo', 'bar' ];
111 var arr = [ ['foo'], 'bar' ];
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'
131 Examples of **correct** code for this rule with the `"always"` option:
140 var arr = [ 'foo', 'bar', 'baz' ];
141 var arr = [ [ 'foo' ], 'bar', 'baz' ];
150 var obj = { 'foo': 'bar' };
151 var obj = { 'foo': { 'bar': 'baz' }, 'qux': 'quxx' };
157 Note that `"always"` has a special case where `{}` and `[]` are not considered problems.
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.
163 You can add exceptions like so:
165 In case of `"always"` option, set an exception to `false` to enable it:
168 "space-in-brackets": ["error", "always", {
169 "singleValue": false,
170 "objectsInArrays": false,
171 "arraysInArrays": false,
172 "arraysInObjects": false,
173 "objectsInObjects": false,
174 "propertyName": false
178 In case of `"never"` option, set an exception to `true` to enable it:
181 "space-in-brackets": ["error", "never", {
183 "objectsInArrays": true,
184 "arraysInArrays": true,
185 "arraysInObjects": true,
186 "objectsInObjects": true,
191 The following exceptions are available:
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.
200 In each of the following examples, the `"always"` option is assumed.
202 Examples of **incorrect** code for this rule when `"singleValue"` is set to `false`:
211 var foo = [ [ 1, 2 ] ];
212 var foo = [ { 'foo': 'bar' } ];
215 Examples of **correct** code for this rule when `"singleValue"` is set to `false`:
220 var foo = [[ 1, 1 ]];
221 var foo = [{ 'foo': 'bar' }];
224 Examples of **incorrect** code when `"objectsInArrays"` is set to `false`:
227 var arr = [ { 'foo': 'bar' } ];
233 Examples of **correct** code when `"objectsInArrays"` is set to `false`:
236 var arr = [{ 'foo': 'bar' }];
242 Examples of **incorrect** code when `"arraysInArrays"` is set to `false`:
245 var arr = [ [ 1, 2 ], 2, 3, 4 ];
246 var arr = [ [ 1, 2 ], 2, [ 3, 4 ] ];
249 Examples of **correct** code when `"arraysInArrays"` is set to `false`:
252 var arr = [[ 1, 2 ], 2, 3, 4 ];
253 var arr = [[ 1, 2 ], 2, [ 3, 4 ]];
256 Examples of **incorrect** code when `"arraysInObjects"` is set to `false`:
259 var obj = { "foo": [ 1, 2 ] };
260 var obj = { "foo": [ "baz", "bar" ] };
263 Examples of **correct** code when `"arraysInObjects"` is set to `false`:
266 var obj = { "foo": [ 1, 2 ]};
267 var obj = { "foo": [ "baz", "bar" ]};
270 Examples of **incorrect** code when `"objectsInObjects"` is set to `false`:
273 var obj = { "foo": { "baz": 1, "bar": 2 } };
274 var obj = { "foo": [ "baz", "bar" ], "qux": { "baz": 1, "bar": 2 } };
277 Examples of **correct** code when `"objectsInObjects"` is set to `false`:
280 var obj = { "foo": { "baz": 1, "bar": 2 }};
281 var obj = { "foo": [ "baz", "bar" ], "qux": { "baz": 1, "bar": 2 }};
284 Examples of **incorrect** code when `"propertyName"` is set to `false`:
288 var foo = obj[ bar ];
291 Examples of **correct** code when `"propertyName"` is set to `false`:
298 ## When Not To Use It
300 You can turn this rule off if you are not concerned with the consistency of spacing between brackets.
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)