Skip to content

@stylistic/

array-element-newline

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" (default) requires line breaks between array elements
  • "never" disallows line breaks between array elements
  • "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):

  • "consistent": <boolean> requires consistent usage of line breaks between array elements. If this is false, this condition is disabled.
  • "multiline": <boolean> requires line breaks if there are line breaks inside elements. If this is false, this condition is disabled.
  • "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:

json
{
    "array-element-newline": ["error", {
        "ArrayExpression": "consistent",
        "ArrayPattern": { "minItems": 3 },
    }]
}
  • "ArrayExpression" configuration for array expressions (if unspecified, this rule will not apply to array expressions)
  • "ArrayPattern" configuration for array patterns of destructuring assignments (if unspecified, this rule will not apply to array patterns)

always

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

js
/*eslint @stylistic/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

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

js
/*eslint @stylistic/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

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

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

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

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

js
/*eslint @stylistic/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

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

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

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

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

js
/*eslint @stylistic/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

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

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

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

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

js
/*eslint @stylistic/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

consistent and multiline

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

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

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

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

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

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

minItems

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

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

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

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

js
/*eslint @stylistic/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 and minItems

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

js
/*eslint @stylistic/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

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

js
/*eslint @stylistic/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 and ArrayPattern

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

js
/*eslint @stylistic/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

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

js
/*eslint @stylistic/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

When Not To Use It

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

Compatibility

Released under the MIT License.