Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add sort-astro-attributes rule #36

Merged
merged 5 commits into from
Jul 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/rules/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ description: ESLint Plugin Perfectionist list of rules
| Name | Description | 🔧 |
| :------------------------------------------------------ | :------------------------------------------ | :- |
| [sort-array-includes](/rules/sort-array-includes) | enforce sorted arrays before include method | 🔧 |
| [sort-astro-attributes](/rules/sort-astro-attributes) | enforce sorted Astro attributes | 🔧 |
| [sort-classes](/rules/sort-classes) | enforce sorted classes | 🔧 |
| [sort-enums](/rules/sort-enums) | enforce sorted TypeScript enums | 🔧 |
| [sort-exports](/rules/sort-exports) | enforce sorted exports | 🔧 |
Expand All @@ -23,7 +24,7 @@ description: ESLint Plugin Perfectionist list of rules
| [sort-named-imports](/rules/sort-named-imports) | enforce sorted named imports | 🔧 |
| [sort-object-types](/rules/sort-object-types) | enforce sorted object types | 🔧 |
| [sort-objects](/rules/sort-objects) | enforce sorted objects | 🔧 |
| [sort-svelte-attributes](/rules/sort-svelte-attributes) | enforce sorted union types | 🔧 |
| [sort-svelte-attributes](/rules/sort-svelte-attributes) | enforce sorted Svelte attributes | 🔧 |
| [sort-union-types](/rules/sort-union-types) | enforce sorted union types | 🔧 |

<!-- end auto-generated rules list -->
148 changes: 148 additions & 0 deletions docs/rules/sort-astro-attributes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
---
title: sort-astro-attributes
description: ESLint Plugin Perfectionist rule which enforce sorted ES class members
---

# sort-astro-attributes

💼 This rule is enabled in the following [configs](/configs/): `recommended-alphabetical`, `recommended-line-length`, `recommended-natural`.

🔧 This rule is automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/latest/user-guide/command-line-interface#--fix).

<!-- end auto-generated rule header -->

## 📖 Rule Details

Enforce sorted attributes in Astro elements.

It's **safe**. The rule considers spread elements in an attributes list and does not break component functionality.

## 🔧 Options

This rule accepts an options object with the following properties:

```ts
type Group =
| 'multiline'
| 'shorthand'
| 'astro-shorthand'
| 'unknown'

interface Options {
type?: 'alphabetical' | 'natural' | 'line-length'
order?: 'asc' | 'desc'
'ignore-case'?: boolean
groups?: (Group | Group[])[]
'custom-groups': { [key in T[number]]: string[] | string }
}
```

### type

<sub>(default: `'alphabetical'`)</sub>

- `alphabetical` - sort alphabetically.
- `natural` - sort in natural order.
- `line-length` - sort by code line length.

### order

<sub>(default: `'asc'`)</sub>

- `asc` - enforce properties to be in ascending order.
- `desc` - enforce properties to be in descending order.

### ignore-case

<sub>(default: `false`)</sub>

Only affects alphabetical and natural sorting. When `true` the rule ignores the case-sensitivity of the order.

### groups

<sub>(default: `[]`)</sub>

You can set up a list of Astro attribute groups for sorting. Groups can be combined. There are predefined groups: `'multiline'`, `'shorthand'`, `'astro-shorthand'`.

### custom-groups

<sub>(default: `{}`)</sub>

