enforce placing object properties on separate lines (object-property-newline)

强制将对象的属性放在不同的行上 (object-property-newline)

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

命令行中的 --fix 选项可以自动修复一些该规则报告的问题。

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" };

Rule Details

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"
};

Options

This rule has an object option:

该规则有一个对象选项:

allowMultiplePropertiesPerLine

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

选项 { "allowMultiplePropertiesPerLine": true }正确 代码示例:

/*eslint object-property-newline: ["error", { "allowMultiplePropertiesPerLine": true }]*/

var obj = { foo: "foo", bar: "bar", baz: "baz" };

var obj2 = {
    foo: "foo", bar: "bar", baz: "baz"
};

When Not To Use It

You can turn this rule off if you are not concerned with the consistency of newlines between object properties.

如果你并不关心对象属性之间换行的一致性,你可以关闭此规则。

Compatibility

Version

This rule was introduced in ESLint 2.10.0.

该规则在 ESLint 2.10.0 中被引入。

Resources