Skip to main content

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?

  1. Parentheses make the condition boundary explicit, preventing misreads about where the test ends and the consequent begins.
  2. Keeps ternary formatting consistent across the codebase.
  3. 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.

OptionTypeDefaultDescription
ignoreFilesstring[][]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 ConditionalExpression node is checked on its own. Both the outer and inner ternary need parenthesized conditions.