map_keys_ordering
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',};Enabling this rule
Section titled “Enabling this rule”Outside preset: pedantic, enabling it by name is not enough — set order::
rules: map_keys_ordering: order: alphabeticalOptions
Section titled “Options”many_lints: rules: map_keys_ordering: order: alphabetical min_entries: 5rules: 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 |
Turning this rule off
Section titled “Turning this rule off”To disable this rule:
rules: map_keys_ordering: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Related rules
Section titled “Related rules”arguments_ordering— Keep named arguments in a configured order.initializers_ordering— Keep constructor initializers in field order.member_ordering— Keep class members in a configured order.parameters_ordering— Keep named parameters in a configured order.