object-property-newline

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

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

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

规则详情

动机

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

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

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

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

saV2EisOfBo7H3z5xG5Y5g==

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

gibntQT8Msr+rVo7M/RpDho2hzTjOKC3fSoEwUEwhNcwltKM0IzRFjzmRxZVVBNs1hO4IwttewolIt8ECEbqQaRkDuRtASz86wmVZOu1WkE=

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

可选异常

jtm9TyyOF/QdbRpRfq1JvA14ZlzNJGmuGM2uvC6A2dPiClHZu5u93i8nP9Qwzsy8KRhlCX3MWePmWOamb5qF3xy/D+56LmUuKY3OEacYPgPPkRBRE7LaiFHhRJNM4C80WnOaNTeaqRBWxNofYh98SFE1yUAE97GqLRl9z9I8XqtKFsrlqUYxkr4+E/vduJ8kck0ZBrokwfn0pcXOHo9a2oHsgZIc3vsVJPWkCMwq3bXjkTWAhmkdLY90nA0nSXk79U8f3ZmiBADQhNdg6vBUznF1UZWnbzB9s2d/fD0gK7IbRh/yiWjS9y+79eiDJPYZWeyxACr2alqy2u1D9PGbdDC5jWwkpsdVEAmqHZwFM0O5JUkwfK8kQ+FiPUUBBPVchAQYtoGhSxcWOzgx1ArlsQ==

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

BRaj7Upv6dBnU76IfhtfDUmHwxoYRCYKu9SWXKFGS0+IgnweJLKg5ba7gTc5fOwhZZ/zMSje0PKrjYBfNiA67WM86U5ArJYVCvGDXg0lGqgIT+6F2HHGt7BDNnNfpnGc

记法

cti4IFn2bCpUP8FJ8Wdm6Glu2RIdz+TPhlUrHoStz5X5SjQYP6eftpDDtIlRuWTV/Gg+cLTRrHgkUIH0PFzlvYJdGwcjMMRjfQftMvmgWU4=

    5VaSxcHFqSgq1rRkGKNZ5znL5lIrO7KSpdgKESUKZiPl8WSgGEZCU+7VixNk9+pw4HVP/vbagJGSZ5L/jMsVFsVN5qUaO8mz//ZUsbEhbG9uA1YierPPA3hoL/6p0r/2nqXbfyKHfUz4RPeHCYYn/nkGuWt90X2QDIuwe6GujIWttQ5fx+NfOC3+oXYrfOQAIx0BjopXPStvwJR5nFKcfg==

tGFqJ7/2AvYGK30vAHGQFz8NAokjZ28XXIUIKK4tHMjrzO8ZYpKIppKAE68Xrt2nCpUreKY5wvMhslLaU8daHu9Wx7xVW4zMR3zzQPmtrpA=

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

uh1EeOLG6K42hMxeoC5ruWsOmJqIlxbiFfjbpfTgfdQprMDTEpy5M5OLHv+52THMs4M0jKzBeQ0oQHQI1gzeAmz0MrDat85rU5rIgO3YBDRPac9eCltt+FRK6eI0cjkPL16Wtb3Fzy06II2NhUoEIUCHp2V2WFbXZWnmfQfmRZTVixOxivvB4/jciQh8Xb2ygm7a5gIPDJnOtGggjjkOzEpawSZouTHqiWV6vIi9FDKGwdOA2qwQDDQoUxzI+dGOtfhRwp+yXFiQHBF1TeFPWtmrGp9t2GbV+Yrk1tSb9kc=

多行属性

iojsoO0EvW7ox9YwE6jDIO48kfRk9NL0BddpP0TMDosk2gNC6vr7EGBZMl61KIDelvKkgDzBe54P4rG1Q1Ubc0DU/yB168aX48mw+asJK+f0hAKnAc2m5FiL6Ux7PQ8hSN0KZUDVKnnnVyFGDy5xoZC2ad+iZEwXvpgWRfUvyqDff4uSIVUN3gW+Beu7mwTWXI9xwhq8OuxHJFISQJ7a4g==

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

jLpnD4rRLw89xA4YcVLQy9PZQAOHX5VxMzu8IRZ75p50ura/4EdQ2kxFum0gkPJ07pkhskO6bMOzcVyMy3azE+/SlGbOxfYqhzpshRvEwxR2oUo7EhjZHI/qOJaT2PFDjP+wqBPQK5emf11tVfq9xgEnqdFGOJ0sQz0QOZiztzI=

GwxLMgLaJsnFzDRY4nnJwu/L8vNYVcZB9pFPW6Tjz70Ar3Otx0yKX3q0iw6NXeATaQXPRWBE0ksfHEbHR5oYQceYf2nsFcEZUGQwRFn8fq2Yq91qVon4ju5w5pAmTWP2922B4skMX50cV2jYibhvPw==

属性间分隔符

9Y/8WDyoAK179fVATWPXxUa9z08+OBE1vZgB2MHnnYk8zMYF13CE9u0VUTSjEVM8qvK2xVDaWJWE5ZjOFaXS6ktkptGVQQOWNRzy98aBvqvj6LxqDaChvCnU5U9TpNEJINI6r+TtTJ8apaMbflKC6N8nBbQapJlL092W7YWOglw=

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

