Index

multiline-ternary

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

🔧 Fixable

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

Important

This rule was deprecated in ESLint v8.53.0. It will be removed in v11.0.0. Please use the corresponding rule in @stylistic/eslint-plugin.

Learn more

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-multiline" 会在三元表达式的操作数之间强制换行,如果该表达式跨越多行。
  • "never" 不允许在三元表达式的操作数之间换行。

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 中引入。

资源