sort-svelte-attributes
Enforce sorted attributes in Svelte elements.
Maintaining a consistent order of attributes in Svelte elements enhances readability and maintainability. This rule ensures that attributes are sorted, making the structure of your Svelte components more predictable and easier to manage.
It’s safe. The rule considers spread elements in an attributes list and does not break component functionality.
Deprecated
This rule has been deprecated in favor of the svelte/sort-attributes
rule.
See Deprecate rules for sorting Vue, Svelte and Astro attributes issue for more information.
Try it out
Options
This rule accepts an options object with the following properties:
type
default:'alphabetical'
Specifies the sorting method.
'alphabetical'
— Sort items alphabetically (e.g., “a” < “b” < “c”).'natural'
— Sort items in a natural order (e.g., “item2” < “item10”).'line-length'
— Sort items by the length of the code line (shorter lines first).
order
default:'asc'
Determines whether the sorted items should be in ascending or descending order.
'asc'
— Sort items in ascending order (A to Z, 1 to 9).'desc'
— Sort items in descending order (Z to A, 9 to 1).
ignoreCase
default:true
Controls whether sorting should be case-sensitive or not.
true
— Ignore case when sorting alphabetically or naturally (e.g., “A” and “a” are the same).false
— Consider case when sorting (e.g., “A” comes before “a”).
specialCharacters
default:keep
Controls whether special characters should be trimmed, removed or kept before sorting.
'keep'
— Keep special characters when sorting (e.g., “_a” comes before “a”).'trim'
— Trim special characters when sorting alphabetically or naturally (e.g., “_a” and “a” are the same).'remove'
— Remove special characters when sorting (e.g., “/a/b” and “ab” are the same).
groups
type: Array<string | string[]>
[]
Allows you to specify a list of Svelte attribute groups for sorting. Groups help organize attributes into categories, making your components more readable and maintainable.
Predefined groups:
'multiline'
— Attributes whose length exceeds one line, such as event handlers or functions.'shorthand'
— Shorthand attributes, which are used without a value, typically for boolean props.'svelte-shorthand'
— Svelte’s shorthand for replacingname={name}
with{name}
.'unknown'
— Svelte attributes that don’t fit into any group specified in thegroups
option.
If the unknown
group is not specified in the groups
option, it will automatically be added to the end of the list.
Each attribute member will be assigned a single group specified in the groups
option (or the unknown
group if no match is found). The order of items in the groups
option determines how groups are ordered.
Within a given group, members will be sorted according to the type
, order
, ignoreCase
, etc. options.
Individual groups can be combined together by placing them in an array. The order of groups in that array does not matter. All members of the groups in the array will be sorted together as if they were part of a single group.
Example
<button
{/* 'multiline' */}
on:click={event => {
event.preventDefault()
fetchDate()
}}
{/* 'shorthand' */}
autofocus
{/* 'svelte-shorthand' */}
{disabled}
>
Click me
</button>
groups
option configuration:
{
groups: [
'multiline',
'shorthand',
'svelte-shorthand',
'unknown',
]
}
customGroups
type: { [groupName: string]: string | string[] }
{}
You can define your own groups and use custom glob patterns or regex to match specific Svelte attributes.
Use the matcher
option to specify the pattern matching method.
Each key of customGroups
represents a group name which you can then use in the groups
option. The value for each key can either be of type:
string
— An attribute’s name matching the value will be marked as part of the group referenced by the key.string[]
— An attribute’s name matching any of the values of the array will be marked as part of the group referenced by the key. The order of values in the array does not matter.
Custom group matching takes precedence over predefined group matching.
Example
{
groups: [
+ ['this', 'bind-this'],
'multiline',
+ 'style-props',
+ 'class',
+ ['bind-directives', 'use-directives'],
['shorthand', 'svelte-shorthand'],
],
+ customGroups: {
+ 'style-props': '--style-props',
+ 'bind-directives': 'bind:*',
+ 'use-directives': 'use:*',
+ 'bind-this': 'bind:this',
+ class: 'class',
+ this: 'this',
+ },
}
matcher
default:'minimatch'
Determines the matcher used for patterns in the customGroups
option.
'minimatch'
— Use the minimatch library for pattern matching.'regex'
— Use regular expressions for pattern matching.
Usage
In order to start using this rule, you need to install additional dependency:
Version
This rule was introduced in v2.0.0.