Fixable: This rule is automatically fixable using the --fix flag on the command line.

Rule Details

This rule aims to maintain consistency around the spacing inside of array 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.

Options

There are two options for this rule:

"always" enforces a space inside of array brackets

"never" enforces no space inside of array brackets (default)

Depending on your coding conventions, you can choose either option by specifying it in your configuration:

"array-bracket-spacing":[2,"always"]

“never”

When "never" is set, the following patterns are considered problems:

/*eslint array-bracket-spacing: [2, "never"]*//*eslint-env es6*/vararr=['foo','bar'];/*error There should be no space after '['*//*error There should be no space before ']'*/vararr=['foo','bar'];/*error There should be no space before ']'*/vararr=[['foo'],'bar'];/*error There should be no space after '['*/vararr=[['foo'],'bar'];/*error There should be no space after '['*//*error There should be no space before ']'*/vararr=['foo','bar'];var[x,y]=z;/*error There should be no space after '['*//*error There should be no space before ']'*/var[x,y]=z;/*error There should be no space after '['*//*error There should be no space before ']'*/var[x,...y]=z;/*error There should be no space after '['*//*error There should be no space before ']'*/var[,,x,]=z;/*error There should be no space after '['*//*error There should be no space before ']'*/

“always”

When "always" is used, the following patterns are considered problems:

/*eslint array-bracket-spacing: [2, "always"]*//*eslint-env es6*/vararr=['foo','bar'];/*error A space is required after '['*//*error A space is required before ']'*/vararr=['foo','bar'];/*error A space is required after '['*/vararr=[['foo'],'bar'];/*error A space is required after '['*//*error A space is required before ']'*/vararr=['foo',/*error A space is required after '['*/'bar'];vararr=['foo','bar'];/*error A space is required before ']'*/var[x,y]=z;/*error A space is required after '['*//*error A space is required before ']'*/var[x,y]=z;/*error A space is required after '['*//*error A space is required before ']'*/var[x,...y]=z;/*error A space is required after '['*//*error A space is required before ']'*/var[,,x,]=z;/*error A space is required after '['*//*error A space is required before ']'*/

Note that "always" has a special case where {} and [] are not considered problems.

Exceptions

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.

singleValue sets the spacing of a single value inside of square brackets of an array.

objectsInArrays sets the spacings between the curly braces and square brackets of object literals that are the first or last element in an array.

arraysInArrays sets the spacing between the square brackets of array literals that are the first or last element in an array.

In each of the following examples, the "always" option is assumed.

When "singleValue" is set to false, the following patterns are considered problems:

/*eslint array-bracket-spacing: [2, "always", { singleValue: false }]*/varfoo=['foo'];/*error There should be no space after '['*//*error There should be no space before ']'*/varfoo=['foo'];/*error There should be no space after '['*/varfoo=['foo'];/*error There should be no space before ']'*/varfoo=[1];/*error There should be no space after '['*//*error There should be no space before ']'*/varfoo=[1];/*error There should be no space after '['*/varfoo=[1];/*error There should be no space before ']'*/varfoo=[[1,2]];/*error There should be no space after '['*//*error There should be no space before ']'*/varfoo=[{'foo':'bar'}];/*error There should be no space after '['*//*error There should be no space before ']'*/

When "objectsInArrays" is set to false, the following patterns are considered problems:

/*eslint array-bracket-spacing: [2, "always", { objectsInArrays: false }]*/vararr=[{'foo':'bar'}];/*error There should be no space after '['*//*error There should be no space before ']'*/vararr=[{/*error There should be no space after '['*/'foo':'bar'}]/*error There should be no space before ']'*/

When "arraysInArrays" is set to false, the following patterns are considered problems:

/*eslint array-bracket-spacing: [2, "always", { arraysInArrays: false }]*/vararr=[[1,2],2,3,4];/*error There should be no space after '['*/vararr=[[1,2],2,[3,4]];/*error There should be no space after '['*//*error There should be no space before ']'*/