multiline-ternary

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

🔧 Fixable

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

此规则在 ESLint v8.53.0 中已弃用。请在 @stylistic/eslint-plugin-js 中使用 相应的规则

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

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

¥JavaScript allows operands of ternary expressions to be separated by newlines, which can improve the readability of your program.

例如:

¥For example:

var foo = bar > baz ? value1 : value2;

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

¥The above can be rewritten as the following to improve readability and more clearly delineate the operands:


var foo = bar > baz ?
    value1 :
    value2;

var foo = bar > baz
    ? value1
    : value2;

规则详情

¥Rule Details

此规则强制或禁止三元表达式的操作数之间的换行符。注意:此规则不强制执行运算符的位置。如果你有兴趣强制执行运算符自己的位置,请参阅 operator-linebreak 规则。

¥This rule enforces or disallows newlines between operands of a ternary expression. Note: The location of the operators is not enforced by this rule. Please see the operator-linebreak rule if you are interested in enforcing the location of the operators themselves.

选项

¥Options

此规则有一个字符串选项:

¥This rule has a string option:

  • "always"(默认)在三元表达式的操作数之间强制换行。

    ¥"always" (default) enforces newlines between the operands of a ternary expression.

  • 如果三元表达式跨越多行,"always-multiline" 会在三元表达式的操作数之间强制换行。

    ¥"always-multiline" enforces newlines between the operands of a ternary expression if the expression spans multiple lines.

  • "never" 不允许在三元表达式的操作数之间使用换行符。

    ¥"never" disallows newlines between the operands of a ternary expression.

always

这是默认选项。

¥This is the default option.

使用 "always" 选项的此规则的错误代码示例:

¥Examples of incorrect code for this rule with the "always" option:

在线运行
/*eslint multiline-ternary: ["error", "always"]*/

foo > bar ? value1 : value2;

foo > bar ? value :
    value2;

foo > bar ?
    value : value2;

使用 "always" 选项的此规则的正确代码示例:

¥Examples of correct code for this rule with the "always" option:

在线运行
/*eslint multiline-ternary: ["error", "always"]*/

foo > bar ?
    value1 :
    value2;

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

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

always-multiline

使用 "always-multiline" 选项的此规则的错误代码示例:

¥Examples of incorrect code for this rule with the "always-multiline" option:

在线运行
/*eslint multiline-ternary: ["error", "always-multiline"]*/

foo > bar ? value1 :
    value2;

foo > bar ?
    value1 : value2;

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

使用 "always-multiline" 选项的此规则的正确代码示例:

¥Examples of correct code for this rule with the "always-multiline" option:

在线运行
/*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;

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

never

使用 "never" 选项的此规则的错误代码示例:

¥Examples of incorrect code for this rule with the "never" option:

在线运行
/*eslint multiline-ternary: ["error", "never"]*/

foo > bar ? value :
    value2;

foo > bar ?
    value : value2;

foo >
    bar ?
    value1 :
    value2;

使用 "never" 选项的此规则的正确代码示例:

¥Examples of correct code for this rule with the "never" option:

在线运行
/*eslint multiline-ternary: ["error", "never"]*/

foo > bar ? value1 : value2;

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

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

何时不使用

¥When Not To Use It

如果你没有关于三元表达式的操作数是否应该用换行符分隔的严格约定,你可以安全地禁用此规则。

¥You can safely disable this rule if you do not have any strict conventions about whether the operands of a ternary expression should be separated by newlines.

兼容性

¥Compatibility

版本

此规则是在 ESLint v3.1.0 中引入。

资源

ESLint 中文网
粤ICP备13048890号