{"record":{"id":"690797763d7cd0c2","repo":"oxc-project/oxc","slug":"prefer-ternary-expressions-over-simple-if-else-s","errorCode":null,"errorMessage":"Prefer ternary expressions over simple `if-else` statements.","messagePattern":"Prefer ternary expressions over simple `if-else` statements\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/unicorn/prefer_ternary.rs","lineNumber":22,"sourceCode":"use serde::Deserialize;\n\nuse oxc_ast::{\n    AstKind,\n    ast::{AssignmentTarget, Expression, IfStatement, MemberExpression, Statement},\n};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::{GetSpan, Span};\n\nuse crate::{\n    AstNode,\n    context::LintContext,\n    rule::{DefaultRuleConfig, Rule},\n    utils::{is_same_expression, is_same_member_expression, static_string_value},\n};\n\nfn prefer_ternary_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Prefer ternary expressions over simple `if-else` statements.\")\n        .with_help(\"Rewrite this `if`/`else` as a ternary expression.\")\n        .with_label(span)\n}\n\n#[derive(Debug, Clone, Copy, Default, Deserialize, JsonSchema, PartialEq, Eq)]\n#[serde(rename_all = \"kebab-case\")]\npub enum PreferTernaryOption {\n    /// Always enforce ternary usage when the branches can be safely merged.\n    #[default]\n    Always,\n    /// Only enforce ternary usage when the condition and both branches are single-line.\n    OnlySingleLine,\n}\n\n#[derive(Debug, Default, Clone, Deserialize)]\npub struct PreferTernary(PreferTernaryOption);\n\ndeclare_oxc_lint!(","sourceCodeStart":4,"sourceCodeEnd":40,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/unicorn/prefer_ternary.rs#L4-L40","documentation":"This is the oxlint rule `unicorn/prefer-ternary`. It fires on a simple `if`/`else` whose two branches each contain exactly one statement that assigns to the same target with `=` (via `is_same_expression`/`is_same_member_expression` checks), suggesting a conditional (ternary) expression instead. The `always` (default) option enforces it whenever merging is safe; `only-single-line` restricts it to single-line condition and branches.","triggerScenarios":"`if (cond) { a = 1; } else { a = 2; }`, or branches assigning the same member (`obj.x = ...` in both arms), where both branch bodies are single assignment expression statements to a syntactically identical target. Non-assignment branches, differing targets, or `else if` chains with mixed shapes are not flagged.","commonSituations":"Config/flag selection code (`if (dev) { level = 'debug'; } else { level = 'info'; }`); teams that find nested ternaries unreadable often set `only-single-line` or disable the rule, since the merged form can hurt readability when condition or values grow.","solutions":["Merge into a ternary: `a = cond ? 1 : 2;` (watch operator precedence — parenthesize the ternary inside larger expressions).","If the branches or condition are long, extract them: `a = cond ? longExprA() : longExprB();` or keep the if/else and set the rule to `only-single-line` in `.oxlintrc.json`.","If your style guide bans ternaries in statements, disable `unicorn/prefer-ternary` outright."],"exampleFix":"// before\nlet level;\nif (isDev) {\n  level = 'debug';\n} else {\n  level = 'info';\n}\n\n// after\nconst level = isDev ? 'debug' : 'info';","handlingStrategy":"validation","validationCode":"// Express dual assignment as a conditional from the start\nconst level = isDev ? 'debug' : 'info';\n// CI: npx oxlint --deny-warn unicorn/prefer-ternary src/","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use ternaries for single-assignment if/else; stop at one level of nesting.","Set `only-single-line` in config if multi-line ternaries hurt your reviews.","Parenthesize ternaries embedded in larger expressions to dodge precedence bugs."],"tags":["oxlint","unicorn","style","ternary","conditional"],"backgroundTag":"ternary-over-if-else","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}