unicorn/prefer-switch
eslint-plugin-unicorn
Safe autofix
eslint.config.js
import unicorn from "eslint-plugin-unicorn";
export default [
{
plugins: { unicorn },
rules: {
"unicorn/prefer-switch": "error",
},
},
];if (foo === 1) {
// 1
} else if (foo === 2) {
// 2
} else if (foo === 3) {
// 3
} else {
// default
}if (foo === 1) {
// 1
} else if (foo === 2) {
// 2
}switch (foo) {
case 1: {
// 1
break;
}
case 2: {
// 2
break;
}
case 3: {
// 3
break;
}
default: {
// default
}
}Examples from the ESLint documentation.
unicorn/prefer-switch
eslint-plugin-unicorn
import unicorn from "eslint-plugin-unicorn";
export default [
{
plugins: { unicorn },
rules: {
"unicorn/prefer-switch": "error",
},
},
];Oxlint does not have this rule.
Biome does not have this rule.
Prefer String#repeat() for repeated whitespace.
Prefer direct Temporal conversion methods.
Prefer String#padStart() and String#padEnd() over manual string padding.
Prefer using Element#toggleAttribute() to toggle attributes.
Prefer String#matchAll() over RegExp#exec() loops.
Prefer Unicode code point escapes over legacy escape sequences.