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"
强制在对象和数组字面量内部添加空格¥
"always"
enforces a space inside of object and array literals -
"never"
强制对象和数组字面量内部有零空格(默认)¥
"never"
enforces zero spaces inside of object and array literals (default)
根据你的编码约定,你可以通过在配置中指定它来选择任一选项:
¥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
设置数组方括号内单个值的间距。¥
singleValue
sets the spacing of a single value inside of square brackets of an array. -
objectsInArrays
设置对象字面量的大括号和方括号之间的间距,对象字面量是数组中的第一个或最后一个元素。¥
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
设置作为数组中第一个或最后一个元素的数组字面的方括号之间的间距。¥
arraysInArrays
sets the spacing between the square brackets of array literals that are the first or last element in an array. -
arraysInObjects
设置作为对象中最后一个元素的数组字面的方括号和大括号之间的间距。¥
arraysInObjects
sets the spacing between the square bracket and the curly brace of an array literal that is the last element in an object. -
objectsInObjects
设置作为对象中最后一个元素的对象字面量的大括号与包含对象的大括号之间的间距。¥
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. -
propertyName
设置计算成员表达式的方括号中的间距。¥
propertyName
sets the spacing in square brackets of computed member expressions.
在以下每个示例中,都假定了 "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 中删除。