Index

space-in-brackets

在对象字面的大括号和数组字面的括号内强制保持一致的间距。

🌐 Enforces consistent spacing inside braces of object literals and brackets of array literals.

虽然格式偏好非常个人化,但许多风格指南要求或不允许括号之间有空格:

🌐 While formatting preferences are very personal, a number of style guides require or disallow spaces between brackets:

var obj = { foo: 'bar' };
var arr = [ 'foo', 'bar' ];
foo[ 'bar' ];

var obj = {foo: 'bar'};
var arr = ['foo', 'bar'];
foo['bar'];

规则详情

🌐 Rule Details

该规则旨在保持方括号内部空格的一致性,要么禁止方括号与其他标记之间的空格,要么强制使用空格。从相邻值换行隔开的括号不受此规则限制,因为这是常见的模式。作为数组的第一个或最后一个元素使用的对象字面量也会被忽略。

🌐 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.

选项

🌐 Options

此规则有两个选项:

🌐 There are two options for this rule:

  • "always" 强制在对象和数组字面量内部留空格
  • "never" 强制在对象和数组字面量内部不使用空格(默认)

根据你的编码约定,你可以通过在配置中指定它来选择任一选项:

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

"space-in-brackets": ["error", "always"]

“never”

使用默认 "never" 选项时,该规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the default "never" option:

foo[ 'bar' ];
foo['bar' ];

var arr = [ 'foo', 'bar' ];
var arr = ['foo', 'bar' ];
var arr = [ ['foo'], 'bar'];
var arr = [[ 'foo' ], 'bar'];
var arr = ['foo',
  'bar'
];

var obj = { 'foo': 'bar' };
var obj = {'foo': 'bar' };
var obj = { baz: {'foo': 'qux'}, bar};
var obj = {baz: { 'foo': 'qux' }, bar};

使用默认 "never" 选项时,该规则的正确代码示例:

🌐 Examples of correct code for this rule with the default "never" option:

// When options are ["error", "never"]

foo['bar'];
foo[
  'bar'
];
foo[
  'bar'];

var arr = [];
var arr = ['foo', 'bar', 'baz'];
var arr = [['foo'], 'bar', 'baz'];
var arr = [
  'foo',
  'bar',
  'baz'
];

var arr = [
  'foo',
  'bar'];

var obj = {'foo': 'bar'};

var obj = {'foo': {'bar': 'baz'}, 'qux': 'quxx'};

var obj = {
  'foo': 'bar'
};
var obj = {'foo': 'bar'
};
var obj = {
  'foo':'bar'};

var obj = {};

“always”

使用 "always" 选项时违反此规则的错误代码示例:

🌐 Examples of incorrect code for this rule with the "always" option:

foo['bar'];
foo['bar' ];
foo[ 'bar'];

var arr = ['foo', 'bar'];
var arr = ['foo', 'bar' ];
var arr = [ ['foo'], 'bar' ];
var arr = ['foo',
  'bar'
];

var arr = [
  'foo',
  'bar'];

var obj = {'foo': 'bar'};
var obj = {'foo': 'bar' };
var obj = { baz: {'foo': 'qux'}, bar};
var obj = {baz: { 'foo': 'qux' }, bar};
var obj = {'foo': 'bar'
};

var obj = {
  'foo':'bar'};

使用 "always" 选项时,此规则的正确代码示例:

🌐 Examples of correct code for this rule with the "always" option:

foo[ 'bar' ];
foo[
  'bar'
];

var arr = [];
var arr = [ 'foo', 'bar', 'baz' ];
var arr = [ [ 'foo' ], 'bar', 'baz' ];

var arr = [
  'foo',
  'bar',
  'baz'
];

var obj = {};
var obj = { 'foo': 'bar' };
var obj = { 'foo': { 'bar': 'baz' }, 'qux': 'quxx' };
var obj = {
  'foo': 'bar'
};

请注意,"always" 有一种特殊情况,其中 {}[] 不被视为问题。

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

异常

🌐 Exceptions

可以使用对象字面量作为第三个数组项来指定间距例外。这些例外在第一个选项的上下文中生效。也就是说,如果将 "always" 设置为强制间距,并将某个例外设置为 false,则会禁止匹配该例外的情况的间距。同样,如果将 "never" 设置为禁止间距,并将某个例外设置为 true,则会强制匹配该例外的情况使用间距。

🌐 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.

你可以像这样添加例外:

🌐 You can add exceptions like so:

"always" 选项的情况下,设置 false 的例外以启用它:

🌐 In case of "always" option, set an exception to false to enable it:

"space-in-brackets": ["error", "always", {
  "singleValue": false,
  "objectsInArrays": false,
  "arraysInArrays": false,
  "arraysInObjects": false,
  "objectsInObjects": false,
  "propertyName": false
}]

