oxc-project/oxc · info · OxcDiagnostic

Unexpected use of ternary expression

Error message

Unexpected use of ternary expression

What it means

`no-ternary` is a stylistic ESLint rule ported to oxlint that flags every conditional (ternary) expression, reported from `no_ternary_diagnostic` with span covering the whole `ConditionalExpression`. The rationale in the rule docs is code-style preference: teams that find nested ternaries hard to read ban all of them uniformly.

Source

Thrown at crates/oxc_linter/src/rules/eslint/no_ternary.rs:9

use oxc_ast::AstKind;
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;

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

fn no_ternary_diagnostic(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Unexpected use of ternary expression")
        .with_help("Do not use the ternary expression.")
        .with_label(span)
}

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

declare_oxc_lint!(
    /// ### What it does
    ///
    /// Disallow ternary operators.
    ///
    /// ### Why is this bad?
    ///
    /// The ternary operator is used to conditionally assign a value to a
    /// variable. Some believe that the use of ternary operators leads to
    /// unclear code.
    ///

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Rewrite the ternary as an if/else statement: `if (cond) { x = a } else { x = b }`.
  2. If only complex ternaries are the concern, switch to `no-nested-ternary` instead of `no-ternary`.
  3. Disable the rule in .oxlintrc.json if the team does not actually want a total ban.
  4. Suppress a single justified use with `// oxlint-disable-next-line eslint/no-ternary`.

Example fix

// before
const label = count === 0 ? 'empty' : 'items';

// after
let label;
if (count === 0) {
  label = 'empty';
} else {
  label = 'items';
}
Defensive patterns

Strategy: validation

Validate before calling

# CI gate once no-ternary is in .oxlintrc.json rules
npx oxlint src/ --deny-warnings

Prevention

When it happens

Trigger: Any `cond ? a : b` expression anywhere in the file — assignments, returns, arguments, JSX — once the rule is enabled in .oxlintrc.json.

Common situations: Adopting a legacy style guide (e.g. older Airbnb-derived configs) that bans ternaries; enabling the rule repo-wide and getting flooded with hits; new team members writing `x ? y : z` in codebases where the rule is on.

Related errors


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