{"record":{"id":"5ba1a7a7ba014651","repo":"oxc-project/oxc","slug":"require-or-disallow-yoda-conditions","errorCode":null,"errorMessage":"Require or disallow \"Yoda\" conditions","messagePattern":"Require or disallow \"Yoda\" conditions","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/eslint/yoda.rs","lineNumber":24,"sourceCode":"    },\n};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_ecmascript::{ToBigInt, WithoutGlobalReferenceInformation};\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::{GetSpan, Span};\nuse schemars::JsonSchema;\nuse serde::Deserialize;\n\nuse crate::{\n    AstNode,\n    context::LintContext,\n    rule::{Rule, TupleRuleConfig},\n    utils::is_same_expression,\n};\n\nfn yoda_diagnostic(span: Span, never: bool, operator: &str) -> OxcDiagnostic {\n    let expected_side = if never { \"right\" } else { \"left\" };\n    OxcDiagnostic::warn(\"Require or disallow \\\"Yoda\\\" conditions\")\n        .with_help(format!(\"Expected literal to be on the {expected_side} side of {operator}.\"))\n        .with_label(span)\n}\n\n#[derive(Debug, Clone, JsonSchema, Deserialize)]\n#[serde(rename_all = \"camelCase\", default)]\npub struct Yoda(AllowYoda, YodaOptions);\n\n#[derive(Debug, Default, Clone, JsonSchema, Deserialize)]\n#[serde(rename_all = \"camelCase\", default, deny_unknown_fields)]\npub struct YodaOptions {\n    /// If the `\"exceptRange\"` property is `true`, the rule *allows* yoda conditions\n    /// in range comparisons which are wrapped directly in parentheses, including the\n    /// parentheses of an `if` or `while` condition.\n    /// A *range* comparison tests whether a variable is inside or outside the range\n    /// between two literal values.\n    except_range: bool,\n    /// If the `\"onlyEquality\"` property is `true`, the rule reports yoda","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/eslint/yoda.rs#L6-L42","documentation":"oxlint's `yoda` rule polices operand order in comparisons between a variable and a literal. With the default `\"never\"` option (per `AllowYoda::Never` default) a literal-first comparison — a Yoda condition — is reported with help text telling you which side the literal is expected on; `\"always\"` enforces the reverse. `exceptRange` and `onlyEquality` options carve out exceptions.","triggerScenarios":"Default config: `if (\"red\" === color)` or parenthesized range checks like `if (0 <= x && x < 1)` outside exceptRange. With `[\"error\", \"always\"]`: `if (color === \"red\")` is reported instead.","commonSituations":"Code ported from old defensive C/PHP style where literal-first avoided accidental assignment (`if (\"red\" = color)` is a syntax error); style drift across a mixed team; enabling the rule repo-wide and cleaning up years of code.","solutions":["Swap the operands so the literal sits on the required side (default: right)","Allow legitimate range checks with `{ \"exceptRange\": true }`","Restrict the rule to equality operators with `{ \"onlyEquality\": true }`, or switch polarity to \"always\" if the team prefers Yoda style"],"exampleFix":"// before — Yoda condition under the default \"never\" option\nif (\"red\" === color) {\n  paint();\n}\n\n// after\nif (color === \"red\") {\n  paint();\n}","handlingStrategy":"validation","validationCode":"// .oxlintrc.json\n{ \"rules\": { \"yoda\": [\"warn\", \"never\", { \"exceptRange\": true }] } }\n// CI gate: npx oxlint src/","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Standardize variable-first comparisons in code review","Enable exceptRange if the codebase uses parenthesized range checks like (0 <= x && x < 1)","Run oxlint --fix when adopting the rule across legacy code","Encode the chosen polarity in shared config so every package lints identically"],"tags":["lint","oxlint","eslint","style","operators","readability"],"backgroundTag":"yoda-conditions","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"}