space-in-brackets

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

(已删除)此规则在 ESLint v1.0 中已删除,并由 object-curly-spacingarray-bracket-spacing 规则替换。

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

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

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

规则详情

此规则旨在保持方括号内间距的一致性,方法是禁止括号内的空格和其他标记之间的括号内的空格或强制空格。用新行与相邻值分隔的括号不适用此规则,因为这是一种常见模式。用作数组中第一个或最后一个元素的对象字面也将被忽略。

选项

此规则有两个选项:

    vqZv+QBNs/gSFuv/ivaAMXcTxzEQ1bmu2yupFGXRbED5gzhHHOrt+u96k8tdL8a2WxrKV2Ed8gucOX9h4e6GB2aKBQu71NJB7segDSgszmapJOZ8i5wIS5J4UZUhSMO5RCmv77nAT45S0FXtrm1aOcV0iJkx29WyLmxUBkC82Oj5e39tVJdp0vSuO3kOY6meOalRc3y4Lm9lr1Bjs+sDa4Cen1BxerLZ4yL6x6V1KR4=

VaHUmm7uD+hTTWHYb9bVo2j0NKJvVKjKUXbye3iPlFsVLgiNKuNIUW6RoNe+Ii2FfD83AF7Y+pbxhWAx9v/4JbVHgDMn/T6lbm8q2QufaVhwXQa1+sOGPZelYPHtuHt3

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

"never"

qwlXDFeOGAub+EzAJFeRQSlZSvSnzZWorZEGjnED1suiH3+xmlvSuv/Apb3xSCZZoV+H80m0VDNN3cIdedLj2uCY0X/LA92MOdGT5ILQfPs=

/*eslint-env es6*/

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};

qwlXDFeOGAub+EzAJFeRQSlZSvSnzZWorZEGjnED1suiH3+xmlvSuv/Apb3xSCZZhp3Fx4XVWlQ/VYlYzazEcZYAo2G+H6zeVFz9vw3tPyE=

// 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"

yqV+SV60QvsOsYXkYvZsZSN35u2BEfk7UzqdB7xGwBXAxMLq9tHsIQE7501GmDQVOKLwOEc6SBB1Q7CuPjXRTvRf6BwRkCRaFSPkKC/ITPA=

/*eslint-env es6*/

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'};

yqV+SV60QvsOsYXkYvZsZSN35u2BEfk7UzqdB7xGwBVZSC+x4VbKrC6dmOngM9JtEZ9HS3DKi+EIk962kYJOsv7sY7JBZk+4gKi/h2XVZbw=

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'
};

kaHWgnQB/AlZV+28ljVQ0K4G+6RxdW8jugOhMOU+i5eI27JgFp16T8PwjYo8nH0gbpg/jnaM2FhXszjP1zEpbe40+WI1YFqgRQLG/TJDhJOlmTl81rJtd3ITsoMD3RFcoPnILJoOmhgZui7LTS5cK1MX2kfnbla2kOAC38c1m2Q=

异常

ONdVfxA7Sfc9aT02vdluGKx6yqjTVv59rYB4wL/6qVCJSoAHRXEePeLhduc0f94S1mKZwafDb19BiKAlNNYgjSz4G6uQNQMgyprsAXA4xjXtEEPVGcrkw6O9m9YkGtwpzsTbvewHZYHdlT7NXVtMnfE6m8KICn8ho5zYlIJwtljJCVoU7U7qDr7LKDEyc1vx2AW9aX0G4i8cUaKD+HY7OT13cnwERWzHjUfsiMhUf6oIrzL0AaXcpuCuOsP6cC/NOYsMQLbU3wj33Gf4zyeOHZTwu2K+ItuBGi+fi77jY/qMKtNy55B/HMmCSWwgmOhd6TUdRazUEgR+ZkhbLUjiCX4swwr7Bo6dqRbH74rOhca0jxSEnNZSowanBIBHLHIN1UChkiUDrNcF0WuiSsndE9KXSXZNZv44cAg+JcCfPR8/UfaUP0/vpYnVjDffke4hTt/XfTdIvpn0SgYufUwN5iUxxRBe4/qofh7Wiae8tE+E3fY88k5/95I4uf4NBgOIw6Xq0ejc5DA3drP/87yt4Js40oANa8ev/+23gFXdl5Qj7dAHZ37elaFlBzZVo0Ydkz1Emshye6PHueFu9u8VHQ==

