# svelte/sort-attributes
enforce order of attributes
- 🔧 The
--fix
option on the command line can automatically fix some of the problems reported by this rule.
# 📖 Rule Details
This rule aims to enforce ordering of attributes.
The default order is:
this
property.bind:this
directive.id
attribute.name
attribute.slot
attribute.--style-props
(Alphabetical order within the same group.)style
attribute, andstyle:
directives.class
attribute.class:
directives. (Alphabetical order within the same group.)- other attributes. (Alphabetical order within the same group.)
bind:
directives (other thenbind:this
), andon:
directives.use:
directives. (Alphabetical order within the same group.)transition:
directive.in:
directive.out:
directive.animate:
directive.let:
directives. (Alphabetical order within the same group.)
<script>
/* eslint svelte/sort-attributes: "error" */
</script>
<!-- ✓ GOOD -->
<svelte:component
this={component}
--style-props={color}
bind:value={componentValue}
on:changeValue={handleChange}
bind:metaData
/>
<input
bind:this={foo}
id="foo"
style="width: 150px;"
style:color
class="my-input"
class:disable
class:enable={!disable}
bind:value={inputValue}
use:action={parameters}
transition:fn
in:fn
out:fn
animate:name
/>
<slot name="content" {abc} {def} />
<!-- ✗ BAD -->
<svelte:component
bind:value={componentValue}
Attribute 'this' should go before 'bind:value'. (svelte/sort-attributes)this={component}
on:changeValue={handleChange}
Attribute 'def' should go before 'bind:value'. (svelte/sort-attributes){def}
Attribute 'data-foo' should go before 'bind:value'. (svelte/sort-attributes)data-foo
Attribute 'abc' should go before 'bind:value'. (svelte/sort-attributes){abc}
bind:metaData
Attribute '--style-props' should go before 'bind:value'. (svelte/sort-attributes)--style-props={color}
/>
<input
id="foo"
Attribute 'bind:this' should go before 'id'. (svelte/sort-attributes)bind:this={foo}
style:color
style="width: 150px;"
class="my-input"
class:enable={!disable}
Attribute 'class:disable' should go before 'class:enable'. (svelte/sort-attributes)class:disable
animate:name
Attribute 'use:action' should go before 'animate:name'. (svelte/sort-attributes)use:action
Attribute 'transition:fn' should go before 'animate:name'. (svelte/sort-attributes)transition:fn
Attribute 'bind:value' should go before 'animate:name'. (svelte/sort-attributes)bind:value={inputValue}
Attribute 'in:fn' should go before 'animate:name'. (svelte/sort-attributes)in:fn
Attribute 'out:fn' should go before 'animate:name'. (svelte/sort-attributes)out:fn
/>
<slot name="content" {def} Attribute 'abc' should go before 'def'. (svelte/sort-attributes){abc}Attribute 'data-foo' should go before 'def'. (svelte/sort-attributes) data-foo />
If there is a spread attribute between the attributes, it will not be reported as changing the order can change the behavior.
<script>
/* eslint svelte/sort-attributes: "error" */
</script>
<!-- ✓ GOOD -->
<div c d {...attrs} a b />
<!-- ✗ BAD -->
<div d Attribute 'c' should go before 'd'. (svelte/sort-attributes)cAttribute 'a' should go before 'b'. (svelte/sort-attributes) {...attrs} b a />
# 🔧 Options
{
"svelte/sort-attributes": [
"error",
{
"order": [
// `this` property.
"this",
// `bind:this` directive.
"bind:this",
// `id` attribute.
"id",
// `name` attribute.
"name",
// `slot` attribute.
"slot",
// `--style-props` (Alphabetical order within the same group.)
{ "match": "/^--/u", "sort": "alphabetical" },
// `style` attribute, and `style:` directives.
["style", "/^style:/u"],
// `class` attribute.
"class",
// `class:` directives. (Alphabetical order within the same group.)
{ "match": "/^class:/u", "sort": "alphabetical" },
// other attributes. (Alphabetical order within the same group.)
{
"match": ["!/:/u", "!/^(?:this|id|name|style|class)$/u", "!/^--/u"],
"sort": "alphabetical"
},
// `bind:` directives (other then `bind:this`), and `on:` directives.
["/^bind:/u", "!bind:this", "/^on:/u"],
// `use:` directives. (Alphabetical order within the same group.)
{ "match": "/^use:/u", "sort": "alphabetical" },
// `transition:` directive.
{ "match": "/^transition:/u", "sort": "alphabetical" },
// `in:` directive.
{ "match": "/^in:/u", "sort": "alphabetical" },
// `out:` directive.
{ "match": "/^out:/u", "sort": "alphabetical" },
// `animate:` directive.
{ "match": "/^animate:/u", "sort": "alphabetical" },
// `let:` directives. (Alphabetical order within the same group.)
{ "match": "/^let:/u", "sort": "alphabetical" }
]
}
]
}
order
… Specify an array of your preferred attribute order. Array elements accept strings, string arrays, and objects.- String … Specify the name or pattern of the attribute.
- String array … Specifies an array of the names or patterns of the attributes to be grouped. It will not be sorted within this same group.
- Object … Specifies an object with a definition for sorting within the same group.
match
… Specifies an array or string of the name or pattern of the attributes to be grouped.sort
… Specify the sorting method. Currently, only"alphabetical"
is supported."alphabetical"
… Sorts the attributes of the same group in alphabetical order."ignore"
… Attributes in the same group are not sorted.
Note that the behavior may change depending on how you specify the order
setting.
For example, bind:value
and on:input={() => console.log(value)}
behave differently depending on the order. See https://svelte.dev/docs#template-syntax-element-directives-bind-property for details.
By default it is designed to be sorted safely.
You can use the following formats for names or patterns:
"foo"
… Matches only thefoo
attribute name."/foo/"
… Matches attribute names that match the/foo/
regex. That is, it matches the attribute name includingfoo
."!foo"
… Excludefoo
attribute from the matched attribute names. When used first in the array or alone, matches other than thefoo
attribute name."!/foo/"
… Excludes attributes that match the/foo/
regex from the matched attribute names. When used first in the array or alone, matches an attribute name that does not match the/foo/
regex.["style", "/^style:/u"]
… Matches thestyle
attribute or the attribute name that matches the/^style:/u
regex.["/^bind:/u", "!bind:this", "/^on:/u"]
… Matches an attribute name that matches/^bind:/u
and other thanbind:this
, or an attribute name that matches/^on:/u
.
# { order: [ /*See below*/ ] }
<script>
/* eslint svelte/sort-attributes: ["error", {
"order": [
"id",
"class",
"/^class:/u",
"value",
"src",
"/^data-/u",
"style",
"/^style:/u",
"/^on:/u",
"/^use:/u",
"/^animate:/u",
"/^transition:/u",
"/^in:/u",
"/^out:/u",
"bind:this",
["/^bind:/u", "!bind:this"],
{
"match": ["!/:/u", "!/^(?:id|class|value|src|style)$/u", "!/^data-/u"],
"sort": "alphabetical"
},
]
}] */
</script>
<!-- ✓ GOOD -->
<MyComponent data-foo bind:this={comp} bind:data {abc} {def} />
<input
id="foo"
class="my-block"
class:bar
value="abc"
data-value="x"
style="width: 30px;"
style:color
animate:name
transition:fn
in:fn
out:fn
bind:this={foo}
/>
<img id="bar" {src} alt="bar" />
<!-- ✗ BAD -->
<MyComponent bind:data Attribute 'bind:this' should go before 'bind:data'. (svelte/sort-attributes)bind:this={comp}Attribute 'data-foo' should go before 'bind:data'. (svelte/sort-attributes) {abc} {def} data-foo />
<input
class:bar
Attribute 'class' should go before 'class:bar'. (svelte/sort-attributes)class="my-block"
Attribute 'id' should go before 'class:bar'. (svelte/sort-attributes)id="foo"
bind:this={foo}
Attribute 'value' should go before 'bind:this'. (svelte/sort-attributes)value="abc"
Attribute 'style:color' should go before 'bind:this'. (svelte/sort-attributes)style:color
Attribute 'style' should go before 'bind:this'. (svelte/sort-attributes)style="width: 30px;"
Attribute 'data-value' should go before 'bind:this'. (svelte/sort-attributes)data-value="x"
Attribute 'animate:name' should go before 'bind:this'. (svelte/sort-attributes)animate:name
Attribute 'in:fn' should go before 'bind:this'. (svelte/sort-attributes)in:fn
Attribute 'out:fn' should go before 'bind:this'. (svelte/sort-attributes)out:fn
Attribute 'transition:fn' should go before 'bind:this'. (svelte/sort-attributes)transition:fn
/>
<img alt="bar" Attribute 'src' should go before 'alt'. (svelte/sort-attributes){src}Attribute 'id' should go before 'alt'. (svelte/sort-attributes) id="bar" />
# 🚀 Version
This rule was introduced in eslint-plugin-svelte v2.4.0