Skip to content

Commit 1773ffb

Browse files
feat(sort-classes): add custom-group to sort-classes rule
1 parent 861a381 commit 1773ffb

File tree

5 files changed

+154
-90
lines changed

5 files changed

+154
-90
lines changed

docs/rules/sort-classes.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ interface Options {
139139
order?: 'asc' | 'desc'
140140
'ignore-case'?: boolean
141141
groups?: (Group | Group[])[]
142+
'custom-groups'?: { [key: string]: string[] | string }
142143
}
143144
```
144145

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

193+
### custom-groups
194+
195+
<sub>(default: `{}`)</sub>
196+
197+
You can define your own groups for object keys. The [minimatch](https://github.com/isaacs/minimatch) library is used for pattern matching.
198+
199+
Example:
200+
201+
```
202+
{
203+
"custom-groups": {
204+
"top": "id"
205+
}
206+
}
207+
```
208+
192209
## ⚙️ Usage
193210

194211
::: code-group

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"docs:dev": "vitepress dev docs",
1717
"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",
1818
"docs:build": "vitepress build docs",
19+
"format:fix": "prettier --write \"**/*.{js,ts,json,md,yml}\"",
1920
"release": "pnpm release:check && pnpm release:version && pnpm release:publish",
2021
"release:check": "pnpm test && pnpm run build",
2122
"release:publish": "clean-publish",

rules/sort-classes.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,11 @@ type Group =
3737
| 'property'
3838
| 'unknown'
3939
| 'method'
40+
| string
4041

4142
type Options = [
4243
Partial<{
44+
'custom-groups': { [key: string]: string[] | string }
4345
groups: (Group[] | Group)[]
4446
'ignore-case': boolean
4547
order: SortOrder
@@ -61,6 +63,9 @@ export default createEslintRule<Options, MESSAGE_ID>({
6163
{
6264
type: 'object',
6365
properties: {
66+
'custom-groups': {
67+
type: 'object',
68+
},
6469
type: {
6570
enum: [
6671
SortType.alphabetical,
@@ -105,11 +110,14 @@ export default createEslintRule<Options, MESSAGE_ID>({
105110
order: SortOrder.asc,
106111
'ignore-case': false,
107112
groups: ['property', 'constructor', 'method', 'unknown'],
113+
'custom-groups': {},
108114
})
109115

110116
let nodes: SortingNode[] = node.body.map(member => {
111117
let name: string
112-
let { getGroup, defineGroup } = useGroups(options.groups)
118+
let { getGroup, defineGroup, setCustomGroups } = useGroups(
119+
options.groups,
120+
)
113121

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

129137
let isPrivate = name.startsWith('_') || name.startsWith('#')
130-
131138
let decorated = 'decorators' in member && member.decorators.length > 0
132139

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

213+
setCustomGroups(options['custom-groups'], name, {
214+
override: true,
215+
})
216+
206217
return {
207218
size: rangeToDiff(member.range),
208219
group: getGroup(),

0 commit comments

Comments
 (0)