"never" 选项的情况下,设置 true 的例外以启用它:

🌐 In case of "never" option, set an exception to true to enable it:

"space-in-brackets": ["error", "never", {
  "singleValue": true,
  "objectsInArrays": true,
  "arraysInArrays": true,
  "arraysInObjects": true,
  "objectsInObjects": true,
  "propertyName": true
}]

以下例外情况可用:

🌐 The following exceptions are available:

  • singleValue 设置数组中方括号内单个值的间距。
  • objectsInArrays 设置数组中第一个或最后一个元素的对象字面量的大括号和方括号之间的间距。
  • arraysInArrays 设置数组字面量中作为数组第一个或最后一个元素的方括号之间的间距。
  • arraysInObjects 设置对象中作为最后一个元素的数组字面量的方括号与大括号之间的间距。
  • objectsInObjects 设置对象字面量中最后一个元素的大括号与包含该对象的大括号之间的间距。
  • propertyName 设置计算成员表达式中方括号的间距。

在以下每个示例中,假设使用 "always" 选项。

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

"singleValue" 设置为 false 时,此规则的 错误 代码示例:

🌐 Examples of incorrect code for this rule when "singleValue" is set to false:

var foo = [ 'foo' ];
var foo = [ 'foo'];
var foo = ['foo' ];
var foo = [ 1 ];
var foo = [ 1];
var foo = [1 ];
var foo = [ [ 1, 2 ] ];
var foo = [ { 'foo': 'bar' } ];

"singleValue" 设置为 false 时,此规则的正确代码示例:

🌐 Examples of correct code for this rule when "singleValue" is set to false:

var foo = ['foo'];
var foo = [1];
var foo = [[ 1, 1 ]];
var foo = [{ 'foo': 'bar' }];

"objectsInArrays" 设置为 false 时的 不正确 代码示例:

🌐 Examples of incorrect code when "objectsInArrays" is set to false:

var arr = [ { 'foo': 'bar' } ];
var arr = [ {
  'foo': 'bar'
} ]

"objectsInArrays" 设置为 false 时的 正确 代码示例:

🌐 Examples of correct code when "objectsInArrays" is set to false:

var arr = [{ 'foo': 'bar' }];
var arr = [{
  'foo': 'bar'
}];

"arraysInArrays" 设置为 false 时的 不正确 代码示例:

🌐 Examples of incorrect code when "arraysInArrays" is set to false:

var arr = [ [ 1, 2 ], 2, 3, 4 ];
var arr = [ [ 1, 2 ], 2, [ 3, 4 ] ];

"arraysInArrays" 设置为 false 时的 正确 代码示例:

🌐 Examples of correct code when "arraysInArrays" is set to false:

var arr = [[ 1, 2 ], 2, 3, 4 ];
var arr = [[ 1, 2 ], 2, [ 3, 4 ]];

"arraysInObjects" 设置为 false 时的 不正确 代码示例:

🌐 Examples of incorrect code when "arraysInObjects" is set to false:

var obj = { "foo": [ 1, 2 ] };
var obj = { "foo": [ "baz", "bar" ] };

"arraysInObjects" 设置为 false 时的 正确 代码示例:

🌐 Examples of correct code when "arraysInObjects" is set to false:

var obj = { "foo": [ 1, 2 ]};
var obj = { "foo": [ "baz", "bar" ]};

"objectsInObjects" 设置为 false 时的 不正确 代码示例:

🌐 Examples of incorrect code when "objectsInObjects" is set to false:

var obj = { "foo": { "baz": 1, "bar": 2 } };
var obj = { "foo": [ "baz", "bar" ], "qux": { "baz": 1, "bar": 2 } };

"objectsInObjects" 设置为 false 时的 正确 代码示例:

🌐 Examples of correct code when "objectsInObjects" is set to false:

var obj = { "foo": { "baz": 1, "bar": 2 }};
var obj = { "foo": [ "baz", "bar" ], "qux": { "baz": 1, "bar": 2 }};

"propertyName" 设置为 false 时的 不正确 代码示例:

🌐 Examples of incorrect code when "propertyName" is set to false:

var foo = obj[ 1 ];
var foo = obj[ bar ];

"propertyName" 设置为 false 时的 正确 代码示例:

🌐 Examples of correct code when "propertyName" is set to false:

var foo = obj[bar];
var foo = obj[0, 1];

何时不使用

🌐 When Not To Use It

如果你不关心括号间距的一致性,你可以关闭此规则。

🌐 You can turn this rule off if you are not concerned with the consistency of spacing between brackets.

版本

此规则是在 ESLint v0.4.1 中引入,并在 v1.0.0-rc-1 中删除。