yoda

要求或禁止 "Yoda" 条件

🔧 Fixable

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

尤达条件之所以如此命名,是因为条件的字面值排在第一位,而变量排在第二位。例如,以下是 Yoda 条件:

¥Yoda conditions are so named because the literal value of the condition comes first while the variable comes second. For example, the following is a Yoda condition:

if ("red" === color) {
    // ...
}

这被称为尤达条件,因为它读作 “如果红色等于颜色”,类似于星球大战角色尤达说话的方式。与排列操作数的其他方式比较:

¥This is called a Yoda condition because it reads as, “if red equals the color”, similar to the way the Star Wars character Yoda speaks. Compare to the other way of arranging the operands:

if (color === "red") {
    // ...
}

这通常读作 “如果颜色等于红色”,这可以说是描述比较的更自然的方式。

¥This typically reads, “if the color equals red”, which is arguably a more natural way to describe the comparison.

Yoda 条件的支持者强调不可能错误地使用 = 而不是 ==,因为你不能分配给字面值。这样做会导致语法错误,并且你会在早期被告知错误。因此,这种做法在工具尚不可用的早期编程中非常普遍。

¥Proponents of Yoda conditions highlight that it is impossible to mistakenly use = instead of == because you cannot assign to a literal value. Doing so will cause a syntax error and you will be informed of the mistake early on. This practice was therefore very common in early programming where tools were not yet available.

Yoda 条件的反对者指出,工具使我们成为更好的程序员,因为工具会发现错误使用 = 而不是 ==(ESLint 会为你发现这一点)。因此,他们认为,该模式的实用性并没有超过使用 Yoda 条件时代码对可读性的影响。

¥Opponents of Yoda conditions point out that tooling has made us better programmers because tools will catch the mistaken use of = instead of == (ESLint will catch this for you). Therefore, they argue, the utility of the pattern doesn’t outweigh the readability hit the code takes while using Yoda conditions.

规则详情

¥Rule Details

此规则旨在强制执行一致的条件样式,将变量与字面值进行比较。

¥This rule aims to enforce consistent style of conditions which compare a variable to a literal value.

选项

¥Options

此规则可以采用字符串选项:

¥This rule can take a string option:

  • 如果是默认的 "never",那么比较绝对不能是 Yoda 条件。

    ¥If it is the default "never", then comparisons must never be Yoda conditions.

  • 如果是 "always",那么字面量值必须始终排在第一位。

    ¥If it is "always", then the literal value must always come first.

默认的 "never" 选项可以在对象字面量中包含异常选项:

¥The default "never" option can have exception options in an object literal:

  • 如果 "exceptRange" 属性是 true,则该规则允许范围比较中的 yoda 条件直接用括号括起来,包括 ifwhile 条件的括号。默认值为 false。范围比较测试变量是否在两个字面值之间的范围之内或之外。

    ¥If the "exceptRange" property is true, the rule allows yoda conditions in range comparisons which are wrapped directly in parentheses, including the parentheses of an if or while condition. The default value is false. A range comparison tests whether a variable is inside or outside the range between two literal values.

  • 如果 "onlyEquality" 属性是 true,则规则仅报告相等运算符 ===== 的 yoda 条件。默认值为 false

    ¥If the "onlyEquality" property is true, the rule reports yoda conditions only for the equality operators == and ===. The default value is false.

onlyEquality 选项允许 exceptRange 允许的异常的超集,因此这两个选项不能一起使用。

¥The onlyEquality option allows a superset of the exceptions which exceptRange allows, thus both options are not useful together.

never

默认 "never" 选项的错误代码示例:

¥Examples of incorrect code for the default "never" option:

在线运行
/*eslint yoda: "error"*/

if ("red" === color) {
    // ...
}

if (`red` === color) {
    // ...
}

if (`red` === `${color}`) {
    // ...
}

if (true == flag) {
    // ...
}

if (5 > count) {
    // ...
}

if (-1 < str.indexOf(substr)) {
    // ...
}

if (0 <= x && x < 1) {
    // ...
}

默认 "never" 选项的正确代码示例:

¥Examples of correct code for the default "never" option:

在线运行
/*eslint yoda: "error"*/

if (5 & value) {
    // ...
}

if (value === "red") {
    // ...
}

if (value === `red`) {
    // ...
}

if (`${value}` === `red`) {

}

exceptRange

"never", { "exceptRange": true } 选项的正确代码示例:

¥Examples of correct code for the "never", { "exceptRange": true } options:

在线运行
/*eslint yoda: ["error", "never", { "exceptRange": true }]*/

function isReddish(color) {
    return (color.hue < 60 || 300 < color.hue);
}

if (x < -1 || 1 < x) {
    // ...
}

if (count < 10 && (0 <= rand && rand < 1)) {
    // ...
}

if (`blue` < x && x < `green`) {
    // ...
}

function howLong(arr) {
    return (0 <= arr.length && arr.length < 10) ? "short" : "long";
}

onlyEquality

"never", { "onlyEquality": true } 选项的正确代码示例:

¥Examples of correct code for the "never", { "onlyEquality": true } options:

在线运行
/*eslint yoda: ["error", "never", { "onlyEquality": true }]*/

if (x < -1 || 9 < x) {
}

if (x !== 'foo' && 'bar' != x) {
}

if (x !== `foo` && `bar` != x) {
}

always

"always" 选项的错误代码示例:

¥Examples of incorrect code for the "always" option:

在线运行
/*eslint yoda: ["error", "always"]*/

if (color == "blue") {
    // ...
}

if (color == `blue`) {
    // ...
}

"always" 选项的正确代码示例:

¥Examples of correct code for the "always" option:

在线运行
/*eslint yoda: ["error", "always"]*/

if ("blue" == value) {
    // ...
}

if (`blue` == value) {
    // ...
}

if (`blue` == `${value}`) {
    // ...
}

if (-1 < str.indexOf(substr)) {
    // ...
}

版本

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

进阶读物

资源

ESLint 中文网
粤ICP备13048890号