ESLint plugin to limit the depth of nested ternary expressions
npm install eslint-plugin-ternary-depth --save-dev
Add ternary-depth
to the plugins section of your .eslintrc
configuration file:
{
"plugins": ["ternary-depth"]
}
Then configure the rules you want to use:
{
"rules": {
"ternary-depth/max-depth": ["error", { "maxDepth": 2 }]
}
}
Or use the recommended configuration:
{
"extends": ["plugin:ternary-depth/recommended"]
}
This rule limits the depth of nested ternary expressions.
maxDepth
: Maximum allowed depth of nested ternary expressions (default: 2)
// Valid with maxDepth: 2
const value = condition1 ? (condition2 ? valueA : valueB) : valueC;
// Invalid with maxDepth: 2
const value = condition1
? (condition2
? (condition3 ? valueA : valueB)
: valueC)
: valueD;