multiline-ternary

在三元表达式的操作数之间强制换行

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

JavaScript 允许三元表达式的操作数用换行符分隔,这样可以提高程序的可读性。

例如:

var foo = bar > baz ? value1 : value2;

上面可以改写如下,以提高可读性并更清楚地描述操作数:

var foo = bar > baz ?
    value1 :
    value2;

规则详情

eqpNV55VgLCQ1Yv8CsVuXmqL7hCUZFYgW2Xb4+pEtBNHm0Xb7PNza366znU1MLz9AYScoEVX3Rbk80DKxl3zFG3KlNRfXWyS+IfFFpGPhBAuxcbnQgZ2KoSHx10nBhYdc5SUQEjQ89dZLrOl04gyIgu3P9vE/SDydhC+Q8gs9rnkNiC50uq/bgsulR4+JHPywno+G3VY3kCBcJtYCe9PZt+uhWhph0Oi5JEkSyiUSCYGWsheX5M3/T/vwFLMiWXOA9WY3TgV4rPXVbyW8cPStOyIPSIlpRceU3djSEmVstSVuAjEzErB52uw9E/zRPoUAyLrVs03cZ/iXGgl54fPzsTbTYJSbkfUI+Z6UO4zFF0=

选项

gOiAZnAcPh/ECcp3u5icNMdXUzIbNOaiXIO+Btuf3/uLdKlAq+lBQREzJgxgBi5T

    vqZv+QBNs/gSFuv/ivaAMR8QY8Rzs5F+3vWKzfUHmOlNrNsYaDEb8JqsWndtGa2Kq1egUCEyD1KrKPPjQg108YR741uv/hBeuqvdTc4bYm/GKY0IIPAJ1OuNsOH79FAKieIFxTbTx3DJM3Yc0W30HST1lknsLekKpF/aKg32Z1P/ziVKjoAN0trIKMY3HppWdJ+K9HmDS6opOdRuPvkR3x4lgIsOTpD5h1Atgy2rQT70xw/FNsYrHz28rTxi1RBT7DmzE8glRsp1m54iTo/9m/Nyb3Ud/3L0OeUYw6V22YmmQ3AfvufqvZVlSSBXdz0mUJRI/yyo8pLH/DyX8cJQHyWwCP7Burr7qUExCM1uHfpd6AwlUfXhc9ZkRJk048rZNagASxzTiDeCmuY/5iRfaA==

always

eg+vY4OQEvpI7HXyEHmO73AVJDVai9rjmkP0AdTmeNk=

yqV+SV60QvsOsYXkYvZsZSN35u2BEfk7UzqdB7xGwBXAxMLq9tHsIQE7501GmDQVOKLwOEc6SBB1Q7CuPjXRTvRf6BwRkCRaFSPkKC/ITPA=

/*eslint multiline-ternary: ["error", "always"]*/

foo > bar ? value1 : value2;

foo > bar ? value :
    value2;

foo > bar ?
    value : value2;

yqV+SV60QvsOsYXkYvZsZSN35u2BEfk7UzqdB7xGwBVZSC+x4VbKrC6dmOngM9JtEZ9HS3DKi+EIk962kYJOsv7sY7JBZk+4gKi/h2XVZbw=

/*eslint multiline-ternary: ["error", "always"]*/

foo > bar ?
    value1 :
    value2;

foo > bar ?
    (baz > qux ?
        value1 :
        value2) :
    value3;

always-multiline

yqV+SV60QvsOsYXkYvZsZbWDQHmfLiDfGZH9G7Jj5CbLv8Mxaej87IdtokyGzc0zx3kf/JiMhn2yxUddGizREF6ujxlrR1L2dCrIMOPzYhU=

/*eslint multiline-ternary: ["error", "always-multiline"]*/

foo > bar ? value1 :
    value2;

foo > bar ?
    value1 : value2;

foo > bar &&
    bar > baz ? value1 : value2;

yqV+SV60QvsOsYXkYvZsZbWDQHmfLiDfGZH9G7Jj5CbLv8Mxaej87IdtokyGzc0zdLTvadgUpYb5yA1/CeHvJVJQVTxL3YbgZzFnPlOSJyA=

/*eslint multiline-ternary: ["error", "always-multiline"]*/

foo > bar ? value1 : value2;

foo > bar ?
    value1 :
    value2;

foo > bar ?
    (baz > qux ? value1 : value2) :
    value3;

foo > bar ?
    (baz > qux ?
        value1 :
        value2) :
    value3;

foo > bar &&
    bar > baz ?
        value1 :
        value2;

never

yqV+SV60QvsOsYXkYvZsZS7WQXw6pPZQT+2Jc45fpPGJ9XMYDapADCnYMkJcWohUyjYf5OWYbXTB1PtlCuwdgsZ9sLGXfE1+NcyXgciDiuA=

/*eslint multiline-ternary: ["error", "never"]*/

foo > bar ? value :
    value2;

foo > bar ?
    value : value2;

foo >
    bar ?
    value1 :
    value2;

yqV+SV60QvsOsYXkYvZsZS7WQXw6pPZQT+2Jc45fpPESLfOk8X2U4OSBulxe5pgXr9y18oNEJdw5rpbnfWY29skTj6fGWCDUPHL555agv9I=

/*eslint multiline-ternary: ["error", "never"]*/

foo > bar ? value1 : value2;

foo > bar ? (baz > qux ? value1 : value2) : value3;

foo > bar ? (
    baz > qux ? value1 : value2
) : value3;

何时不使用

DDjVa0xUF00GY85rOE0b86eTbxvHiNkir1xOaHel8/8WAaCOgvggVDVgD4sEkk5SVHwizv68wPIFaKeEe4g5fSlUsESSWR3LPOP7y4jTjHeZAQDhP8+cDeLyBQBRgChtXNIdiKEEUwi5tDtGkwHAMlznVVhvQrYU85F0umnp1y/NIuaJXA7YSJ4ZUmTzbEf9

兼容性

    A2arLA16BaFzoOfXvkJXZFrw4NcT3SGVL3d8aXz3etF7jjS6e2aHyrSUfkkG62/TNavrvPC/Ba7cRcX+Qa546LBwitj8SbSdc3iy6cO41bhgKkYksDV2XEhVRRgPJvIkniBRPpzFutfQv6X3o8LCQg==