no-ternary

禁止三元运算符

❄️ Frozen

此规则目前已 冻结,不接受功能请求。

三元运算符用于有条件地为变量赋值。有人认为使用三元运算符会导致代码不清晰。

¥The ternary operator is used to conditionally assign a value to a variable. Some believe that the use of ternary operators leads to unclear code.

const foo = isBar ? baz : qux;

规则详情

¥Rule Details

此规则不允许三元运算符。

¥This rule disallows ternary operators.

此规则的错误代码示例:

¥Examples of incorrect code for this rule:

在线运行
/*eslint no-ternary: "error"*/

const foo = isBar ? baz : qux;

function quux() {
  return foo ? bar() : baz();
}

此规则的正确代码示例:

¥Examples of correct code for this rule:

在线运行
/*eslint no-ternary: "error"*/

let foo;

if (isBar) {
    foo = baz;
} else {
    foo = qux;
}

function quux() {
    if (foo) {
        return bar();
    } else {
        return baz();
    }
}

版本

此规则是在 ESLint v0.0.9 中引入。

资源