yoda
要求或禁止 "Yoda" 条件
一些该规则报告的问题可以通过 --fix 命令行选项 自动修复
尤达条件之所以如此命名,是因为条件的字面值排在第一位,而变量排在第二位。例如,以下是 Yoda 条件:
if ("red" === color) {
// ...
}
这被称为尤达条件,因为它读作 "if red equals the color",类似于星球大战角色尤达说话的方式。与排列操作数的其他方式比较:
if (color === "red") {
// ...
}
这通常读作 "if the color equals red",这可以说是描述比较的更自然的方式。
3T2Lhtv/8MdIEsfUNHvePxvod9O+LrvnrHIkBG6ajmyLEOYTvuCKpI9ERceEan0ck7de69uLNznBXbQKNkh3g8TdNEBkFJXSNK+pn+w5ZxJ47ajzQy/rLSBtK9DFEozI04lEYbvYKH/n+aCUKdwXYON7zGyjtTQ5sqMVGuwYmWt79d8J2oH5huKbZgJoTth2FryqhgiyQ87nDoMZXs6sh0PprwFmRfxI6ce/9jzwwJilPrXFlhvPcZHgJqB3dTXOZPc5nBQx74zH1iRuXFNnwb3nAtCvOvl3zzo61iTNAK7V+5CsLCl5UaP1IdkhtiK/pIw/I3uswhVyDF6pG08yfpxyQ27Bl/ZQGE6EcDt6FGtqgGWMy/WVUSQw0BaRgSsB
VM8LkknJ8eAu6y6ZR1mqmAKEH8OJjIbyY6/Ic12xsd1hAYp3Blxx5rI72xGsNUY8j6g2FGWt/34gWPY/D/w8V/pio3AzTt1W6078s/ktHHM5teDbl8BCfbeFTY6wksLCul6Zy16pSHly6cXZa5GQQONqTAl+xL04kj0lG4ezPjUSJzAuUFTBHPYOZgYSsllCoXrWGiWTBsArSJkqjW7n4O51aL601uBKw3wV3vOzo8b+gggSqZ300V1oqM5vUiVxu28xKdG/EwGyYx4+Nva5OB7Y9e/EC4sBnVLgF1dNIjSARfswCAAcZlPuFQHYE91dRIgPyrof3VaDSFnIU4Veb+83m0OmFF+3szP3eMOtvuWmqSo2IcLqlR4aY39EydcEkJTMFRbvzWMEljMs8VrLZg==
规则详情
io8JIkAW+VzgRpwNHPSdGytR77eryskol5Sz6X0LQ3Je+xY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zQ6Cj4E/Px/XaUu3q/z910bRiCD+I9fA1NRWZ3VkYXuYxh16Dhg5qTLHviwc/ofiEHM/7DtEsCl3ZYoMZJFhYxrpefKmVtR4npPragr3bqv8J4INoH9FDAoIuhIk3gFgQ15rzG2nYU9gwrk2z7sZhGWp/fSBOpj
Tn77bFJ/ZC8O0IfN7I4C68bM/WIlLnSB8nrab4ZbLyPaDcNoGISmZ8McyIVq6aGkkGm/RRWl8oLKIfi9KdzR7SQqJ0ueC1c7QApxfr58p9p9hSEbyYJWF+tJ+eXlPITeTgYfP+JDZnZy9GVPOLYYQ62bd2AqbpiVCG2rLwc7RQqgkZ9RC6+8wXGdano+ZUvX
never
7fFO6bYnKNBRgc0arcmhHE5IgxcJPQaRDTTpN3DmbLF1vNSg8kGLBrIz1DJxiaI1szmcpoCI+NPnNhmQGTYC7g==
/*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) {
// ...
}
7fFO6bYnKNBRgc0arcmhHE5IgxcJPQaRDTTpN3DmbLHsGMilHF2bLJ2yeT8wVFraHvXrfCphJlZFCmtnmPhWgw==
/*eslint yoda: "error"*/
if (5 & value) {
// ...
}
if (value === "red") {
// ...
}
if (value === `red`) {
// ...
}
if (`${value}` === `red`) {
}
exceptRange
Cmn6SXXP3UgeG/huMbE6z19M+2sM5rJvfYW7FX2A1/PVixgjlAtXqhh0Ig5a4/2ps+49mklymw4JKqAuX/EEaXWvcMxwmPN3xsx52nWWjOo=
/*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
Cmn6SXXP3UgeG/huMbE6z8r6UuWd7+7J1u9UYbYVvt14SPQBBJ7Xr9FlTvz6Y/6LxjyglUlChCEOUcFMKw2NtYr0cVY/IMAuWTSvG7vlhGQ=
/*eslint yoda: ["error", "never", { "onlyEquality": true }]*/
if (x < -1 || 9 < x) {
}
if (x !== 'foo' && 'bar' != x) {
}
if (x !== `foo` && `bar` != x) {
}
always
xKgTW08coDL3tiNVhb8F9F0Zb5Cp/aR7+tvKagL+rWIzDcpy/s18MVehIKShR03wYr3YBADOoyziMxGoTpaJmQ==
/*eslint yoda: ["error", "always"]*/
if (color == "blue") {
// ...
}
if (color == `blue`) {
// ...
}
xKgTW08coDL3tiNVhb8F9LmTDpRFZzk0wX+wiRp1Pf8QRd3RueOE9e6HGs1nextlmBA7Dwdc/YD5iNmmNfB9GQ==
/*eslint yoda: ["error", "always"]*/
if ("blue" == value) {
// ...
}
if (`blue` == value) {
// ...
}
if (`blue` == `${value}`) {
// ...
}
if (-1 < str.indexOf(substr)) {
// ...
}