Skip to content

Commit

Permalink
feat(sort-classes): add decorators support
Browse files Browse the repository at this point in the history
  • Loading branch information
chirokas authored Mar 5, 2024
1 parent 96f3dd8 commit cbe3f4b
Show file tree
Hide file tree
Showing 3 changed files with 728 additions and 0 deletions.
7 changes: 7 additions & 0 deletions docs/rules/sort-classes.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,14 @@ This rule accepts an options object with the following properties:

```ts
type Group =
| 'private-decorated-accessor-property'
| 'decorated-accessor-property'
| 'private-decorated-property'
| 'static-private-method'
| 'decorated-set-method'
| 'decorated-get-method'
| 'decorated-property'
| 'decorated-method'
| 'private-property'
| 'static-property'
| 'index-signature'
Expand Down
37 changes: 37 additions & 0 deletions rules/sort-classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ import { compare } from '../utils/compare'
type MESSAGE_ID = 'unexpectedClassesOrder'

type Group =
| 'private-decorated-accessor-property'
| 'decorated-accessor-property'
| 'private-decorated-property'
| 'static-private-method'
| 'decorated-set-method'
| 'decorated-get-method'
| 'decorated-property'
| 'decorated-method'
| 'private-property'
| 'static-property'
| 'index-signature'
Expand Down Expand Up @@ -121,6 +128,8 @@ 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') {
if (member.kind === 'constructor') {
defineGroup('constructor')
Expand All @@ -131,6 +140,18 @@ export default createEslintRule<Options, MESSAGE_ID>({

let isStaticMethod = member.static

if (decorated) {
if (member.kind === 'get') {
defineGroup('decorated-get-method')
}

if (member.kind === 'set') {
defineGroup('decorated-set-method')
}

defineGroup('decorated-method')
}

if (isPrivateMethod && isStaticMethod) {
defineGroup('static-private-method')
}
Expand All @@ -154,7 +175,23 @@ export default createEslintRule<Options, MESSAGE_ID>({
defineGroup('method')
} else if (member.type === 'TSIndexSignature') {
defineGroup('index-signature')
} else if (member.type === 'AccessorProperty') {
if (decorated) {
if (member.accessibility === 'private' || isPrivate) {
defineGroup('private-decorated-accessor-property')
}

defineGroup('decorated-accessor-property')
}
} else if (member.type === 'PropertyDefinition') {
if (decorated) {
if (member.accessibility === 'private' || isPrivate) {
defineGroup('private-decorated-property')
}

defineGroup('decorated-property')
}

if (member.accessibility === 'private' || isPrivate) {
defineGroup('private-property')
}
Expand Down
Loading

0 comments on commit cbe3f4b

Please sign in to comment.