You can define your own groups for Astro attributes. The [minimatch](https://github.com/isaacs/minimatch) library is used for pattern matching.

Example:

```
{
"custom-groups": {
"callback": "on*"
}
}
```

## ⚙️ Usage

In order to start using this rule, you need to install additional dependency:

- `astro-eslint-parser`

::: code-group

```json [Legacy Config]
// .eslintrc
{
"plugins": ["perfectionist"],
"rules": {
"perfectionist/sort-astro-attributes": [
"error",
{
"type": "natural",
"order": "asc",
"groups": [
"multiline",
"unknown",
["shorthand", "astro-shorthand"]
]
}
]
}
}
```

```js [Flat Config]
// eslint.config.js
import perfectionist from 'eslint-plugin-perfectionist'

export default [
{
plugins: {
perfectionist,
},
rules: {
'perfectionist/sort-astro-attributes': [
'error',
{
type: 'natural',
order: 'asc',
groups: [
'multiline',
'unknown',
['shorthand', 'astro-shorthand'],
],
},
],
},
},
]
```

:::

## 🚀 Version

Coming soon.

## 📚 Resources

- [Rule source](https://github.com/azat-io/eslint-plugin-perfectionist/blob/main/rules/sort-astro-attributes.ts)
- [Test source](https://github.com/azat-io/eslint-plugin-perfectionist/blob/main/test/sort-astro-attributes.test.ts)
3 changes: 3 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sortSvelteAttributes, { RULE_NAME as sortSvelteAttributesName } from './rules/sort-svelte-attributes'
import sortAstroAttributes, { RULE_NAME as sortAstroAttributesName } from './rules/sort-astro-attributes'
import sortArrayIncludes, { RULE_NAME as sortArrayIncludesName } from './rules/sort-array-includes'
import sortNamedImports, { RULE_NAME as sortNamedImportsName } from './rules/sort-named-imports'
import sortNamedExports, { RULE_NAME as sortNamedExportsName } from './rules/sort-named-exports'
Expand Down Expand Up @@ -92,6 +93,7 @@ let createConfigWithOptions = (options: {
},
],
[sortSvelteAttributesName]: ['error'],
[sortAstroAttributesName]: ['error'],
[sortNamedExportsName]: ['error'],
[sortNamedImportsName]: ['error'],
[sortObjectTypesName]: ['error'],
Expand All @@ -116,6 +118,7 @@ let createConfigWithOptions = (options: {
export default {
rules: {
[sortArrayIncludesName]: sortArrayIncludes,
[sortAstroAttributesName]: sortAstroAttributes,
[sortClassesName]: sortClasses,
[sortEnumsName]: sortEnums,
[sortExportsName]: sortExports,
Expand Down
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,19 @@
"./package.json": "./package.json"
},
"peerDependenciesMeta": {
"astro-eslint-parser": {
"optional": true
},
"svelte": {
"optional": true
},
"svelte-eslint-parser": {
"optional": true
}
},

"peerDependencies": {
"astro-eslint-parser": "^0.14.0",
"eslint": ">=8.0.0",
"svelte": ">=3.0.0",
"svelte-eslint-parser": "^0.32.0"
Expand All @@ -81,6 +86,7 @@
"@typescript-eslint/eslint-plugin": "^5.62.0",
"@typescript-eslint/parser": "^5.62.0",
"@vitest/coverage-v8": "^0.33.0",
"astro-eslint-parser": "^0.14.0",
"changelogen": "^0.5.4",
"clean-publish": "^4.2.0",
"eslint": "^8.45.0",
Expand Down
33 changes: 33 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ export default [
| Name | Description | 🔧 |
| :------------------------------------------------------------------------------------------------- | :------------------------------------------ | :- |
| [sort-array-includes](https://eslint-plugin-perfectionist.azat.io/rules/sort-array-includes) | enforce sorted arrays before include method | 🔧 |
| [sort-astro-attributes](https://eslint-plugin-perfectionist.azat.io/rules/sort-astro-attributes) | enforce sorted Astro attributes | 🔧 |
| [sort-classes](https://eslint-plugin-perfectionist.azat.io/rules/sort-classes) | enforce sorted classes | 🔧 |
| [sort-enums](https://eslint-plugin-perfectionist.azat.io/rules/sort-enums) | enforce sorted TypeScript enums | 🔧 |
| [sort-exports](https://eslint-plugin-perfectionist.azat.io/rules/sort-exports) | enforce sorted exports | 🔧 |
Expand All @@ -152,7 +153,7 @@ export default [
| [sort-named-imports](https://eslint-plugin-perfectionist.azat.io/rules/sort-named-imports) | enforce sorted named imports | 🔧 |
| [sort-object-types](https://eslint-plugin-perfectionist.azat.io/rules/sort-object-types) | enforce sorted object types | 🔧 |
| [sort-objects](https://eslint-plugin-perfectionist.azat.io/rules/sort-objects) | enforce sorted objects | 🔧 |
| [sort-svelte-attributes](https://eslint-plugin-perfectionist.azat.io/rules/sort-svelte-attributes) | enforce sorted union types | 🔧 |
| [sort-svelte-attributes](https://eslint-plugin-perfectionist.azat.io/rules/sort-svelte-attributes) | enforce sorted Svelte attributes | 🔧 |
| [sort-union-types](https://eslint-plugin-perfectionist.azat.io/rules/sort-union-types) | enforce sorted union types | 🔧 |

<!-- end auto-generated rules list -->
Expand Down
Loading
Loading