oxc-project/oxc · error · OxcDiagnostic

Unexpected deeply nested ternary expression.

Error message

Unexpected deeply nested ternary expression.

What it means

Raised by unicorn/no-nested-ternary when ternaries are nested more than one level deep (a ternary inside a parenthesized nested ternary), exceeding the rule's depth allowance. The faulting input is the deeply nested conditional chain at the labeled span.

Source

Thrown at crates/oxc_linter/src/rules/unicorn/no_nested_ternary.rs:15

use oxc_ast::{AstKind, ast::Expression};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;

use crate::{AstNode, context::LintContext, rule::Rule};

fn unparenthesized_nested_ternary(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Unexpected nested ternary expression without parentheses.")
        .with_help("Add parentheses around the nested ternary expression.")
        .with_label(span)
}

fn deeply_nested_ternary(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Unexpected deeply nested ternary expression.")
        .with_help("Avoid nesting ternary expressions for more than one level.")
        .with_label(span)
}

#[derive(Debug, Default, Clone)]
pub struct NoNestedTernary;

declare_oxc_lint!(
    /// ### What it does
    ///
    /// Disallow deeply nested ternary expressions.
    ///
    /// Nested ternary expressions that are only one level deep and wrapped in parentheses are allowed by this rule.
    ///
    /// ### Why is this bad?
    ///
    /// Nesting ternary expressions can make code more difficult to understand.
    ///

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Flatten the logic into if/else statements or a switch
  2. Extract nested conditionals into well-named helper variables or functions
  3. Replace multi-way branching with a lookup table/object
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/oxc_linter/src/rules/unicorn/no_nested_ternary.rs:15 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20). Data as JSON: /api/errors/212f79e9c3968624. Report an issue: GitHub.