{"record":{"id":"f877d3b3ab134a5e","repo":"oxc-project/oxc","slug":"imports-should-be-sorted-alphabetically","errorCode":null,"errorMessage":"Imports should be sorted alphabetically.","messagePattern":"Imports should be sorted alphabetically\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"info","filePath":"crates/oxc_linter/src/rules/eslint/sort_imports.rs","lineNumber":32,"sourceCode":"use schemars::JsonSchema;\nuse serde::{Deserialize, Serialize};\n\nuse crate::{\n    context::LintContext,\n    rule::{DefaultRuleConfig, Rule},\n};\n\nfn unexpected_syntax_order_diagnostic(\n    curr_kind: &ImportKind,\n    prev_kind: &ImportKind,\n    span: Span,\n) -> OxcDiagnostic {\n    OxcDiagnostic::warn(format!(\"Expected '{curr_kind}' syntax before '{prev_kind}' syntax.\"))\n        .with_label(span)\n}\n\nfn sort_imports_alphabetically_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Imports should be sorted alphabetically.\").with_label(span)\n}\n\nfn sort_members_alphabetically_diagnostic(name: &str, span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(format!(\n        \"Member '{name}' of the import declaration should be sorted alphabetically.\"\n    ))\n    .with_label(span)\n}\n\n#[derive(Debug, Default, Clone, Deserialize)]\npub struct SortImports(Box<SortImportsOptions>);\n\n#[derive(Debug, Default, Clone, JsonSchema, Deserialize)]\n#[serde(rename_all = \"camelCase\", default, deny_unknown_fields)]\npub struct SortImportsOptions {\n    /// When `true`, the rule ignores case-sensitivity when sorting import names.\n    ignore_case: bool,\n    /// When `true`, the rule ignores the sorting of import declarations (the order of `import` statements).","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/eslint/sort_imports.rs#L14-L50","documentation":"oxlint `eslint/sort-imports`: consecutive import declarations of the same syntax kind are out of alphabetical order by first member/alias name (then source). `sort_imports_alphabetically_diagnostic` (sort_imports.rs:32) emits 'Imports should be sorted alphabetically.' This is the within-kind ordering check of the rule described in its doc block: imports are sorted first by member syntax, then alphabetically.","triggerScenarios":"`import b from 'b.js'; import a from 'a.js';` — two adjacent declarations of the same ImportKind whose compared names invert the sort. Comparison uses the first member or alias name; `ignoreCase` and `allowSeparatedGroups` options alter it.","commonSituations":"Merge conflicts resolved by keeping both imports in the wrong order; new imports prepended rather than inserted in order; enabling the rule on legacy files.","solutions":["Sort the adjacent declarations alphabetically.","Separate logically distinct groups with a blank line and set `allowSeparatedGroups: true` so each group sorts independently.","Set `ignoreCase: true` if case differences ('API' vs 'api') cause churn.","Set `ignoreDeclarationSort: true` to disable declaration-order checking."],"exampleFix":"// before\nimport { zebra } from 'animals.js';\nimport { apple } from 'fruits.js';\nimport { banana } from 'fruits.js';\n\n// after\nimport { apple } from 'fruits.js';\nimport { banana } from 'fruits.js';\nimport { zebra } from 'animals.js';","handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["Insert new imports in sorted position rather than appending at the end of the block.","After merge conflicts in import blocks, run the linter before committing.","Set `allowSeparatedGroups: true` and group by module type — groups then sort independently and churn drops."],"tags":["eslint","oxlint","imports","sorting","style","lint"],"backgroundTag":"import-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"}