{"record":{"id":"e0fc86ed838b6414","repo":"oxc-project/oxc","slug":"unexpected-use-of-ternary-expression","errorCode":null,"errorMessage":"Unexpected use of ternary expression","messagePattern":"Unexpected use of ternary expression","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"info","filePath":"crates/oxc_linter/src/rules/eslint/no_ternary.rs","lineNumber":9,"sourceCode":"use oxc_ast::AstKind;\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::Span;\n\nuse crate::{AstNode, context::LintContext, rule::Rule};\n\nfn no_ternary_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Unexpected use of ternary expression\")\n        .with_help(\"Do not use the ternary expression.\")\n        .with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct NoTernary;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// Disallow ternary operators.\n    ///\n    /// ### Why is this bad?\n    ///\n    /// The ternary operator is used to conditionally assign a value to a\n    /// variable. Some believe that the use of ternary operators leads to\n    /// unclear code.\n    ///","sourceCodeStart":1,"sourceCodeEnd":27,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/eslint/no_ternary.rs#L1-L27","documentation":"`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.","triggerScenarios":"Any `cond ? a : b` expression anywhere in the file — assignments, returns, arguments, JSX — once the rule is enabled in .oxlintrc.json.","commonSituations":"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.","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`."],"exampleFix":"// before\nconst label = count === 0 ? 'empty' : 'items';\n\n// after\nlet label;\nif (count === 0) {\n  label = 'empty';\n} else {\n  label = 'items';\n}","handlingStrategy":"validation","validationCode":"# CI gate once no-ternary is in .oxlintrc.json rules\nnpx oxlint src/ --deny-warnings","typeGuard":null,"tryCatchPattern":null,"preventionTips":["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."],"tags":["eslint","oxlint","no-ternary","style","conditional-expression"],"backgroundTag":"ternary-operator-misuse","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}