Require Ternary Parens
Require parentheses around the condition in ternary expressions so the test is visually distinct from the branches.
Summary
The require-ternary-parens rule reports any ConditionalExpression where the test (condition) is not immediately preceded by an opening parenthesis (.
Why Use This Rule?
- Parentheses make the condition boundary explicit, preventing misreads about where the test ends and the consequent begins.
- Keeps ternary formatting consistent across the codebase.
- Improves readability when the condition is a complex expression.
Examples
Configuration
Options
warning
ignoreFiles is an escape hatch for files where this rule genuinely does not apply. It is not intended for routine use.
| Option | Type | Default | Description |
|---|---|---|---|
ignoreFiles | string[] | [] | File patterns to skip. |
Autofix
This rule provides automatic fixes. Run ESLint with the --fix flag:
npx eslint --fix . --rule '@cbnventures/nova/require-ternary-parens: error'
Troubleshooting
- Warning fires inside a JSX expression — Ternary conditions inside JSX are checked the same way. Wrap the condition in parentheses:
{(isReady === true) ? <A /> : <B />}. - Nested ternaries are each flagged independently — Each
ConditionalExpressionnode is checked on its own. Both the outer and inner ternary need parenthesized conditions.