Skip to content

Commit

Permalink
feat: add static-block and accessor-property to default groups in sor…
Browse files Browse the repository at this point in the history
…t-classes
  • Loading branch information
hugop95 committed Aug 16, 2024
1 parent f1f875e commit 0c724e0
Show file tree
Hide file tree
Showing 3 changed files with 236 additions and 44 deletions.
80 changes: 42 additions & 38 deletions docs/content/rules/sort-classes.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,12 @@ Allows you to use comments to separate the class members into logical groups. Th
default:
```
[
'static-block',
'index-signature',
'static-property',
'protected-property',
'private-property',
'property',
['protected-property', 'protected-accessor-property'],
['private-property', 'private-accessor-property'],
['property', 'accessor-property'],
'constructor',
'static-method',
'protected-method',
Expand Down Expand Up @@ -301,6 +302,11 @@ Example 2: (The most important group is written in the comments)
```ts
abstract class Example extends BaseExample {

// 'static-block'
static {
console.log("I am a static block");
}

// 'index-signature'
[key: string]: any;

Expand All @@ -314,13 +320,21 @@ abstract class Example extends BaseExample {
@SomeDecorator
protected abstract override readonly _value: number;

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

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

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

// 'private-decorated-accessor-property'
@SomeDecorator
private accessor _value: number;

// private-function-property
private arrowProperty = () => {};

Expand All @@ -337,6 +351,10 @@ abstract class Example extends BaseExample {
@SomeDecorator
public value: number;

// 'public-decorated-accessor-property'
@SomeDecorator
public accessor value: number;

// 'public-constructor'
constructor(value: number) {
this._value = value;
Expand Down Expand Up @@ -417,23 +435,6 @@ abstract class Example extends BaseExample {
set value(value: number) {
this._value = value;
}

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

// 'private-decorated-accessor-property'
@SomeDecorator
private accessor _value: number;

// 'static-block'
static {
console.log("I am a static block");
}

// 'public-decorated-accessor-property'
@SomeDecorator
public accessor value: number;
}
```

Expand All @@ -448,19 +449,20 @@ Example:
```js
{
groups: [
'index-signature',
'static-property',
'protected-property',
'private-property',
'property',
'constructor',
'static-method',
'protected-method',
'private-method',
'static-block',
'index-signature',
'static-property',
['protected-property', 'protected-accessor-property'],
['private-property', 'private-accessor-property'],
['property', 'accessor-property'],
'constructor',
'static-method',
'protected-method',
'private-method',
'static-private-method',
'method',
['get-method', 'set-method'],
'unknown',
'method',
['get-method', 'set-method'],
'unknown',
+ 'value', // [!code ++]
],
+ customGroups: { // [!code ++]
Expand Down Expand Up @@ -492,11 +494,12 @@ Example:
ignoreCase: true,
partitionByComment: false,
groups: [
'static-block',
'index-signature',
'static-property',
'protected-property',
'private-property',
'property',
['protected-property', 'protected-accessor-property'],
['private-property', 'private-accessor-property'],
['property', 'accessor-property'],
'constructor',
'static-method',
'protected-method',
Expand Down Expand Up @@ -531,11 +534,12 @@ Example:
ignoreCase: true,
partitionByComment: false,
groups: [
'static-block',
'index-signature',
'static-property',
'protected-property',
'private-property',
'property',
['protected-property', 'protected-accessor-property'],
['private-property', 'private-accessor-property'],
['property', 'accessor-property'],
'constructor',
'static-method',
'protected-method',
Expand Down
14 changes: 8 additions & 6 deletions rules/sort-classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,12 @@ export default createEslintRule<Options, MESSAGE_ID>({
ignoreCase: true,
partitionByComment: false,
groups: [
'static-block',
'index-signature',
'static-property',
'protected-property',
'private-property',
'property',
['protected-property', 'protected-accessor-property'],
['private-property', 'private-accessor-property'],
['property', 'accessor-property'],
'constructor',
'static-method',
'protected-method',
Expand All @@ -251,11 +252,12 @@ export default createEslintRule<Options, MESSAGE_ID>({

let options = complete(context.options.at(0), settings, {
groups: [
'static-block',
'index-signature',
'static-property',
'protected-property',
'private-property',
'property',
['protected-property', 'protected-accessor-property'],
['private-property', 'private-accessor-property'],
['property', 'accessor-property'],
'constructor',
'static-method',
'protected-method',
Expand Down
186 changes: 186 additions & 0 deletions test/sort-classes.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4984,5 +4984,191 @@ describe(ruleName, () => {
],
},
)

ruleTester.run(`${ruleName}: sorts using default groups`, rule, {
valid: [],
invalid: [
{
code: dedent`
class Class {
set setMethod() {}
get getMethod() {}
public publicMethod() {}
private privateMethod() {}
protected protectedMethod() {}
static staticMethod() {}
constructor() {}
public accessor publicAccessorProperty
public publicProperty
private accessor privateAccessorProperty
private privateProperty
protected accessor protectedAccessorProperty
protected protectedProperty
static staticProperty
[key: string]: string
static {}
}
`,
output: dedent`
class Class {
static {}
[key: string]: string
static staticProperty
protected accessor protectedAccessorProperty
protected protectedProperty
private accessor privateAccessorProperty
private privateProperty
public accessor publicAccessorProperty
public publicProperty
constructor() {}
static staticMethod() {}
protected protectedMethod() {}
private privateMethod() {}
public publicMethod() {}
get getMethod() {}
set setMethod() {}
}
`,
errors: [
{
messageId: 'unexpectedClassesOrder',
data: {
left: 'setMethod',
right: 'getMethod',
},
},
{
messageId: 'unexpectedClassesGroupOrder',
data: {
left: 'getMethod',
leftGroup: 'get-method',
right: 'publicMethod',
rightGroup: 'method',
},
},
{
messageId: 'unexpectedClassesGroupOrder',
data: {
left: 'publicMethod',
leftGroup: 'method',
right: 'privateMethod',
rightGroup: 'private-method',
},
},
{
messageId: 'unexpectedClassesGroupOrder',
data: {
left: 'privateMethod',
leftGroup: 'private-method',
right: 'protectedMethod',
rightGroup: 'protected-method',
},
},
{
messageId: 'unexpectedClassesGroupOrder',
data: {
left: 'protectedMethod',
leftGroup: 'protected-method',
right: 'staticMethod',
rightGroup: 'static-method',
},
},
{
messageId: 'unexpectedClassesGroupOrder',
data: {
left: 'staticMethod',
leftGroup: 'static-method',
right: 'constructor',
rightGroup: 'constructor',
},
},
{
messageId: 'unexpectedClassesGroupOrder',
data: {
left: 'constructor',
leftGroup: 'constructor',
right: 'publicAccessorProperty',
rightGroup: 'accessor-property',
},
},
{
messageId: 'unexpectedClassesGroupOrder',
data: {
left: 'publicProperty',
leftGroup: 'property',
right: 'privateAccessorProperty',
rightGroup: 'private-accessor-property',
},
},
{
messageId: 'unexpectedClassesGroupOrder',
data: {
left: 'privateProperty',
leftGroup: 'private-property',
right: 'protectedAccessorProperty',
rightGroup: 'protected-accessor-property',
},
},
{
messageId: 'unexpectedClassesGroupOrder',
data: {
left: 'protectedProperty',
leftGroup: 'protected-property',
right: 'staticProperty',
rightGroup: 'static-property',
},
},
{
messageId: 'unexpectedClassesGroupOrder',
data: {
left: 'staticProperty',
leftGroup: 'static-property',
right: '[key: string]',
rightGroup: 'index-signature',
},
},
{
messageId: 'unexpectedClassesGroupOrder',
data: {
left: '[key: string]',
leftGroup: 'index-signature',
right: 'static',
rightGroup: 'static-block',
},
},
],
},
],
})
})
})

0 comments on commit 0c724e0

Please sign in to comment.