enforce line breaks after opening and before closing array brackets (array-bracket-newline)

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

A number of style guides require or disallow line breaks inside of array brackets.

Rule Details

This rule enforces line breaks after opening and before closing array brackets.

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 "always" option:

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

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

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

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

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

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

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

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

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

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

multiline

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

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

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

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

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

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

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

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

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

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

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

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

When Not To Use It

If you don't want to enforce line breaks after opening and before closing array brackets, don't enable this rule.

Compatibility

Version

This rule was introduced in ESLint 4.0.0-alpha.1.

Resources