AbsoluteJS

absolute/sort-keys-fixable

Enforce sorted keys in object literals with a comment-preserving auto-fix for provably safe cases.

Imports & ExportsSuggestionFixableType-aware (optional)
View on GitHub

#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

Incorrect
TS
const palette = { primary: 'blue', danger: 'red' }; //{ danger, primary }

pureImports lets the fixer reorder

TS
// 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 pure
Correct

Sorted keys

TS
const palette = { danger: 'red', primary: 'blue', success: 'green' };
TS
// options: [{ minKeys: 3 }]
const pair = { b: 1, a: 2 }; // below minKeys, not checked

#Options

This rule accepts the following options:

OptionTypeDefaultDescription
order'asc' | 'desc''asc'Sort direction: "asc" or "desc".
caseSensitivebooleanfalseCase-sensitive comparison when true.
naturalbooleanfalseNumeric-aware comparison (a2 before a10).
minKeysinteger2Minimum number of properties before an object is checked (minimum 2).
variablesBeforeFunctionsbooleanfalseWhen true, non-function values are sorted before function values.
pureImportsstring[][]Callee names or member paths (e.g. "t.Object") to treat as side-effect-free, so the autofix may reorder objects containing those calls.
JS
{
	rules: {
		'absolute/sort-keys-fixable': [
			'warn',
			{ natural: true, pureImports: ['t.Object', 't.String'] }
		]
	}
}

#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.

#Resources