Skip to content

Commit

Permalink
feat: support for ordering protected properties and methods in classes
Browse files Browse the repository at this point in the history
  • Loading branch information
williamkolean authored Jul 31, 2024
1 parent bf730c4 commit 7efadfa
Show file tree
Hide file tree
Showing 3 changed files with 192 additions and 44 deletions.
36 changes: 36 additions & 0 deletions docs/content/rules/sort-classes.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,12 @@ Allows you to use comments to separate the class members into logical groups. Th
[
'index-signature',
'static-property',
'protected-property',
'private-property',
'property',
'constructor',
'static-method',
'protected-method',
'private-method',
'method',
['get-method', 'set-method'],
Expand All @@ -218,15 +220,20 @@ There are a lot of predefined groups.
Predefined Groups:

- `'index-signature'` — Index signatures, which define the types of keys and values in an object.
- `'protected-decorated-accessor-property'` — Protected accessor properties with decorators.
- `'private-decorated-accessor-property'` — Private accessor properties with decorators.
- `'decorated-accessor-property'` — Accessor properties with decorators.
- `'protected-decorated-property'` — Protected properties with decorators.
- `'private-decorated-property'` — Private properties with decorators.
- `'decorated-property'` — Properties with decorators.
- `'protected-property'` — Protected properties.
- `'private-property'` — Private properties.
- `'static-property'` — Static properties.
- `'property'` — Regular properties.
- `'constructor'` — Constructor method.
- `'protected-method'` — Protected methods.
- `'private-method'` — Private methods.
- `'static-protected-method'` — Static protected methods.
- `'static-private-method'` — Static private methods.
- `'static-method'` — Static methods.
- `'decorated-method'` — Methods with decorators.
Expand All @@ -244,6 +251,12 @@ class Example {
// 'index-signature'
[key: string]: any;

// 'protected-decorated-accessor-property'
@SomeDecorator
protected get accessor() {
return this._value;
}

// 'private-decorated-accessor-property'
@SomeDecorator
private get accessor() {
Expand All @@ -256,6 +269,10 @@ class Example {
return this._value;
}

// 'protected-decorated-property'
@SomeDecorator
protected _value: number;

// 'private-decorated-property'
@SomeDecorator
private _value: number;
Expand All @@ -264,6 +281,9 @@ class Example {
@SomeDecorator
public value: number;

// 'protected-property'
protected name: string;

// 'private-property'
private name: string;

Expand All @@ -278,11 +298,21 @@ class Example {
this._value = value;
}

// 'protected-method'
protected calculate() {
return this._value * 2;
}

// 'private-method'
private calculate() {
return this._value * 2;
}

// 'static-protected-method'
protected static initialize() {
this.instance = new Example(0);
}

// 'static-private-method'
private static initialize() {
this.instance = new Example(0);
Expand Down Expand Up @@ -341,10 +371,12 @@ Example:
groups: [
'index-signature',
'static-property',
'protected-property',
'private-property',
'property',
'constructor',
'static-method',
'protected-method',
'private-method',
'static-private-method',
'method',
Expand Down Expand Up @@ -383,10 +415,12 @@ Example:
groups: [
'index-signature',
'static-property',
'protected-property',
'private-property',
'property',
'constructor',
'static-method',
'protected-method',
'private-method',
'method',
['get-method', 'set-method'],
Expand Down Expand Up @@ -420,10 +454,12 @@ Example:
groups: [
'index-signature',
'static-property',
'protected-property',
'private-property',
'property',
'constructor',
'static-method',
'protected-method',
'private-method',
'method',
['get-method', 'set-method'],
Expand Down
31 changes: 31 additions & 0 deletions rules/sort-classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,20 @@ import { compare } from '../utils/compare'
type MESSAGE_ID = 'unexpectedClassesOrder'

type Group =
| 'protected-decorated-accessor-property'
| 'private-decorated-accessor-property'
| 'protected-decorated-property'
| 'decorated-accessor-property'
| 'private-decorated-property'
| 'static-protected-method'
| 'static-private-method'
| 'decorated-set-method'
| 'decorated-get-method'
| 'decorated-property'
| 'protected-property'
| 'decorated-method'
| 'private-property'
| 'protected-method'
| 'static-property'
| 'index-signature'
| 'private-method'
Expand Down Expand Up @@ -150,10 +155,12 @@ export default createEslintRule<Options, MESSAGE_ID>({
groups: [
'index-signature',
'static-property',
'protected-property',
'private-property',
'property',
'constructor',
'static-method',
'protected-method',
'private-method',
'method',
['get-method', 'set-method'],
Expand All @@ -169,10 +176,12 @@ export default createEslintRule<Options, MESSAGE_ID>({
groups: [
'index-signature',
'static-property',
'protected-property',
'private-property',
'property',
'constructor',
'static-method',
'protected-method',
'private-method',
'method',
['get-method', 'set-method'],
Expand Down Expand Up @@ -278,6 +287,8 @@ export default createEslintRule<Options, MESSAGE_ID>({
defineGroup('constructor')
}

let isProtectedMethod = member.accessibility === 'protected'

let isPrivateMethod =
member.accessibility === 'private' || isPrivate

Expand Down Expand Up @@ -307,6 +318,14 @@ export default createEslintRule<Options, MESSAGE_ID>({
defineGroup('static-method')
}

if (isProtectedMethod && isStaticMethod) {
defineGroup('static-protected-method')
}

if (isProtectedMethod) {
defineGroup('protected-method')
}

if (member.kind === 'get') {
defineGroup('get-method')
}
Expand All @@ -320,6 +339,10 @@ export default createEslintRule<Options, MESSAGE_ID>({
defineGroup('index-signature')
} else if (member.type === 'AccessorProperty') {
if (decorated) {
if (member.accessibility === 'protected') {
defineGroup('protected-decorated-accessor-property')
}

if (member.accessibility === 'private' || isPrivate) {
defineGroup('private-decorated-accessor-property')
}
Expand All @@ -328,13 +351,21 @@ export default createEslintRule<Options, MESSAGE_ID>({
}
} else if (member.type === 'PropertyDefinition') {
if (decorated) {
if (member.accessibility === 'protected') {
defineGroup('protected-decorated-property')
}

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

defineGroup('decorated-property')
}

if (member.accessibility === 'protected') {
defineGroup('protected-property')
}

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

0 comments on commit 7efadfa

Please sign in to comment.