-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add sort-astro-attributes rule
- Loading branch information
Showing
8 changed files
with
674 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
--- | ||
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 | ||
interface Options { | ||
type?: 'alphabetical' | 'natural' | 'line-length' | ||
order?: 'asc' | 'desc' | ||
'ignore-case'?: boolean | ||
} | ||
``` | ||
|
||
### 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. | ||
|
||
## ⚙️ 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" | ||
} | ||
] | ||
} | ||
} | ||
``` | ||
|
||
```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', | ||
}, | ||
], | ||
}, | ||
}, | ||
] | ||
``` | ||
|
||
::: | ||
|
||
## 🚀 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
import type { TSESTree } from '@typescript-eslint/types' | ||
import type { AST } from 'astro-eslint-parser' | ||
|
||
import path from 'path' | ||
|
||
import type { SortingNode } from '../typings' | ||
|
||
import { createEslintRule } from '../utils/create-eslint-rule' | ||
import { rangeToDiff } from '../utils/range-to-diff' | ||
import { SortOrder, SortType } from '../typings' | ||
import { makeFixes } from '../utils/make-fixes' | ||
import { sortNodes } from '../utils/sort-nodes' | ||
import { pairwise } from '../utils/pairwise' | ||
import { complete } from '../utils/complete' | ||
import { compare } from '../utils/compare' | ||
|
||
export const RULE_NAME = 'sort-astro-attributes' | ||
|
||
type MESSAGE_ID = 'unexpectedAstroAttributesOrder' | ||
|
||
type Options = [ | ||
Partial<{ | ||
'ignore-case': boolean | ||
order: SortOrder | ||
type: SortType | ||
}>, | ||
] | ||
|
||
export default createEslintRule<Options, MESSAGE_ID>({ | ||
name: RULE_NAME, | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'enforce sorted union types', | ||
recommended: false, | ||
}, | ||
fixable: 'code', | ||
schema: [ | ||
{ | ||
type: 'object', | ||
properties: { | ||
type: { | ||
enum: [ | ||
SortType.alphabetical, | ||
SortType.natural, | ||
SortType['line-length'], | ||
], | ||
default: SortType.natural, | ||
}, | ||
order: { | ||
enum: [SortOrder.asc, SortOrder.desc], | ||
default: SortOrder.asc, | ||
}, | ||
'ignore-case': { | ||
type: 'boolean', | ||
default: false, | ||
}, | ||
}, | ||
additionalProperties: false, | ||
}, | ||
], | ||
messages: { | ||
unexpectedAstroAttributesOrder: | ||
'Expected "{{right}}" to come before "{{left}}"', | ||
}, | ||
}, | ||
defaultOptions: [ | ||
{ | ||
type: SortType.alphabetical, | ||
order: SortOrder.asc, | ||
}, | ||
], | ||
// @ts-ignore | ||
create: context => { | ||
if (path.extname(context.getFilename()) !== '.astro') { | ||
return {} | ||
} | ||
|
||
return { | ||
JSXElement: (node: AST.JSXElement) => { | ||
let { attributes } = node.openingElement | ||
|
||
if (attributes.length > 1) { | ||
let options = complete(context.options.at(0), { | ||
type: SortType.alphabetical, | ||
order: SortOrder.asc, | ||
'ignore-case': false, | ||
}) | ||
|
||
let source = context.getSourceCode() | ||
|
||
let parts: SortingNode[][] = attributes.reduce( | ||
(accumulator: SortingNode[][], attribute) => { | ||
if (attribute.type === 'JSXSpreadAttribute') { | ||
accumulator.push([]) | ||
return accumulator | ||
} | ||
|
||
accumulator.at(-1)!.push({ | ||
size: rangeToDiff(attribute.range), | ||
node: attribute as unknown as TSESTree.Node, | ||
name: | ||
typeof attribute.name.name === 'string' | ||
? attribute.name.name | ||
: source.text.slice(...attribute.name.range), | ||
}) | ||
|
||
return accumulator | ||
}, | ||
[[]], | ||
) | ||
|
||
for (let nodes of parts) { | ||
pairwise(nodes, (left, right) => { | ||
if (compare(left, right, options)) { | ||
context.report({ | ||
messageId: 'unexpectedAstroAttributesOrder', | ||
data: { | ||
left: left.name, | ||
right: right.name, | ||
}, | ||
node: right.node, | ||
fix: fixer => | ||
makeFixes(fixer, nodes, sortNodes(nodes, options), source), | ||
}) | ||
} | ||
}) | ||
} | ||
} | ||
}, | ||
} | ||
}, | ||
}) |
Oops, something went wrong.