oxc-project/oxc · info · OxcDiagnostic
Variable declarations should be sorted
Error message
Variable declarations should be sorted
What it means
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.
Source
Thrown at crates/oxc_linter/src/rules/eslint/sort_vars.rs:16
use std::{borrow::Cow, cmp::Ordering};
use cow_utils::CowUtils;
use oxc_ast::{
AstKind,
ast::{BindingPattern, Expression, VariableDeclarator},
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;
use schemars::JsonSchema;
use crate::{AstNode, context::LintContext, rule::Rule};
fn sort_vars_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Variable declarations should be sorted")
.with_help("Sort variable declarations in ascending order (case-sensitive by default).")
.with_label(span)
}
#[derive(Debug, Default, Clone, JsonSchema)]
#[serde(rename_all = "camelCase", default, deny_unknown_fields)]
pub struct SortVars {
/// When `true`, the rule ignores case-sensitivity when sorting variables.
ignore_case: bool,
}
declare_oxc_lint!(
/// ### What it does
///
/// Enforce sorting of variable declarations within the same block.
///
/// ### Why is this bad?
///View on GitHub (pinned to e1e7af627c)
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.
Example fix
// before let width = 10, height = 5, depth = 2; // after let depth = 2, height = 5, width = 10;
Defensive patterns
Strategy: validation
Prevention
- 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.
When it happens
Trigger: `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.
Common situations: Destructuring-free multi-assign statements; code moved around in refactors; enabling sort-vars in a shared config over legacy files.
Related errors
- Expected '{curr_kind}' syntax before '{prev_kind}' syntax.
- Imports should be sorted alphabetically.
- Member '{name}' of the import declaration should be sorted a
- Object keys should be sorted
- Unexpected comment inline with code
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/baf763b8c8b9a688.
Report an issue: GitHub.