ySHI80WzK3QAvfXBqbcSGStICEcNM811vV2Xwah7tnKAO6sz8wwppMcQQKMn+ybx

/U7Fa3muWi/F3Zzs9XVFRJEOHayJhlIW2Y+9z7qYv5NiUgHsioSXO6o3zVIUAfLHJNS3eko51Z/QmP4BEa1fFvAtJ5C3s43KHhUwjRXPf5LUvjkutoNnpXvca+V6+L/NdM75V7pFeqcGVAxvtqmM0w==

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

NIExn/pchi+UJuWdKZ6p3IfNE/3J6Qs5bswcNRtXJCA3Kez0EYdVm3km6VMu6qeWV7EzMBTh+H6ux0VnNtJiUpB3NwF+M5OjZz2EJjJZg4hTmHVWBoWPi19zwvIR/Jr23UtxCQpvbMZT+mKZQhi4DA==

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

xI1DetXULEIGbUr9qvuZOT4UkGnAvOz4qFLqjuY/xOQ=

    qbypU1EcFFnjF2ixTrE0Rh99PsUGywh8MgCf36Lb+AbEIyFgVz/U4AUxx0pqcTTquOTBHhnx++hklhzqdAuTktN4vnsTGPL9lw6osJMiPU1ZHYbfeOpz/gsl3yrK0EAfcM70BIDwvTSUFWZTlBUaDYuGtvqPBjFdcyFyV5yIw/XL9G2B6iybSeOqeWZAPYJthwLNsrzd+JLpLJFfgSshyGXZCugdc0fRHx1XyCpQddXyQdcjPx8GnNID518CC13zCLuqENWeZxLuz1WD4mLJgmiBWVE9QVC+9IgN5M8tFMKeCsEIvv43Tu6iyhG7MON3tq860g+zzev6relzGa2W0OED6kwen8nvhk8oFSibXKvLlwoITJp4pW4Yul/0IIFx46oRRU5mwxrGymoqUGPzR/LcVAcdh2w1iSafXOYNFM6omuJuv1h5LPRiFVVXoKm2PG2XuHbJ3qQNAaoT0WB9znQtqOEeYtJkZJVE4KphqDE1XyAxoiTT8Vip/ywvDxP7ONUQJcSSrO3LYPcpRmU4auEp21ZiJoPIK+9eTqnmbh44mwv8CsJEi4YxwkpUKBQpbdQtgjUp5ROGaXHFE9GTIzEIr++4V75xUbRmXr885krNnQZHs25oDrCDIb2s8elfN3xsGgg5BTfmOLh2vO503AKjRvTjXvfzrJ89HFYSfOt3in3h4aIywgqpTDcP/uhJ0pgQD1E6tuDI19TKIJMnwG2i/KK1xGYXD+Q0KViZrde+qWaRKi3WU0im42Gs9Bd6y8XDC8ngv9FSu8fuEY1dBhuC08rUSOM1ABUXfUFK+lbLa3cRP3StK5oG0LlzZTACT3Hgyzf/MBNKzqhGStDaz748dOO8Rb9XK8oH+7RhOayCOIC7MKOd2D3iVQEvetAztu49VnBpQnX4GSUzmgU8k+z93YPCzGWKG/G8D/ucZ6TViU+vtDNiz/ICR3aiaArEQ8pUd6wrV3sK4xb8axKoQVpbVlIhoxrFrbcivwKau1At4GZCF+w5GNO2iRdBLELI

HAzJWIiJnY8EW3Dy3Lfnr4EVA+ZaR5B1983aPUTte0emuaOk5IY8TFp581XZIZUKSaTCF9m6oLLJCmJ72BHyGMKs8O5KgYjdQFlx5oiPUWc=

