array-bracket-newline - ESLint - Pluggable JavaScript Linter (original) (raw)

Enforce linebreaks after opening and before closing array brackets

🔧 Fixable

Some problems reported by this rule are automatically fixable by the --fix command line option

Table of Contents

  1. Rule Details
  2. Options
    1. always
    2. never
    3. consistent
    4. multiline
    5. minItems
    6. multiline and minItems
  3. When Not To Use It
  4. Compatibility
  5. Related Rules
  6. Version
  7. Resources

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:

Open in Playground

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

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

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

Open in Playground

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

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

never

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

Open in Playground

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

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

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

Open in Playground

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

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

consistent

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

Open in Playground

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

const a = [1
];
const b = [
    1];
const c = [function foo() {
    dosomething();
}
]
const d = [
    function foo() {
        dosomething();
    }]

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

Open in Playground

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

const a = [];
const b = [
];
const c = [1];
const d = [
    1
];
const e = [function foo() {
    dosomething();
}];
const f = [
    function foo() {
        dosomething();
    }
];

multiline

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

Open in Playground

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

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

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

Open in Playground

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

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

minItems

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

Open in Playground

/*eslint array-bracket-newline: ["error", { "minItems": 2 }]*/

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

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

Open in Playground

/*eslint array-bracket-newline: ["error", { "minItems": 2 }]*/

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

multiline and minItems

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

Open in Playground

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

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

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

Open in Playground

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

const a = [];
const b = [1];
const c = [
    1, 2
];
const d = [
    1,
    2
];
const 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 v4.0.0-alpha.1.

Resources