While formatting preferences are very personal, a number of style guides require that object properties be placed on separate lines for better readability.
Another argument in favor of this style is that it improves the readability of diffs when a property is changed:
// More readable
var obj = {
foo: "foo",
- bar: "bar",
+ bar: "bazz",
baz: "baz"
};
// Less readable
-var obj = { foo: "foo", bar: "bar", baz: "baz" };
+var obj = { foo: "foo", bar: "bazz", baz: "baz" };
This rule aims to maintain consistency of newlines between object properties.
Examples of incorrect code for this rule:
/*eslint object-property-newline: "error"*/
var obj = { foo: "foo", bar: "bar", baz: "baz" };
var obj2 = {
foo: "foo", bar: "bar", baz: "baz"
};
var obj3 = {
foo: "foo", bar: "bar",
baz: "baz"
};
Examples of correct code for this rule:
/*eslint object-property-newline: "error"*/
var obj = {
foo: "foo",
bar: "bar",
baz: "baz"
};
This rule has an object option:
"allowMultiplePropertiesPerLine"
:true
allows all keys and values to be on the same line
Examples of additional correct code for this rule with the { "allowMultiplePropertiesPerLine": true }
option:
/*eslint object-property-newline: ["error", { "allowMultiplePropertiesPerLine": true }]*/
var obj = { foo: "foo", bar: "bar", baz: "baz" };
var obj2 = {
foo: "foo", bar: "bar", baz: "baz"
};
You can turn this rule off if you are not concerned with the consistency of newlines between object properties.