attr-order
๐ง This rule supports auto-fix with the --fix option.
Enforces a consistent order of attributes on elements. By default, attributes are sorted alphabetically. You can configure priority lists, predefined groups (global, event, aria, data, spread), and custom patterns to define the desired order.
โ Examples of incorrect code for this rule
<div style="color: red" class="foo" id="bar"></div>
โ Examples of correct code for this rule
<div class="foo" id="bar" style="color: red"></div>
Configurationโ
Priority listโ
{
"attr-order": ["id", "class", "style"]
}
Attributes matching the list are placed first in the specified order. Unmatched attributes follow alphabetically.
Predefined groupsโ
{
"attr-order": [{ "group": "global" }, { "group": "aria" }, { "group": "event" }, { "group": "data" }]
}
| Group | Matches |
|---|---|
global | HTML global attributes (id, class, style, etc.) |
event | Event handler attributes (onclick, onchange, etc.) |
aria | ARIA attributes (aria-label, aria-hidden, etc.) |
data | Custom data attributes (data-*) |
spread | Spread attributes (JSX {...props}) |
Pattern matchingโ
{
"attr-order": [{ "pattern": "^data-" }]
}
Group-internal sort orderโ
{
"attr-order": [
{ "group": "global", "order": "alphabetical" },
{ "group": "aria", "order": ["aria-label", "aria-describedby", "aria-hidden"] },
{ "group": "event", "order": "source-order" }
]
}
"alphabetical"(default) โ Sort alphabetically within the group."source-order"โ Preserve the original order within the group.string[]โ Fixed order. Unlisted attributes are appended alphabetically.
Optionsโ
alphabeticalโ
Type: boolean Default: true
Whether to sort unmatched attributes alphabetically. When false, unmatched attributes preserve their source order.
{
"attr-order": {
"value": ["id", "class"],
"options": { "alphabetical": false }
}
}
Interfaceโ
{
"attr-order": undefined[]
}
An ordered list of attribute order entries. Each entry can be a string (attribute name), or an object specifying a group or pattern.
Optionsโ
{
"attr-order": {
"options": {
"alphabetical"?: boolean
}
}
}
| Property | Type | Default Value | Description |
|---|---|---|---|
alphabetical | boolean | true | Whether to sort unmatched attributes alphabetically. |
Default Severityโ
warning