Eslint
规则 | Rules

array-bracket-newline

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

--fix命令行上的选项可以自动修复一些被这条规则反映的问题。

许多样式指南要求或不允许数组括号内的换行符。

规则细节

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

选项

这条规则有字符串选项:

  • "always" 需要括号内的换行符

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

  • "consistent"要求每对括号一致使用换行符。如果其中一个支架在其内部有换行符,而另一个支架没有,则报告错误。

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

  • "multiline": true(默认)如果元素内或元素之间存在换行符,则需要换行。如果这是错误的,则禁用此条件。

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

always

"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( }];

"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

"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( } ];

"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"规则的错误代码示例包含以下选项:

/*eslint array-bracket-newline: ["error", "consistent"]*/ var a = [1 ]; var b = [ 1]; var c = [function foo() { dosomething( } ] var d = [ function foo() { dosomething( }]

"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

{ "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( }];

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

{ "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( } ];

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

{ "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( }];

{ "multiline": true, "minItems": 2 }规则的正确代码示例包含以下{ "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( } ];

何时不使用它

如果您不希望在打开数组括号后强制换行,请不要启用此规则。

兼容性

  • JSCS: validateNewlineAfterArrayElements相关规则

  • array-bracket-spacing

版本

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

资源