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.

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”).

groups

default: []

Allows you to specify a list of Astro attribute groups for sorting. Groups help organize attributes into categories, prioritizing them during sorting. Multiple groups can be combined to achieve the desired sorting order.

There are predefined groups available: 'multiline', 'shorthand', 'astro-shorthand'.

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 other group.

Example:

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

customGroups

default: {}

You can define your own groups for Astro attributes using custom glob patterns for matching.

Example:

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

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