Perfectionist

sort-astro-attributes

Enforce sorted attributes in Astro elements.

Maintaining a consistent order of attributes in Astro elements is crucial for readability and maintainability. This rule ensures that attributes are sorted, making the structure of your elements more predictable and easier to manage.

Adopting this rule helps standardize code formatting across your project, facilitating better collaboration and reducing cognitive load for developers.

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 astro/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[]>

default: []

Allows you to specify a list of Astro attribute groups for sorting. Groups help organize attributes into categories, prioritizing them during sorting.

Predefined groups:

  • 'multiline' — Attributes with multiline values.
  • 'shorthand' — Shorthand attributes, which are used without a value, typically for boolean props.
  • 'astro-shorthand' — Astro shorthand attributes, where the attribute name and value are the same.
  • 'unknown' — Attributes that don’t fit into any group specified in the groups 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 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

<Form
  {/* 'multiline' */}
  onClick={event => {
    event.preventDefault()
    handleSubmit()
  }}
  {/* 'shorthand' */}
  validate
  {/* 'astro-shorthand' */}
  {success}
/>

groups option configuration:

{
  groups: [
    'multiline',
    'shorthand',
    'astro-shorthand',
    'unknown',
  ]
}

customGroups

type: { [groupName: string]: string | string[] }

default: {}

You can define your own groups and use custom glob patterns or regex to match specific Astro 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

<Form
  {/* 'callback' */}
  onClick={event => {
    event.preventDefault()
    handleSubmit()
  }}
  {/* 'shorthand' */}
  validate
  {/* 'astro-shorthand' */}
  {success}
  {/* 'unknown' because 'multiline' is not in groups */}
  notCallbackEvent={foo => {
    console.log('bar')
  }}
/>

groups and customGroups configuration:

 {
   groups: [
+    'callback',
     ['shorthand', 'astro-shorthand'],
     'unknown',
   ],
+  customGroups: {   
+    callback: 'on*'
+  }                 
 }

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.

Resources

Table of Contents