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 committed Mar 3, 2024
1 parent 96f3dd8 commit a267c42
Show file tree
Hide file tree
Showing 3 changed files with 729 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,6 +114,13 @@ This rule accepts an options object with the following properties:

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

type Group =
| 'decorated-accessor-property'
| 'decorated-method'
| 'decorated-property'
| 'decorated-set-method'
| 'decorated-get-method'
| 'private-decorated-accessor-property'
| 'private-decorated-property'
| 'static-private-method'
| 'private-property'
| 'static-property'
Expand Down Expand Up @@ -121,6 +128,9 @@ export default createEslintRule<Options, MESSAGE_ID>({

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

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

if (member.type === 'MethodDefinition') {
if (member.kind === 'constructor') {
defineGroup('constructor')
Expand All @@ -131,6 +141,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 +176,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 a267c42

Please sign in to comment.