SE8Qu9hmzyME4xklJOoyASZEobU5BJ3+iDnuvOghUIAiGaATx23HnZT+b0M9rrBOmeftcgDbTOAdCIWBmRA4YpsaGYEn8S0ceA2l47v+YECF9Ezj5whh/fw/ABi2yC/5SKutt/REcjbhgTvUByWA1g==

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' } ];

SE8Qu9hmzyME4xklJOoyASZEobU5BJ3+iDnuvOghUIAiGaATx23HnZT+b0M9rrBOmeftcgDbTOAdCIWBmRA4YhckqFFF97k6GNqNLDhqGmUEkn4CFxr4drWoUMh/5XUvQRZ/z07kWFzaR5Lm1NhmEQ==

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

0OzlFo+Y6h5j7Kf5K5SmwkK2VB1ky67+4uRfPZUrcR2y96Vfs8G26zIAxQEIwQtKDlPzxCisjR9YMP+9lSPtmGwI/AJ1EAQEHxeJuDT20XfBk5Qn/pRwMjCU4xb6Jeh1

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

0OzlFo+Y6h5j7Kf5K5SmwkK2VB1ky67+4uRfPZUrcR2y96Vfs8G26zIAxQEIwQtKDlPzxCisjR9YMP+9lSPtmKwK0FCr1SXELIdl9fS0GKlmex0aIG9MzwO1qT6PmxZV

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

Ne1IdXfrr1aplZ2XViDs8Nybduu+QbFQukpTxEUWZ9Gpeh6lsadCYv4AP+l0DC/S0nawEH3rHgRIprS1n0MzWXwitaglWszRi3NIh24iD+2XAOdh9FJanAtQ5bzIpFDy

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

Ne1IdXfrr1aplZ2XViDs8Nybduu+QbFQukpTxEUWZ9Gpeh6lsadCYv4AP+l0DC/S0nawEH3rHgRIprS1n0MzWeqakrcLcWXHCr82f4hkGBuKQUIN1MEySSHSi1LxgY0c

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

ncO/gnb2GvVKNVe5/FmaGG7Pf/EpJaeY4hFvHoEnE6ihwV85Bqz4oZY1m2TlGx83f8SYVR7VEJU6U/pqbmGoBJZYJNYCnasrJWGs0OXLbPKK5JyJKrJhNg1sRhy7q22P

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

ncO/gnb2GvVKNVe5/FmaGG7Pf/EpJaeY4hFvHoEnE6ihwV85Bqz4oZY1m2TlGx83f8SYVR7VEJU6U/pqbmGoBPUXInM1AmBWP4jK1Pksj7I0vVs4SeQm2QkItzMFrzVK

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

0OzlFo+Y6h5j7Kf5K5SmwqHO71w3PlyDPJD+oYr4QUmBBQUkA59rQSFGrAnDZ04Pw/sqi7Cee4HhAxjfthTd2yJhozvkZKZearSZ3GK6uDYTCI+L3xBiQjKlSFAPRq4A

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

0OzlFo+Y6h5j7Kf5K5SmwqHO71w3PlyDPJD+oYr4QUmBBQUkA59rQSFGrAnDZ04Pw/sqi7Cee4HhAxjfthTd2/NyHevzP/JuOlO5eVZIiw2g2lturC0MbzdYm5fdaFdB

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

lTdx0R+EW23wxlJLZ++aKeIN0TEzVEdJqlwXH4zanVN56mKpLPSEHoXc99yQ9vzXAjsFD/3d2SeFeuCAlbOr9DkaJ+Bkj00j5B9An2NcTrOjit4SO3nRSYDy4/ZDU+2J

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

lTdx0R+EW23wxlJLZ++aKeIN0TEzVEdJqlwXH4zanVN56mKpLPSEHoXc99yQ9vzXUdP8epjENRGzZeu6y91Q1iC4vtjIgdbLQI+J+4/IRhTsvOj2bjfNIu4OlsgGBDxy

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

何时不使用

uYEZVMMeEUwUwbTo3rC6SHGGRs+FDUuFhrd8+E292GlvYRz2VxGs/KiAWLx0HHStp26v4+Iin64NnzRnuKyM92pfdwGl63pgETO8vm1N5W8=