Skip to content

no-nested-ternary: Add option to allow single-level nesting without requiring parentheses #2901

@reteps

Description

@reteps

Description

Currently no-nested-ternary permits single-level nesting only when wrapped in parentheses, and deeply nested ternaries (3+ levels) are always rejected. This creates a well-documented conflict with Prettier (#2604), which removes the parentheses the auto-fixer adds — producing a circular fix loop.

eslint-config-prettier resolves this by disabling the rule entirely, but this throws away the valuable deep-nesting protection.

I'd like to propose an option (e.g. allowSingleNesting: true) that permits single-level nesting without requiring parentheses, while still rejecting deep nesting. This would let projects use Prettier and still benefit from the rule for multi-level ternaries.

Examples

With "unicorn/no-nested-ternary": ["error", { "allowSingleNesting": true }]:

// Pass — single-level nesting, no parens needed
const foo = a ? b : c ? d : e;

// Pass — single-level nesting with parens (still fine)
const foo = a ? (b > 1 ? x : y) : z;

// Fail — deep nesting (3+ levels)
const foo = a ? b : c ? d : e ? f : g;

Additional info

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions