{"record":{"id":"baf763b8c8b9a688","repo":"oxc-project/oxc","slug":"variable-declarations-should-be-sorted","errorCode":null,"errorMessage":"Variable declarations should be sorted","messagePattern":"Variable declarations should be sorted","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"info","filePath":"crates/oxc_linter/src/rules/eslint/sort_vars.rs","lineNumber":16,"sourceCode":"use std::{borrow::Cow, cmp::Ordering};\n\nuse cow_utils::CowUtils;\nuse oxc_ast::{\n    AstKind,\n    ast::{BindingPattern, Expression, VariableDeclarator},\n};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::Span;\nuse schemars::JsonSchema;\n\nuse crate::{AstNode, context::LintContext, rule::Rule};\n\nfn sort_vars_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Variable declarations should be sorted\")\n        .with_help(\"Sort variable declarations in ascending order (case-sensitive by default).\")\n        .with_label(span)\n}\n\n#[derive(Debug, Default, Clone, JsonSchema)]\n#[serde(rename_all = \"camelCase\", default, deny_unknown_fields)]\npub struct SortVars {\n    /// When `true`, the rule ignores case-sensitivity when sorting variables.\n    ignore_case: bool,\n}\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// Enforce sorting of variable declarations within the same block.\n    ///\n    /// ### Why is this bad?\n    ///","sourceCodeStart":1,"sourceCodeEnd":34,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/eslint/sort_vars.rs#L1-L34","documentation":"oxlint `eslint/sort-vars`: multiple declarators in one `var`/`let`/`const` statement are not in ascending name order, so `sort_vars_diagnostic` (sort_vars.rs:17) reports 'Variable declarations should be sorted' with help noting case-sensitive ascending order by default. The rule's `ignoreCase` option (visible in the struct at sort_vars.rs:16-21) relaxes the comparison.","triggerScenarios":"`var c = 1, a = 2, b = 3;` — a VariableDeclaration whose declarator names are out of order. Only consecutive declarators are compared (mixed initialized/uninitialized sequences behave like ESLint's sort-vars), and the check is purely on the identifier names.","commonSituations":"Destructuring-free multi-assign statements; code moved around in refactors; enabling sort-vars in a shared config over legacy files.","solutions":["Reorder the declarators: `var b = 2, a = 1;` becomes `var a = 1, b = 2;`.","Set `\"ignoreCase\": true` if mixed-case names cause noise.","Split into separate statements if order carries meaning (e.g. dependency between initializers), or disable the rule for that line."],"exampleFix":"// before\nlet width = 10, height = 5, depth = 2;\n\n// after\nlet depth = 2, height = 5, width = 10;","handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep multi-declarator statements short and sorted, or split into one declarator per statement.","If initializers depend on each other, splitting statements removes them from the rule's comparison entirely.","Set `ignoreCase: true` when the file mixes casing conventions."],"tags":["eslint","oxlint","sorting","variables","style","lint"],"backgroundTag":"variable-sorting","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"}