跳至内容

@stylistic/js/

array-element-newline

许多风格指南要求或禁止数组元素之间的换行符。

规则详情

此规则强制在数组元素之间换行。

选项

此规则具有字符串选项

  • "always" (默认) 要求在数组元素之间换行
  • "never" 禁止在数组元素之间换行
  • "consistent" 要求在数组元素之间一致使用换行符

或对象选项(如果满足任何属性,则需要换行符。否则,禁止换行符)

  • "multiline": <boolean> 如果元素内部有换行符,则需要换行符。如果为 false,则禁用此条件。
  • "minItems": <number> 如果元素数量至少为给定的整数,则需要换行符。如果为 0,则此条件将与选项 "always" 相同。如果为 null(默认值),则禁用此条件。

或者,可以为数组表达式和数组模式指定不同的配置

json
{
    "array-element-newline": ["error", {
        "ArrayExpression": "consistent",
        "ArrayPattern": { "minItems": 3 },
    }]
}
  • "ArrayExpression" 数组表达式的配置(如果未指定,此规则将不适用于数组表达式)
  • "ArrayPattern" 解构赋值的数组模式的配置(如果未指定,此规则将不适用于数组模式)

always

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

js
/*eslint @stylistic/js/array-element-newline: ["error", "always"]*/

var c = [1,
2];
var d = [1,
2,
3];
var e = [1,
2,
3
]; var f = [ 1,
2,
3
]; var g = [ function foo() { dosomething(); },
function bar() {
dosomething(); } ];
incorrect

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

js
/*eslint @stylistic/js/array-element-newline: ["error", "always"]*/

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

never

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

js
/*eslint @stylistic/js/array-element-newline: ["error", "never"]*/

var c = [
    1,
2 ]; var d = [ 1,
2,
3 ]; var e = [ function foo() { dosomething(); },
function bar() { dosomething(); } ];
incorrect

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

js
/*eslint @stylistic/js/array-element-newline: ["error", "never"]*/

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

consistent

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

js
/*eslint @stylistic/js/array-element-newline: ["error", "consistent"]*/

var a = [
    1,
2,
3 ]; var b = [ function foo() { dosomething(); },
function bar() {
dosomething(); }, function baz() { dosomething(); } ];
incorrect

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

js
/*eslint @stylistic/js/array-element-newline: ["error", "consistent"]*/

var a = [];
var b = [1];
var c = [1, 2];
var d = [1, 2, 3];
var e = [
    1,
    2
];
var f = [
    1,
    2,
    3
];
var g = [
    function foo() {
        dosomething();
    }, function bar() {
        dosomething();
    }, function baz() {
        dosomething();
    }
];
var h = [
    function foo() {
        dosomething();
    },
    function bar() {
        dosomething();
    },
    function baz() {
        dosomething();
    }
];
correct

多行

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

js
/*eslint @stylistic/js/array-element-newline: ["error", { "multiline": true }]*/

var d = [1,
2, 3]; var e = [ function foo() { dosomething(); },
function bar() {
dosomething(); } ];
incorrect

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

js
/*eslint @stylistic/js/array-element-newline: ["error", { "multiline": true }]*/

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

最小项数

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

js
/*eslint @stylistic/js/array-element-newline: ["error", { "minItems": 3 }]*/

var c = [1,
2]; var d = [1,
2,
3];
var e = [ function foo() { dosomething(); },
function bar() { dosomething(); } ];
incorrect

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

js
/*eslint @stylistic/js/array-element-newline: ["error", { "minItems": 3 }]*/

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

多行和最小项数

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

js
/*eslint @stylistic/js/array-element-newline: ["error", { "multiline": true, "minItems": 3 }]*/

var c = [1,
2]; var d = [1,
2,
3];
var e = [ function foo() { dosomething(); },
function bar() {
dosomething(); } ];
incorrect

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

js
/*eslint @stylistic/js/array-element-newline: ["error", { "multiline": true, "minItems": 3 }]*/

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

数组表达式和数组模式

使用 { "ArrayExpression": "always", "ArrayPattern": "never" } 选项时,此规则的错误代码示例

js
/*eslint @stylistic/js/array-element-newline: ["error", { "ArrayExpression": "always", "ArrayPattern": "never" }]*/

var a = [1,
2];
var b = [1,
2,
3];
var c = [ function foo() { dosomething(); },
function bar() {
dosomething(); } ]; var [d,
e] = arr; var [f,
g,
h] = arr; var [i = function foo() { dosomething() },
j = function bar() { dosomething() }] = arr
incorrect

使用 { "ArrayExpression": "always", "ArrayPattern": "never" } 选项时,此规则的正确代码示例

js
/*eslint @stylistic/js/array-element-newline: ["error", { "ArrayExpression": "always", "ArrayPattern": "never" }]*/

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

var [d, e] = arr
var [f, g, h] = arr
var [i = function foo() {
    dosomething()
}, j = function bar() {
    dosomething()
}] = arr
correct

何时不使用它

如果您不想在数组元素之间强制换行,请不要启用此规则。

兼容性