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
- Rewrite the ternary as an if/else statement: `if (cond) { x = a } else { x = b }`.
- If only complex ternaries are the concern, switch to `no-nested-ternary` instead of `no-ternary`.
- Disable the rule in .oxlintrc.json if the team does not actually want a total ban.
- 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
- Decide as a team whether the total ternary ban is really wanted before enabling it.
- Prefer if/else in codebases with this rule to avoid inline suppressions.
- Consider no-nested-ternary as the lighter-weight alternative.
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
- Expected method{method_name_str} to have this.
- {prefix} {{ after '{keyword}'{condition_if_needed}.
- Enforce default clauses in switch statements to be last
- Expected a function {style}.
- Accessor pair {getter_key} and {setter_key} should be groupe
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/e0fc86ed838b6414.
Report an issue: GitHub.