object-property-newline

强制将对象属性放在单独的行上

🔧 Fixable

此规则报告的一些问题可通过 --fix 命令行 选项自动修复

Important

This rule was deprecated in ESLint v8.53.0. Please use the corresponding rule in @stylistic/eslint-plugin-js.

Learn more

此规则允许你限制对象字面中属性规范的位置。你可以禁止任何属性规范的任何部分与任何其他属性规范的任何部分出现在同一行。你可以将此禁止设为绝对,或者,通过调用对象选项,你可以允许异常,允许对象字面量的所有属性规范的所有部分都在一行中。

¥This rule permits you to restrict the locations of property specifications in object literals. You may prohibit any part of any property specification from appearing on the same line as any part of any other property specification. You may make this prohibition absolute, or, by invoking an object option, you may allow an exception, permitting an object literal to have all parts of all of its property specifications on a single line.

规则详情

¥Rule Details

动机

¥Motivations

此规则可以确保按照某些风格指南的要求,属性规范出现在单独的行中以提高可读性。例如,你可以禁止所有这些:

¥This rule makes it possible to ensure, as some style guides require, that property specifications appear on separate lines for better readability. For example, you can prohibit all of these:

const newObject = {a: 1, b: [2, {a: 3, b: 4}]};
const newObject = {
    a: 1, b: [2, {a: 3, b: 4}]
};
const newObject = {
    a: 1,
    b: [2, {a: 3, b: 4}]
};
const newObject = {
    a: 1,
    b: [
        2,
        {a: 3, b: 4}
    ]
};

而不是那些,你可以通过编写来遵守规则

¥Instead of those, you can comply with the rule by writing

const newObject = {
    a: 1,
    b: [2, {
        a: 3,
        b: 4
    }]
};

或者

¥or

const newObject = {
    a: 1,
    b: [
        2,
        {
            a: 3,
            b: 4
        }
    ]
};

此规则的另一个好处是更改属性时差异的特异性:

¥Another benefit of this rule is specificity of diffs when a property is changed:

// More specific
 var obj = {
     foo: "foo",
-    bar: "bar",
+    bar: "bazz",
     baz: "baz"
 };
// Less specific
-var obj = { foo: "foo", bar: "bar", baz: "baz" };
+var obj = { foo: "foo", bar: "bazz", baz: "baz" };

可选异常

¥Optional Exception

该规则提供了一个对象选项 allowAllPropertiesOnSameLine(不推荐使用的同义词是 allowMultiplePropertiesPerLine)。如果将其设置为 true,则允许使用上述前两个对象字面量,所有属性规范位于同一行,但类似

¥The rule offers one object option, allowAllPropertiesOnSameLine (a deprecated synonym is allowMultiplePropertiesPerLine). If you set it to true, object literals such as the first two above, with all property specifications on the same line, will be permitted, but one like

const newObject = {
    a: 'a.m.', b: 'p.m.',
    c: 'daylight saving time'
};

将被禁止,因为两个属性(但不是所有属性)出现在同一行。

¥will be prohibited, because two properties, but not all properties, appear on the same line.

记法

¥Notations

该规则同样适用于所有属性规范,无论符号如何,包括:

¥This rule applies equally to all property specifications, regardless of notation, including:

  • a: 1(ES5)

  • a(ES2015 简写属性)

    ¥a (ES2015 shorthand property)

  • [prop${a}](ES2015 计算属性名称)

    ¥[`prop${a}`] (ES2015 computed property name)

因此,该规则(没有可选的例外)禁止这两种情况:

¥Thus, the rule (without the optional exception) prohibits both of these:

const newObject = {
    a: 1, [
        process.argv[4]
    ]: '01'
};
const newObject = {
    a: 1, [process.argv[4]]: '01'
};

(此行为与下面引用的 JSCS 规则不同,后者不会将计算属性名称的前导 [ 视为该属性规范的一部分。JSCS 规则禁止第二种格式,但允许第一种格式。)

¥(This behavior differs from that of the JSCS rule cited below, which does not treat the leading [ of a computed property name as part of that property specification. The JSCS rule prohibits the second of these formats but permits the first.)

多行属性

¥Multiline Properties

该规则禁止将一个属性规范的至少 1 个字符与任何其他属性规范的至少 1 个字符放在任何行上。例如,规则禁止

¥The rule prohibits the colocation on any line of at least 1 character of one property specification with at least 1 character of any other property specification. For example, the rule prohibits

const newObject = {a: [
    'Officiële website van de Europese Unie',
    'Официален уебсайт на Европейския съюз'
], b: 2};

因为 a 规范的 1 个字符(即其值的尾随 ])与 b 规范在同一行。

¥because 1 character of the specification of a (i.e. the trailing ] of its value) is on the same line as the specification of b.

