ESLint
Docs for yoda in ESLintyoda
eslint
Safe autofix
eslint.config.js
export default [
{
rules: {
"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) {
// ...
}if (color == "blue") {
// ...
}
if (color == `blue`) {
// ...
}if (5 & value) {
// ...
}
if (value === "red") {
// ...
}
if (value === `red`) {
// ...
}
if (`${value}` === `red`) {
}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";
}if (x < -1 || 9 < x) {
}
if (x !== 'foo' && 'bar' != x) {
}
if (x !== `foo` && `bar` != x) {
}Examples from the ESLint documentation.
yoda
eslint
export default [
{
rules: {
"yoda": "error",
},
},
];yoda
eslint plugin · style
{
"rules": {
"yoda": "error"
}
}style/noYodaExpression
style · since 1.8.0
{
"linter": {
"rules": {
"style": {
"noYodaExpression": "error"
}
}
}
}Disallow new operators outside of assignments or comparisons
Require or disallow Unicode byte order mark (BOM)
Disallow unnecessary computed property keys in objects and classes
Disallow throwing literals as exceptions
Enforce variables to be declared either together or separately in functions