The way that I like writing ternary operators is:
- Ternaries should be wrapped in parentheses.
- Each section should be on a new line.
- The operator should be at the start of the line.
- Everything should be on the same indent.
Here are two examples of ternaries following my opinionated rules.
const one = (
true
? 1
: 2
);
const two = (
true
? 1
: (
false
? 2
: 3
)
);
I'm trying to set up ESLint to work with my preference and I'm having difficulty. I'm wondering if it's possible to set it up like that. I've got as far as looking at the indent rule, but I'm not sure that I can set it the way that I want it.
I've got a demo here showing that I haven't quite figured it out, the code looks like this:
/* eslint indent: ["warn", 4, {
"flatTernaryExpressions": false,
"offsetTernaryExpressions": false
}] */
/* eslint-disable no-unused-vars, no-constant-condition */
const maybe = (
true
? 1
: (
false
? 2
: 3
)
);
Is it possible to configure ESLint to match my preferred ternary indent style?