unicorn/prefer-ternary
eslint-plugin-unicorn
Safe autofix
eslint.config.js
import unicorn from "eslint-plugin-unicorn";
export default [
{
plugins: { unicorn },
rules: {
"unicorn/prefer-ternary": "error",
},
},
];if (test) {
call(a);
} else {
call(b);
}function unicorn() {
if (test) {
return a;
} else {
return b;
}
}function unicorn() {
if (test) {
return a;
}
return b;
}call(test ? a : b);function unicorn() {
return test ? a : b;
}// Preserve consecutive early-return guards.
function compare(a, b) {
if (a > b) {
return 1;
}
if (a < b) {
return -1;
}
return 0;
}Examples from the ESLint documentation.
unicorn/prefer-ternary
eslint-plugin-unicorn
import unicorn from "eslint-plugin-unicorn";
export default [
{
plugins: { unicorn },
rules: {
"unicorn/prefer-ternary": "error",
},
},
];unicorn/prefer-ternary
unicorn plugin · style
{
"rules": {
"unicorn/prefer-ternary": "error"
}
}Biome does not have this rule.
Prefer using structuredClone to create a deep clone.
Enforce consistent relative URL style.
Prefer String#replaceAll() over regex searches with the global flag and String#split().join().
Enforce using the separator argument with Array#join().
Enforce combining multiple Array#{push,unshift}(), Element#classList.{add,remove}(), and importScripts() into one call.
Require non-empty module attributes for imports and exports