Skip to content

map_keys_ordering

v1.0.0WarningConfigurableCode Organization

This rule flags a map literal whose keys are not in the configured order.

A long map literal is a lookup table, and an unordered one has to be read end to end to answer “is this key here?”. Ordering keeps a diff honest too: a new key lands in the middle where it can be seen, rather than appended at the end beside a duplicate nobody noticed.

Outside preset: pedantic, this rule reports nothing until configured. Many map literals are deliberately ordered by meaning — a config map mirroring a form’s field order, a theme map going from lightest to darkest. The pedantic preset chooses alphabetical order.

A literal containing a computed key, a spread, an if or a for is skipped entirely rather than partially ordered: ordering the rest around an unsortable entry would produce an arrangement the user cannot reach.

This rule is in the pedantic preset.

const labels = {
'banana': 'Banana',
'apple': 'Apple',
'cherry': 'Cherry',
'date': 'Date',
'elder': 'Elderberry',
};
const labels = {
'apple': 'Apple',
'banana': 'Banana',
'cherry': 'Cherry',
'date': 'Date',
'elder': 'Elderberry',
};

Outside preset: pedantic, enabling it by name is not enough — set order::

many_lints.yaml
rules:
map_keys_ordering:
order: alphabetical
analysis_options.yaml
many_lints:
rules:
map_keys_ordering:
order: alphabetical
min_entries: 5
Option Type Default Description
order string alphabetical, alphabetical_case_sensitive or by_length. Unset means the rule is silent
min_entries int 5 How many entries a literal needs before order is checked

To disable this rule:

many_lints.yaml
rules:
map_keys_ordering: false

To keep the rule on but skip certain paths, use per-rule exclude.