enforce line breaks after opening and before closing array brackets (array-bracket-newline)

在数组开括号后和闭括号前强制换行 (array-bracket-newline)

The --fix option on the command line can automatically fix some of the problems reported by this rule.

命令行中的 --fix 选项可以自动修复一些该规则报告的问题。

A number of style guides require or disallow line breaks inside of array brackets.

很多风格指南要求或禁止在数组括号内出现换行。

Rule Details

This rule enforces line breaks after opening and before closing array brackets.

该规则强制在数组开括号后和闭括号前出现换行。

Options

This rule has either a string option:

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

Or an object option (Requires line breaks if any of properties is satisfied. Otherwise, disallows line breaks):

或一个对象选项(只要有任何一个属性满足条件,要求换行。否则,禁止换行):

always

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

选项 "always"错误 代码示例:

/*eslint array-bracket-newline: ["error", "always"]*/

var a = [];
var b = [1];
var c = [1, 2];
var d = [1,
    2];
var e = [function foo() {
    dosomething();
}];

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

选项 "always"正确 代码示例:

/*eslint array-bracket-newline: ["error", "always"]*/

var a = [
];
var b = [
    1
];
var c = [
    1, 2
];
var d = [
    1,
    2
];
var e = [
    function foo() {
        dosomething();
    }
];

never

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

选项 "never"错误 代码示例:

/*eslint array-bracket-newline: ["error", "never"]*/

var a = [
];
var b = [
    1
];
var c = [
    1, 2
];
var d = [
    1,
    2
];
var e = [
    function foo() {
        dosomething();
    }
];

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

选项 "never"正确 代码示例:

/*eslint array-bracket-newline: ["error", "never"]*/

var a = [];
var b = [1];
var c = [1, 2];
var d = [1,
    2];
var e = [function foo() {
    dosomething();
}];

consistent

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

选项 "consistent"错误 代码示例:

/*eslint array-bracket-newline: ["error", "consistent"]*/

var a = [1
];
var b = [
    1];
var c = [function foo() {
    dosomething();
}
]
var d = [
    function foo() {
        dosomething();
    }]

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

选项 "consistent"正确 代码示例:

/*eslint array-bracket-newline: ["error", "consistent"]*/

var a = [];
var b = [
];
var c = [1];
var d = [
    1
];
var e = [function foo() {
    dosomething();
}];
var f = [
    function foo() {
        dosomething();
    }
];

multiline

Examples of incorrect code for this rule with the default { "multiline": true } option:

默认选项 { "multiline": true }错误 代码示例:

/*eslint array-bracket-newline: ["error", { "multiline": true }]*/

var a = [
];
var b = [
    1
];
var c = [
    1, 2
];
var d = [1,
    2];
var e = [function foo() {
    dosomething();
}];

Examples of correct code for this rule with the default { "multiline": true } option:

默认选项 { "multiline": true }正确 代码示例:

/*eslint array-bracket-newline: ["error", { "multiline": true }]*/

var a = [];
var b = [1];
var c = [1, 2];
var d = [
    1,
    2
];
var e = [
    function foo() {
        dosomething();
    }
];

minItems

Examples of incorrect code for this rule with the { "minItems": 2 } option:

选项 { "minItems": 2 }错误 代码示例:

/*eslint array-bracket-newline: ["error", { "minItems": 2 }]*/

var a = [
];
var b = [
    1
];
var c = [1, 2];
var d = [1,
    2];
var e = [
  function foo() {
    dosomething();
  }
];

Examples of correct code for this rule with the { "minItems": 2 } option:

选项 { "minItems": 2 }正确 代码示例:

/*eslint array-bracket-newline: ["error", { "minItems": 2 }]*/

var a = [];
var b = [1];
var c = [
    1, 2
];
var d = [
    1,
    2
];
var e = [function foo() {
    dosomething();
}];

multiline and minItems

Examples of incorrect code for this rule with the { "multiline": true, "minItems": 2 } options:

选项 { "multiline": true, "minItems": 2 }错误 代码示例:

/*eslint array-bracket-newline: ["error", { "multiline": true, "minItems": 2 }]*/

var a = [
];
var b = [
    1
];
var c = [1, 2];
var d = [1,
    2];
var e = [function foo() {
    dosomething();
}];

Examples of correct code for this rule with the { "multiline": true, "minItems": 2 } options:

选项 { "multiline": true, "minItems": 2 }正确 代码示例:

/*eslint array-bracket-newline: ["error", { "multiline": true, "minItems": 2 }]*/

var a = [];
var b = [1];
var c = [
    1, 2
];
var d = [
    1,
    2
];
var e = [
    function foo() {
        dosomething();
    }
];

When Not To Use It

If you don’t want to enforce line breaks after opening and before closing array brackets, don’t enable this rule.

如果你不想在数组开括号后和闭括号前强制换行,不要启用此规则。

Compatibility

Version

This rule was introduced in ESLint 4.0.0-alpha.1.

该规则在 ESLint 4.0.0-alpha.1 中被引入。

Resources