enforce line breaks between array elements (array-element-newline)

强制数组元素间出现换行 (array-element-newline)

The --fix option on the command line can automatically fix some of the problems reported by this rule.

命令行中的 --fix 选项可以自动修复一些该规则报告的问题。

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:

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

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

或一个对象选项(只要有任何一个属性满足条件,要求换行。否则,禁止换行):

always

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

默认选项 "always"错误 代码示例:

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

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

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

默认选项 "always"正确 代码示例:

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

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

never

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

选项 "never"错误 代码示例:

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

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

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

选项 "never"正确 代码示例:

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

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

consistent

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

选项 "consistent"错误 代码示例:

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

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

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

选项 "consistent"正确 代码示例:

/*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

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

选项 { "multiline": true }错误 代码示例:

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

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

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

选项 { "multiline": true }正确 代码示例:

/*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

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

选项 { "minItems": 3 }错误 代码示例:

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

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

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

选项 { "minItems": 3 }正确 代码示例:

/*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 and minItems

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

选项 { "multiline": true, "minItems": 3 }错误 代码示例:

/*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();
    }
];

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

选项 { "multiline": true, "minItems": 3 }正确 代码示例:

/*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();
    }
];

When Not To Use It

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

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

Compatibility

Version

This rule was introduced in ESLint 4.0.0-rc.0.

该规则在 ESLint 4.0.0-rc.0 中被引入。

Resources