Skip to content

Commit

Permalink
feat(sort-classes): add custom-group to sort-classes rule
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavopedroni committed Jun 12, 2024
1 parent 861a381 commit 1773ffb
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 90 deletions.
17 changes: 17 additions & 0 deletions docs/rules/sort-classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ interface Options {
order?: 'asc' | 'desc'
'ignore-case'?: boolean
groups?: (Group | Group[])[]
'custom-groups'?: { [key: string]: string[] | string }
}
```

Expand Down Expand Up @@ -189,6 +190,22 @@ If you use [one of the configs](/configs/) exported by this plugin, you get the
}
```

### custom-groups

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

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

Example:

```
{
"custom-groups": {
"top": "id"
}
}
```

## ⚙️ Usage

::: code-group
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"docs:dev": "vitepress dev docs",
"docs:generate": "eslint-doc-generator --rule-list-columns name,description,fixable,hasSuggestions,deprecated --path-rule-list readme.md --url-rule-doc https://eslint-plugin-perfectionist.azat.io/rules/{name} --url-configs https://eslint-plugin-perfectionist.azat.io/configs && eslint-doc-generator --rule-list-columns name,description,fixable,hasSuggestions,deprecated --rule-doc-title-format name --path-rule-list ./docs/rules/index.md --url-rule-doc /rules/{name} --url-configs /configs/ && prettier --write readme.md ./docs/rules/index.md",
"docs:build": "vitepress build docs",
"format:fix": "prettier --write \"**/*.{js,ts,json,md,yml}\"",
"release": "pnpm release:check && pnpm release:version && pnpm release:publish",
"release:check": "pnpm test && pnpm run build",
"release:publish": "clean-publish",
Expand Down
15 changes: 13 additions & 2 deletions rules/sort-classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ type Group =
| 'property'
| 'unknown'
| 'method'
| string

type Options = [
Partial<{
'custom-groups': { [key: string]: string[] | string }
groups: (Group[] | Group)[]
'ignore-case': boolean
order: SortOrder
Expand All @@ -61,6 +63,9 @@ export default createEslintRule<Options, MESSAGE_ID>({
{
type: 'object',
properties: {
'custom-groups': {
type: 'object',
},
type: {
enum: [
SortType.alphabetical,
Expand Down Expand Up @@ -105,11 +110,14 @@ export default createEslintRule<Options, MESSAGE_ID>({
order: SortOrder.asc,
'ignore-case': false,
groups: ['property', 'constructor', 'method', 'unknown'],
'custom-groups': {},
})

let nodes: SortingNode[] = node.body.map(member => {
let name: string
let { getGroup, defineGroup } = useGroups(options.groups)
let { getGroup, defineGroup, setCustomGroups } = useGroups(
options.groups,
)

if (member.type === 'StaticBlock') {
name = 'static'
Expand All @@ -127,7 +135,6 @@ export default createEslintRule<Options, MESSAGE_ID>({
}

let isPrivate = name.startsWith('_') || name.startsWith('#')

let decorated = 'decorators' in member && member.decorators.length > 0

if (member.type === 'MethodDefinition') {
Expand Down Expand Up @@ -203,6 +210,10 @@ export default createEslintRule<Options, MESSAGE_ID>({
defineGroup('property')
}

setCustomGroups(options['custom-groups'], name, {
override: true,
})

return {
size: rangeToDiff(member.range),
group: getGroup(),
Expand Down
Loading

0 comments on commit 1773ffb

Please sign in to comment.