yoda
要求或禁止 "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.
尤达条件的支持者强调,不可能错误地使用 = 替代 ==,因为不能对字面值进行赋值。这样做会导致语法错误,并且会在早期就被告知错误。因此,这种做法在早期编程中非常常见,当时工具尚未普及。
🌐 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.
反对尤达条件的人指出,工具使我们成为了更好的程序员,因为工具会捕捉错误使用 = 而不是 == 的情况(ESLint 会为你捕捉到这个问题)。因此,他们认为,这种模式的实用性并不超过使用尤达条件时代码可读性受到的损失。
🌐 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 条件。 - 如果是
"always",那么字面值必须始终放在最前面。
默认的 "never" 选项可以在对象字面量中具有例外选项:
🌐 The default "never" option can have exception options in an object literal:
- 如果
"exceptRange"属性是true,规则允许在范围比较中使用 Yoda 条件,这些条件可以直接用括号括起来,包括if或while条件的括号。默认值是false。范围比较测试一个变量是否在两个字面值之间的范围内或范围外。 - 如果
"onlyEquality"属性是true,该规则仅对等号运算符==和===报告 Yoda 条件。默认值是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 中引入。