uh1EeOLG6K42hMxeoC5ruWsOmJqIlxbiFfjbpfTgfdQprMDTEpy5M5OLHv+52THMb/NB6jIMJBiM8r3lNGFEyo8NdAKPqKRVYWSI7sGaTB4lGk8N3F8jyF3NksIAtyBZs/h4DIrgqQBd7N5AWtgCEQ==

--fix

b8/1YdnGOtBG87kfQTZUQSbskUdZCQpOZ1wJ752yH86Os7mIiHDxfVPVZHEGCQ1Ig/5x5pK7pD22AF0FG4Qrs5qCosFNjsRO0U/rzaIfpS/cbsqZ+HK6UASj2wOUKsBFSznUA7/iRiGZZrBFSo61pFtAwqflt3/QjLCh/t1UWUWVE9a08gmG/5DqhYQ0hUQ4z62e/3LoxIndBVopjDx436ZVxlS7rtQ7z400aQMmbwrjkncBqOfAoDSzfLt64ovoKbyravS8QdnGi6L+HsSigubSFgB8D5b5IgKxlvJcpdELss1nV7rwB1QVtOPC0Dnu3On+N+GfmLVf1vQ4CkWBUbPC2eRclvwB2VxYa9hci/Y=

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

5OzIOhxp/ykcemJCiiw0Wg==

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

DfSrHN8Wihcw/0gDKf2bBxfCVCRi1oYDnsRuWOdxw6dELs7BUv7v9ihzGytyBzTGDeUVoDHWL56ahia8gbjGC3ziIOXPDhMi1oHAQ0HMKHGYQ3thbsuz/JXlPvlL7XP4BGlgx9UN5UopSu78PCq/+VWc2aggtPGDjTM4VINQqjnv1pg/m7ITW0xwF1VcfMQP/yXKRhx3e9H07uS+uv02kDU+HarPBSnMJJvytV7jasw947qV34v8Wmc2VKeZvwfs

krg2h0SM4S3FqshAVbVmiio4OzvkX4wNt37/51hxqXvrJEI38/78/kfKOtb8DpZlGLh2Lag019iapTYSsSDOkstA4k4aHYkUlOTdbKzRAYW1cC6C5kTZj3CrIpJ/mR9g7/4nCcfcAz6/SswLTkH8wjTw10h09nO0rM/M9grwlcZUV+yY8AOnAGKjvl+H4WAG/R8Yk4uvIIcsGtBBxI4vyA5Tep03YYpI+P5VLFaebHk=

Ed1MEMotLakXY2WVkDG7oeKr7k8pnr1VORODJytmMZm2avZAABwF8z3W/DqgJqnUmjrEsOwQmIC9LOypNLRaQFC6BnlCX6kcq7whQSY056tae1574vIK7P8KFnFNCiBkc2Vi1SwCHlW+LWa0YIYS4MQct0xHgxCJK3IKEkUajzQ/+J40vh1FcukSWg85h2kBeBvBSuSb1TbVDLaPKzzvPDqVgqdlG1m4NRIu5spkFJWq68x7ORpq+LKZBGq8/Cf2tpmCT4X3YfC6+IabILLKYw==

示例

GwS4EaSovppQ4mqJLqfbKzSYv3y9ds7cGwzaMFa/7z48InzaHuu0vd8hDlefOi2IegnNZdQVTw48mN37l8FWFws50RJ/l69C7s7ZzM/0NQBFhJmJ7lgjQ4Gjb4oJzGvjfRo3S6mKRdzr1Fo0942jo7EmGZupZOUI5wP8HUWCMf04JmxM83pqPRTH3FAZmcjg

/*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};

tvK23x9kXaq858zZ8KSDe/grTZY5yefhuA6McIGYa60jXOPvEjgwFQFlrciJCenBhjWpa2oD/Ah1dgbANP8/CVGELrsdOc77SWc6x8mDpuyVLrx+ryPucK9qehKGvPPDqyRZgzD82Jsoy1YPRJOcwDo77Bok3lA1Y61BVssQqfNwlsNS2SKtj1LNYEs3+ZdV

/*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
    ]
};

qVCMgjR5tKUNSBmRY5fDOTEeuxWljhnCzzVyeGKs/6j/RAF0jIvI8TY+S7Jx0/XZjrdQur4q4nJ7DVlxMNDfJ6xokbkbAwRHTkQZdFQc0U/HvC+jPhQeMQKfvVwf/O8vonjXyy6rkNCKTr6G6+2ADQ==

/*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]
};

何时不使用

pj7s/+HH5nEWuUuN/k7karoF7hjpW8ssriVz7GZjYznOMgTnj6f5lXcfGbI9FfNd8EsL5+4ls3VpuPTiCHk4VlVzbR6Jr9cE6EjgGlnqmlT3z1/c4nSdzH3OpiX2vWMsL80yfrsu1ttDM0XbJiv9bw==

兼容性

    UJClmMyqFd40rpszglFBI7hrr53BSA0ltHyTJl7WyNsJ9eVjcTrY210ZdgwtAoLCIXxrmlMt7ArPq3eLLlIgbwG0BrxkhHSPfR8LNzNEdKD8N0ux5o6cnPe1RyaRkNwI/5wE8lt0sGv+QNg7554ryQo6f15c1542IYpDAUC+Yn3qtdE+kIpuapl4O7v0t4MCOsLNjhHteM/E+qJdrV5vPg==