AbsoluteJS

absolute/sort-exports

Enforce that top-level export declarations are sorted by exported name, optionally variables before functions.

Imports & ExportsSuggestionFixable
View on GitHub

#Rule Details

Enforces that contiguous blocks of top-level named exports are sorted by exported name, with an autofix that reorders the block. Consistent export order makes files scannable and keeps diffs small. Only blocks with at least minKeys exports are checked, and export type aliases break a block (they are ignored).

Sorting respects several toggles: order (asc/desc), caseSensitive, natural (numeric-aware), and variablesBeforeFunctions (non-function exports first). The rule is dependency-safe — it will not reorder when the current order has a forward reference between exports, nor when sorting would create one — and decorators move with their class.

#Examples

Incorrect
TS
export const gamma = 3;
export const alpha = 1;   //reordered to alpha, beta, gamma
export const beta = 2;

variablesBeforeFunctions

TS
// options: [{ variablesBeforeFunctions: true }]
export function build() {}
export const config = {}; //const sorts before function
Correct

Sorted ascending

TS
export const alpha = 1;
export const beta = 2;
export const gamma = 3;
TS
// A forward dependency is detected, so the block is left unsorted
export const doubled = base * 2;
export const base = 21;

#Options

This rule accepts the following options:

OptionTypeDefaultDescription
order'asc' | 'desc''asc'Sort direction: "asc" or "desc".
caseSensitivebooleanfalseWhen false, names are lowercased before comparison (case-insensitive sort).
naturalbooleanfalseNumeric-aware comparison so export2 sorts before export10.
minKeysinteger2Minimum number of named exports in a contiguous block before it is checked (minimum 2).
variablesBeforeFunctionsbooleanfalseWhen true, non-function exports are sorted before function exports.
JS
{
	rules: {
		'absolute/sort-exports': [
			'warn',
			{ order: 'asc', natural: true, variablesBeforeFunctions: true }
		]
	}
}

#When Not To Use It

If you group exports semantically (by feature or by call order) rather than alphabetically, sorting will fight that structure — leave the rule off or raise minKeys.

#Resources