useSortedAttributes (HTML)
Summary
Section titled “Summary”- Rule available since:
v2.5.0 - Diagnostic Category:
assist/source/useSortedAttributes - Sources:
- Inspired from
@html-eslint/sort-attrs - Inspired from
vue/attributes-order - Inspired from
svelte/sort-attributes - Inspired from
astro/sort-attributes
- Inspired from
How to enable in your editor
Section titled “How to enable in your editor”{ "editor.codeActionsOnSave": { "source.action.useSortedAttributes.biome": "explicit", "source.fixAll.biome": "explicit" }}{ "code_actions_on_format": { "source.action.useSortedAttributes.biome": true, "source.fixAll.biome": true }}source.action.useSortedAttributes.biome How to configure
Section titled “How to configure”{ "assist": { "actions": { "source": { "useSortedAttributes": "on" } } }}Description
Section titled “Description”Sort HTML attributes and framework directives into a consistent order.
Regular HTML attributes and Astro directives are sorted by name. Svelte directives follow
the order from eslint-plugin-svelte’s
sort-attributes rule,
and Vue directives follow the
Vue attribute-order convention.
Attributes within the same category use the configured sortOrder.
A spread attribute or Vue’s
v-bind="object"
can provide or replace several attributes. The action leaves each spread in place and sorts
only the consecutive attributes on either side, preserving override behavior.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”<input type="text" id="name" name="name" />code-block.html:1:1 assist/source/useSortedAttributes FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ The attributes are not sorted.
> 1 │ <input type="text" id="name" name="name" />
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Safe fix: Sort the HTML attributes.
1 │ - <input·type="text"·id="name"·name="name"·/>
1 │ + <input·id="name"·name="name"·type="text"·/>
2 2 │
<textarea id="mytextarea" name="textarea" rows="5" cols="20" data-1="" data-11="" data-12="" data-2="">Hello, world!</textarea>code-block.html:1:1 assist/source/useSortedAttributes FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ The attributes are not sorted.
> 1 │ <textarea id="mytextarea" name="textarea" rows="5" cols="20" data-1="" data-11="" data-12="" data-2="">Hello, world!</textarea>
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Safe fix: Sort the HTML attributes.
1 │ - <textarea·id="mytextarea"·name="textarea"·rows="5"·cols="20"·data-1=""·data-11=""·data-12=""·data-2="">Hello,·world!</textarea>
1 │ + <textarea·cols="20"·data-1=""·data-2=""·data-11=""·data-12=""·id="mytextarea"·name="textarea"·rows="5"·>Hello,·world!</textarea>
2 2 │
<svg slot="fallback" class="generic-avatar" transition:name="avatar">...</svg>code-block.astro:1:3 assist/source/useSortedAttributes FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ The attributes are not sorted.
> 1 │ <svg slot="fallback" class="generic-avatar" transition:name="avatar">...</svg>
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Safe fix: Sort the HTML attributes.
1 │ - ··<svg·slot="fallback"·class="generic-avatar"·transition:name="avatar">...</svg>
1 │ + ··<svg·class="generic-avatar"·slot="fallback"·transition:name="avatar">...</svg>
2 2 │
<input type="range" bind:value={b} min="0" max="10" />code-block.svelte:1:4 assist/source/useSortedAttributes FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ The attributes are not sorted.
> 1 │ <input type="range" bind:value={b} min="0" max="10" />
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Safe fix: Sort the HTML attributes.
1 │ - ··→ <input·type="range"·bind:value={b}·min="0"·max="10"·/>
1 │ + ··→ <input·max="10"·min="0"·type="range"·bind:value={b}·/>
2 2 │
<div bind:value2={a} bind:value1={a} {...props} style:color="red">...</div>code-block.svelte:1:4 assist/source/useSortedAttributes FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ The attributes are not sorted.
> 1 │ <div bind:value2={a} bind:value1={a} {...props} style:color="red">...</div>
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Safe fix: Sort the HTML attributes.
1 │ - ··→ <div·bind:value2={a}·bind:value1={a}·{...props}·style:color="red">...</div>
1 │ + ··→ <div·bind:value1={a}·bind:value2={a}·{...props}·style:color="red">...</div>
2 2 │
<input @input="onInput" :value="text" placeholder="Type here">code-block.vue:1:4 assist/source/useSortedAttributes FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ The attributes are not sorted.
> 1 │ <input @input="onInput" :value="text" placeholder="Type here">
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Safe fix: Sort the HTML attributes.
1 │ - ··→ <input·@input="onInput"·:value="text"·placeholder="Type·here">
1 │ + ··→ <input·placeholder="Type·here"·:value="text"·@input="onInput"·>
2 2 │
<input id="name" name="name" type="text" /><textarea cols="20" data-1="" data-2="" data-11="" data-12="" id="mytextarea" name="textarea" rows="5">Hello, world!</textarea> <svg class="generic-avatar" slot="fallback" transition:name="avatar">...</svg> <input max="10" min="0" type="range" bind:value={b} /> <div bind:value1={a} bind:value2={a} {...props} style:color="red">...</div> <input placeholder="Type here" :value="text" @input="onInput" >Options
Section titled “Options”sortOrder
Section titled “sortOrder”Selects natural or lexicographic ordering. Natural ordering compares numbers by value,
so data-2 comes before data-11, and is the default. Lexicographic ordering compares names
character by character.
The following configuration uses lexicographic order:
{ "assist": { "actions": { "source": { "useSortedAttributes": { "level": "on", "options": { "sortOrder": "lexicographic" } } } } }}<textarea id="mytextarea" name="textarea" rows="5" cols="20" data-1="" data-2="" data-11="" data-12="">Hello, world!</textarea>code-block.html ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Source action diff:
1 │ - <textarea·id="mytextarea"·name="textarea"·rows="5"·cols="20"·data-1=""·data-2=""·data-11=""·data-12="">Hello,·world!</textarea>
1 │ + <textarea·cols="20"·data-1=""·data-11=""·data-12=""·data-2=""·id="mytextarea"·name="textarea"·rows="5"·>Hello,·world!</textarea>
2 2 │
sortFirst
Section titled “sortFirst”A list of attribute names that should be sorted before all other attributes, in the order they appear in this list. The remaining attributes are sorted after the listed ones. Listed attributes take precedence over the category-based ordering.
{ "assist": { "actions": { "source": { "useSortedAttributes": { "level": "on", "options": { "sortFirst": [ "type" ] } } } } }}<input id="name" name="name" type="text" />code-block.html:1:1 assist/source/useSortedAttributes FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ The attributes are not sorted.
> 1 │ <input id="name" name="name" type="text" />
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2 │
ℹ Safe fix: Sort the HTML attributes.
1 │ - <input·id="name"·name="name"·type="text"·/>
1 │ + <input·type="text"·id="name"·name="name"·/>
2 2 │
Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.