absolute/sort-keys-fixable
Enforce sorted keys in object literals with a comment-preserving auto-fix for provably safe cases.
#Rule Details
Mirrors ESLint’s built-in sort-keys, reporting out-of-order keys in objects with at least minKeys properties, but adds a comment-preserving autofix — the core rule has none. It also covers object literals inside JSX prop expression containers and JSX attribute lists.
The autofix only runs when reordering is provably safe. It backs off (report-only) when a property is a spread, computed, or non-identifier key; when duplicate names exist (accessor pairs aside); or when two or more property values are side-effecting. A single impure value still autofixes, since it executes once regardless of position.
Purity analysis is conservative but smart — literals, stable reads, pure global/member calls, and provably-pure local helpers are safe. For imported helpers you know are pure (such as TypeBox’s t.Object), list them in pureImports so the fixer can reorder objects that use them. With parserOptions.project set, the rule can resolve imported-callee purity automatically.
#Examples
const palette = { primary: 'blue', danger: 'red' }; // → { danger, primary }pureImports lets the fixer reorder
// options: [{ pureImports: ['t.Object', 't.String', 't.Optional'] }]
import { t } from 'elysia';
const schema = {
name: t.String(),
id: t.Optional(t.String())
}; // → { id, name } once the imports are marked pureSorted keys
const palette = { danger: 'red', primary: 'blue', success: 'green' };// options: [{ minKeys: 3 }]
const pair = { b: 1, a: 2 }; // below minKeys, not checked#Options
This rule accepts the following options:
| Option | Type | Default | Description |
|---|---|---|---|
order | 'asc' | 'desc' | 'asc' | Sort direction: "asc" or "desc". |
caseSensitive | boolean | false | Case-sensitive comparison when true. |
natural | boolean | false | Numeric-aware comparison (a2 before a10). |
minKeys | integer | 2 | Minimum number of properties before an object is checked (minimum 2). |
variablesBeforeFunctions | boolean | false | When true, non-function values are sorted before function values. |
pureImports | string[] | [] | Callee names or member paths (e.g. "t.Object") to treat as side-effect-free, so the autofix may reorder objects containing those calls. |
{
rules: {
'absolute/sort-keys-fixable': [
'warn',
{ natural: true, pureImports: ['t.Object', 't.String'] }
]
}
}#Related Rules
#When Not To Use It
Objects whose key order is meaningful (ordered form fields, prioritized config) should not be sorted. Disable the rule for those files, or keep the order intentional with an inline disable comment.