Perfectionist

sort-vue-attributes

Enforce sorted attributes in Vue elements.

Ensuring a consistent order of attributes in Vue elements is essential for readability and maintainability.

This rule enforces attribute sorting, making the structure of Vue components more predictable and easier to manage. By adopting this rule, developers can maintain a high standard of code organization and clarity in their Vue projects.

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 Vue attribute groups for sorting. Groups help organize attributes into categories, making your components more readable and maintainable.

Predefined groups:

  • 'multiline' — Attributes with multiline values, such as event handlers or functions.
  • 'shorthand' — Shorthand attributes, which are used without a value, typically for boolean props.
  • 'unknown' — Vue 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
  @onClick="() => {   // 'multiline'
    g.value = false
  }"
  validate            // 'shorthand'
/>

groups option configuration:

{
  groups: [
    'multiline',
    '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 Vue 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
  @onClick="@count++" // 'callback'
  validate            // 'shorthand'
  @notCallbackEvent={ // 'unknown' because 'multiline' is not in groups
    console.log('bar')
  }
/>

groups and customGroups configuration:

 {
   groups: [
+    'callback',
     '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