array-bracket-spacing

在数组括号内强制执行一致的间距

🔧 Fixable

此规则报告的一些问题可通过 --fix 命令行选项自动修复

此规则在 ESLint v8.53.0 中已弃用。请在 @stylistic/eslint-plugin-js 中使用 相应的规则

¥This rule was deprecated in ESLint v8.53.0. Please use the corresponding rule in @stylistic/eslint-plugin-js.

许多风格指南要求或不允许在数组括号和其他标记之间使用空格。此规则适用于数组字面和解构赋值(ECMAScript 6)。

¥A number of style guides require or disallow spaces between array brackets and other tokens. This rule applies to both array literals and destructuring assignments (ECMAScript 6).

var arr = [ 'foo', 'bar' ];
var [ x, y ] = z;

var arr = ['foo', 'bar'];
var [x,y] = z;

规则详情

¥Rule Details

此规则强制数组括号内的间距一致。

¥This rule enforces consistent spacing inside array brackets.

选项

¥Options

此规则有一个字符串选项:

¥This rule has a string option:

  • "never"(默认)不允许数组括号内有空格

    ¥"never" (default) disallows spaces inside array brackets

  • "always" 需要在数组括号内有一个或多个空格或换行符

    ¥"always" requires one or more spaces or newlines inside array brackets

此规则有一个对象选项,用于 "never" 选项的例外情况:

¥This rule has an object option for exceptions to the "never" option:

  • "singleValue": true 需要在包含单个元素的数组字面量的括号内包含一个或多个空格或换行符

    ¥"singleValue": true requires one or more spaces or newlines inside brackets of array literals that contain a single element

  • "objectsInArrays": true 需要在数组字面量的括号与其对象字面量元素 [ {} ] 的大括号之间有一个或多个空格或换行符

    ¥"objectsInArrays": true requires one or more spaces or newlines between brackets of array literals and braces of their object literal elements [ { or } ]

  • "arraysInArrays": true 需要在数组字面量的括号与其数组字面量元素 [ [] ] 的括号之间有一个或多个空格或换行符

    ¥"arraysInArrays": true requires one or more spaces or newlines between brackets of array literals and brackets of their array literal elements [ [ or ] ]

此规则有一个对象选项,用于 "always" 选项的例外情况:

¥This rule has an object option for exceptions to the "always" option:

  • "singleValue": false 不允许包含单个元素的数组字面量的括号内有空格

    ¥"singleValue": false disallows spaces inside brackets of array literals that contain a single element

  • "objectsInArrays": false 不允许数组字面量的括号与其对象字面量元素 [{}] 的大括号之间有空格

    ¥"objectsInArrays": false disallows spaces between brackets of array literals and braces of their object literal elements [{ or }]

  • "arraysInArrays": false 不允许数组字面量的括号与其数组字面量元素 [[]] 的括号之间有空格

    ¥"arraysInArrays": false disallows spaces between brackets of array literals and brackets of their array literal elements [[ or ]]

此规则具有内置例外:

¥This rule has built-in exceptions:

  • "never"(以及 "always" 选项的例外)允许在数组括号内使用换行符,因为这是一种常见模式

    ¥"never" (and also the exceptions to the "always" option) allows newlines inside array brackets, because this is a common pattern

  • "always" 空数组字面量中不需要空格或换行符 []

    ¥"always" does not require spaces or newlines in empty array literals []

never

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

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

在线运行
/*eslint array-bracket-spacing: ["error", "never"]*/

var arr = [ 'foo', 'bar' ];
var arr = ['foo', 'bar' ];
var arr = [ ['foo'], 'bar'];
var arr = [[ 'foo' ], 'bar'];
var arr = [ 'foo',
  'bar'
];
var [ x, y ] = z;
var [ x,y ] = z;
var [ x, ...y ] = z;
var [ ,,x, ] = z;

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

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

在线运行
/*eslint array-bracket-spacing: ["error", "never"]*/

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

var [x, y] = z;
var [x,y] = z;
var [x, ...y] = z;
var [,,x,] = z;

always

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

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

在线运行
/*eslint array-bracket-spacing: ["error", "always"]*/

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

var [x, y] = z;
var [x,y] = z;
var [x, ...y] = z;
var [,,x,] = z;

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

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

在线运行
/*eslint array-bracket-spacing: ["error", "always"]*/

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

var [ x, y ] = z;
var [ x,y ] = z;
var [ x, ...y ] = z;
var [ ,,x, ] = z;

singleValue

使用 "always", { "singleValue": false } 选项的此规则的错误代码示例:

¥Examples of incorrect code for this rule with the "always", { "singleValue": false } options:

在线运行
/*eslint array-bracket-spacing: ["error", "always", { "singleValue": 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' } ];

使用 "always", { "singleValue": false } 选项的此规则的正确代码示例:

¥Examples of correct code for this rule with the "always", { "singleValue": false } options:

在线运行
/*eslint array-bracket-spacing: ["error", "always", { "singleValue": false }]*/

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

objectsInArrays

使用 "always", { "objectsInArrays": false } 选项的此规则的错误代码示例:

¥Examples of incorrect code for this rule with the "always", { "objectsInArrays": false } options:

在线运行
/*eslint array-bracket-spacing: ["error", "always", { "objectsInArrays": false }]*/

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

使用 "always", { "objectsInArrays": false } 选项的此规则的正确代码示例:

¥Examples of correct code for this rule with the "always", { "objectsInArrays": false } options:

在线运行
/*eslint array-bracket-spacing: ["error", "always", { "objectsInArrays": false }]*/

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

arraysInArrays

使用 "always", { "arraysInArrays": false } 选项的此规则的错误代码示例:

¥Examples of incorrect code for this rule with the "always", { "arraysInArrays": false } options:

在线运行
/*eslint array-bracket-spacing: ["error", "always", { "arraysInArrays": false }]*/

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

使用 "always", { "arraysInArrays": false } 选项的此规则的正确代码示例:

¥Examples of correct code for this rule with the "always", { "arraysInArrays": false } options:

在线运行
/*eslint array-bracket-spacing: ["error", "always", { "arraysInArrays": false }]*/

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

何时不使用

¥When Not To Use It

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

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

版本

此规则是在 ESLint v0.24.0 中引入。

资源

ESLint 中文网
粤ICP备13048890号