可选的例外不能原谅这种情况,因为整个属性规范集合跨越 4 行,而不是 1 行。

¥The optional exception does not excuse this case, because the entire collection of property specifications spans 4 lines, not 1.

属性间分隔符

¥Inter-property Delimiters

分隔属性规范的逗号和任何空格不被视为它们的一部分。因此,该规则允许这两种格式:

¥The comma and any whitespace that delimit property specifications are not considered parts of them. Therefore, the rule permits both of these formats:

const newFunction = multiplier => ({
    a: 2 * multiplier,
    b: 4 * multiplier,
    c: 8 * multiplier
});
const newFunction = multiplier => ({
    a: 2 * multiplier
    , b: 4 * multiplier
    , c: 8 * multiplier
});

(此行为不同于下面引用的 JSCS 规则,后者允许第一种格式但禁止第二种格式。)

¥(This behavior differs from that of the JSCS rule cited below, which permits the first but prohibits the second format.)

–fix

如果使用命令行 --fix 选项调用此规则,则通常会修改违反规则的对象字面以符合它。每种情况下的修改是,只要在同一行上有部分或全部先前的属性规范,就将属性规范移动到下一行。例如,

¥If this rule is invoked with the command-line --fix option, object literals that violate the rule are generally modified to comply with it. The modification in each case is to move a property specification to the next line whenever there is part or all of a previous property specification on the same line. For example,

const newObject = {
    a: 'a.m.', b: 'p.m.',
    c: 'daylight saving time'
};

被转换为

¥is converted to

const newObject = {
    a: 'a.m.',
b: 'p.m.',
    c: 'daylight saving time'
};

修改不依赖于对象选项是否设置为 true。换句话说,ESLint 永远不会将所有属性规范收集到一行中,即使对象选项允许这样做。

¥The modification does not depend on whether the object option is set to true. In other words, ESLint never collects all the property specifications onto a single line, even when the object option would permit that.

如果注释紧接在一行的第二个或后续属性规范之前,ESLint 不会更正违反此规则的行为,因为 ESLint 无法确定将注释放在哪一行。

¥ESLint does not correct a violation of this rule if a comment immediately precedes the second or subsequent property specification on a line, since ESLint cannot determine which line to put the comment onto.

如上所示,应用于此规则的 --fix 选项不符合其他规则,例如 indent,但是,如果这些其他规则也有效,则该选项也会应用它们。

¥As illustrated above, the --fix option, applied to this rule, does not comply with other rules, such as indent, but, if those other rules are also in effect, the option applies them, too.

示例

¥Examples

此规则的错误代码示例(没有对象选项或 allowAllPropertiesOnSameLine 设置为 false):

¥Examples of incorrect code for this rule, with no object option or with allowAllPropertiesOnSameLine set to false:

在线运行
/*eslint object-property-newline: "error"*/

const obj0 = { foo: "foo", bar: "bar", baz: "baz" };

const obj1 = {
    foo: "foo", bar: "bar", baz: "baz"
};

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

const obj3 = {
    [process.argv[3] ? "foo" : "bar"]: 0, baz: [
        1,
        2,
        4,
        8
    ]
};

const a = "antidisestablishmentarianistically";
const b = "yugoslavyalılaştırabildiklerimizdenmişsiniz";
const obj4 = {a, b};

const domain = process.argv[4];
const obj5 = {
    foo: "foo", [
    domain.includes(":") ? "complexdomain" : "simpledomain"
]: true};

此规则的正确代码示例(没有对象选项或 allowAllPropertiesOnSameLine 设置为 false):

¥Examples of correct code for this rule, with no object option or with allowAllPropertiesOnSameLine set to false:

在线运行
/*eslint object-property-newline: "error"*/

const obj1 = {
    foo: "foo",
    bar: "bar",
    baz: "baz"
};

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

const user = process.argv[2];
const obj3 = {
    user,
    [process.argv[3] ? "foo" : "bar"]: 0,
    baz: [
        1,
        2,
        4,
        8
    ]
};

使用 { "allowAllPropertiesOnSameLine": true } 选项的此规则的附加正确代码示例:

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

在线运行
/*eslint object-property-newline: ["error", { "allowAllPropertiesOnSameLine": true }]*/

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

const obj2 = {
    foo: "foo", bar: "bar", baz: "baz"
};
const user = process.argv[2];
const obj3 = {
    user, [process.argv[3] ? "foo" : "bar"]: 0, baz: [1, 2, 4, 8]
};

何时不使用

¥When Not To Use It

如果你想逐个决定是否将属性规范放在单独的行上,你可以关闭此规则。

¥You can turn this rule off if you want to decide, case-by-case, whether to place property specifications on separate lines.

兼容性

¥Compatibility

版本

此规则是在 ESLint v2.10.0 中引入。

资源