array-element-newline

在每个数组元素后强制换行

🔧 Fixable

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

Important

This rule was deprecated in ESLint v8.53.0. Please use the corresponding rule in @stylistic/eslint-plugin-js.

Learn more

许多风格指南要求或不允许在数组元素之间换行。

¥A number of style guides require or disallow line breaks between array elements.

规则详情

¥Rule Details

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

¥This rule enforces line breaks between array elements.

选项

¥Options

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

¥This rule has either a string option:

  • "always"(默认)要求数组元素之间有换行符

    ¥"always" (default) requires line breaks between array elements

  • "never" 不允许数组元素之间换行

    ¥"never" disallows line breaks between array elements

  • "consistent" 要求数组元素之间换行符的使用保持一致

    ¥"consistent" requires consistent usage of linebreaks between array elements

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

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

  • 如果元素内部有换行符,"multiline": <boolean> 需要换行符。如果为假,则禁用此条件。

    ¥"multiline": <boolean> requires line breaks if there are line breaks inside elements. If this is false, this condition is disabled.

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

    ¥"minItems": <number> 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.

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

¥Alternatively, different configurations can be specified for array expressions and array patterns:

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

    ¥"ArrayExpression" configuration for array expressions (if unspecified, this rule will not apply to array expressions)

  • 解构赋值数组模式的 "ArrayPattern" 配置(如果未指定,则此规则不适用于数组模式)

    ¥"ArrayPattern" configuration for array patterns of destructuring assignments (if unspecified, this rule will not apply to array patterns)

always

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

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

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

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

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

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

never

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

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

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

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

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

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

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

consistent

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

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

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

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

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

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

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

multiline

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

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

在线运行
/*eslint array-element-newline: ["error", { "multiline": true }]*/

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

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

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

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

minItems

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

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

在线运行
/*eslint array-element-newline: ["error", { "minItems": 3 }]*/

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

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

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

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

multiline 和 minItems

¥multiline and minItems

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

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

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

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

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

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

ArrayExpression 和 ArrayPattern

¥ArrayExpression and ArrayPattern

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

¥Examples of incorrect code for this rule with the { "ArrayExpression": "always", "ArrayPattern": "never" } options:

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

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

¥Examples of correct code for this rule with the { "ArrayExpression": "always", "ArrayPattern": "never" } options:

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

何时不使用

¥When Not To Use It

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

¥If you don’t want to enforce linebreaks between array elements, don’t enable this rule.

兼容性

¥Compatibility

版本

此规则是在 ESLint v4.0.0-rc.0 中引入。

资源