array-bracket-newline

在打开数组括号之后和关闭数组括号之前强制换行

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

许多风格指南要求或不允许在数组括号内换行。

¥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:

  • "always" 需要在括号内换行

    ¥"always" requires line breaks inside brackets

  • "never" 不允许括号内换行

    ¥"never" disallows line breaks inside brackets

  • "consistent" 要求每对括号一致地使用换行符。如果一对中的一个括号内有换行符,而另一个括号内没有,它会报告错误。

    ¥"consistent" requires consistent usage of linebreaks for each pair of brackets. It reports an error if one bracket in the pair has a linebreak inside it and the other bracket does not.

或者一个对象选项(如果满足任何属性,则需要换行符。否则,不允许换行符):

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

  • 如果元素内部或元素之间有换行符,"multiline": true(默认)需要换行符。如果为假,则禁用此条件。

    ¥"multiline": true (default) requires line breaks if there are line breaks inside elements or between elements. If this is false, this condition is disabled.

  • 如果元素数量至少为给定整数,"minItems": null(默认)需要换行符。如果为 0,则此条件的作用与选项 "always" 相同。如果这是 null(默认值),则禁用此条件。

    ¥"minItems": null (default) requires line breaks if the number of elements is at least the given integer. If this is 0, this condition will act the same as the option "always". If this is null (the default), this condition is disabled.

always

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

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

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

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

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

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

在线运行
/*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

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

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

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

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

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

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

在线运行
/*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

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

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

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

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

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

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

在线运行
/*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

使用默认 { "multiline": true } 选项的此规则的错误代码示例:

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

在线运行
/*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();
}];

使用默认 { "multiline": true } 选项的此规则的正确代码示例:

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

在线运行
/*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

使用 { "minItems": 2 } 选项的此规则的错误代码示例:

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

在线运行
/*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();
  }
];

使用 { "minItems": 2 } 选项的此规则的正确代码示例:

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

在线运行
/*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 和 minItems

¥multiline and minItems

使用 { "multiline": true, "minItems": 2 } 选项的此规则的错误代码示例:

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

在线运行
/*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();
}];

使用 { "multiline": true, "minItems": 2 } 选项的此规则的正确代码示例:

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

在线运行
/*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

版本

此规则是在 ESLint v4.0.0-alpha.1 中引入。

资源

ESLint 中文网
粤ICP备13048890号