Index

array-bracket-newline

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

🔧 Fixable

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

Important

This rule was deprecated in ESLint v8.53.0. It will be removed in v11.0.0. Please use the corresponding rule in @stylistic/eslint-plugin.

Learn more

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

🌐 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" 在括号内需要换行
  • "never" 不允许在括号内换行
  • "consistent" 要求每对括号使用一致的换行。如果一对括号中的一个括号内部有换行而另一个没有,它就会报告错误。

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

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

  • "multiline": true(默认)如果元素内部或元素之间有换行,则需要换行。如果为 false,则此条件被禁用。
  • "minItems": null(默认)要求在元素数量至少达到给定整数时进行换行。如果为 0,则此条件的作用与选项 "always" 相同。如果为 null(默认值),则此条件被禁用。

always

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

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

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

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

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

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

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

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

never

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

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

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

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

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

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

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

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

consistent

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

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

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

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

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

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

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

const a = [];
const b = [
];
const c = [1];
const d = [
    1
];
const e = [function foo() {
    dosomething();
}];
const 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 }]*/

const a = [
];
const b = [
    1
];
const c = [
    1, 2
];
const d = [1,
    2];
const 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 }]*/

const a = [];
const b = [1];
const c = [1, 2];
const d = [
    1,
    2
];
const 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 }]*/

const a = [
];
const b = [
    1
];
const c = [1, 2];
const d = [1,
    2];
const 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 }]*/

const a = [];
const b = [1];
const c = [
    1, 2
];
const d = [
    1,
    2
];
const 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 }]*/

const a = [
];
const b = [
    1
];
const c = [1, 2];
const d = [1,
    2];
const 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 }]*/

const a = [];
const b = [1];
const c = [
    1, 2
];
const d = [
    1,
    2
];
const 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 中引入。

资源