Linter rules for single or multiline object literals

Viewed 359

I try to set up ESLint rules for both linting and autoformatting of object literal.

This snippet should cause linter error:

let obj = {    foo: 1,
  bar: 2, baz: 3
};

And I expect it to be fixed to either:

let obj = { foo: 1, bar: 2, baz: 3 };

Or:

let obj = {
    foo: 1,
    bar: 2,
    baz: 3
};

I have several rules including object-property-newline but they are not enough. Here's a demo:

/*eslint indent: "error"*/
/*eslint no-multi-spaces: "error"*/
/*eslint object-curly-spacing: ["error", "always"]*/
/*eslint object-property-newline: ["error", { allowAllPropertiesOnSameLine: true }] */
let obj = {    foo: 1,
  bar: 2, baz: 3
};

The result is:

let obj = { foo: 1,
    bar: 2,
    baz: 3
};

These rules don't address inconsistent curly braces. This passes too:

let obj = { foo: 1, bar: 2, baz: 3
};

Is it possible to address this with ESLint core rules? Is this possible with third-party rules?

0 Answers
Related