Skip to content

Commit 0c724e0

Browse files
authored
feat: add static-block and accessor-property to default groups in sort-classes
1 parent f1f875e commit 0c724e0

File tree

3 files changed

+236
-44
lines changed

3 files changed

+236
-44
lines changed

docs/content/rules/sort-classes.mdx

Lines changed: 42 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,12 @@ Allows you to use comments to separate the class members into logical groups. Th
197197
default:
198198
```
199199
[
200+
'static-block',
200201
'index-signature',
201202
'static-property',
202-
'protected-property',
203-
'private-property',
204-
'property',
203+
['protected-property', 'protected-accessor-property'],
204+
['private-property', 'private-accessor-property'],
205+
['property', 'accessor-property'],
205206
'constructor',
206207
'static-method',
207208
'protected-method',
@@ -301,6 +302,11 @@ Example 2: (The most important group is written in the comments)
301302
```ts
302303
abstract class Example extends BaseExample {
303304

305+
// 'static-block'
306+
static {
307+
console.log("I am a static block");
308+
}
309+
304310
// 'index-signature'
305311
[key: string]: any;
306312

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

323+
// 'protected-decorated-accessor-property'
324+
@SomeDecorator
325+
protected accessor _value: number;
326+
317327
// 'protected-property'
318328
protected name: string;
319329

320330
// 'private-decorated-property'
321331
@SomeDecorator
322332
private _value: number;
323333

334+
// 'private-decorated-accessor-property'
335+
@SomeDecorator
336+
private accessor _value: number;
337+
324338
// private-function-property
325339
private arrowProperty = () => {};
326340

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

354+
// 'public-decorated-accessor-property'
355+
@SomeDecorator
356+
public accessor value: number;
357+
340358
// 'public-constructor'
341359
constructor(value: number) {
342360
this._value = value;
@@ -417,23 +435,6 @@ abstract class Example extends BaseExample {
417435
set value(value: number) {
418436
this._value = value;
419437
}
420-
421-
// 'protected-decorated-accessor-property'
422-
@SomeDecorator
423-
protected accessor _value: number;
424-
425-
// 'private-decorated-accessor-property'
426-
@SomeDecorator
427-
private accessor _value: number;
428-
429-
// 'static-block'
430-
static {
431-
console.log("I am a static block");
432-
}
433-
434-
// 'public-decorated-accessor-property'
435-
@SomeDecorator
436-
public accessor value: number;
437438
}
438439
```
439440

@@ -448,19 +449,20 @@ Example:
448449
```js
449450
{
450451
groups: [
451-
'index-signature',
452-
'static-property',
453-
'protected-property',
454-
'private-property',
455-
'property',
456-
'constructor',
457-
'static-method',
458-
'protected-method',
459-
'private-method',
452+
'static-block',
453+
'index-signature',
454+
'static-property',
455+
['protected-property', 'protected-accessor-property'],
456+
['private-property', 'private-accessor-property'],
457+
['property', 'accessor-property'],
458+
'constructor',
459+
'static-method',
460+
'protected-method',
461+
'private-method',
460462
'static-private-method',
461-
'method',
462-
['get-method', 'set-method'],
463-
'unknown',
463+
'method',
464+
['get-method', 'set-method'],
465+
'unknown',
464466
+ 'value', // [!code ++]
465467
],
466468
+ customGroups: { // [!code ++]
@@ -492,11 +494,12 @@ Example:
492494
ignoreCase: true,
493495
partitionByComment: false,
494496
groups: [
497+
'static-block',
495498
'index-signature',
496499
'static-property',
497-
'protected-property',
498-
'private-property',
499-
'property',
500+
['protected-property', 'protected-accessor-property'],
501+
['private-property', 'private-accessor-property'],
502+
['property', 'accessor-property'],
500503
'constructor',
501504
'static-method',
502505
'protected-method',
@@ -531,11 +534,12 @@ Example:
531534
ignoreCase: true,
532535
partitionByComment: false,
533536
groups: [
537+
'static-block',
534538
'index-signature',
535539
'static-property',
536-
'protected-property',
537-
'private-property',
538-
'property',
540+
['protected-property', 'protected-accessor-property'],
541+
['private-property', 'private-accessor-property'],
542+
['property', 'accessor-property'],
539543
'constructor',
540544
'static-method',
541545
'protected-method',

rules/sort-classes.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,12 @@ export default createEslintRule<Options, MESSAGE_ID>({
228228
ignoreCase: true,
229229
partitionByComment: false,
230230
groups: [
231+
'static-block',
231232
'index-signature',
232233
'static-property',
233-
'protected-property',
234-
'private-property',
235-
'property',
234+
['protected-property', 'protected-accessor-property'],
235+
['private-property', 'private-accessor-property'],
236+
['property', 'accessor-property'],
236237
'constructor',
237238
'static-method',
238239
'protected-method',
@@ -251,11 +252,12 @@ export default createEslintRule<Options, MESSAGE_ID>({
251252

252253
let options = complete(context.options.at(0), settings, {
253254
groups: [
255+
'static-block',
254256
'index-signature',
255257
'static-property',
256-
'protected-property',
257-
'private-property',
258-
'property',
258+
['protected-property', 'protected-accessor-property'],
259+
['private-property', 'private-accessor-property'],
260+
['property', 'accessor-property'],
259261
'constructor',
260262
'static-method',
261263
'protected-method',

test/sort-classes.test.ts

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4984,5 +4984,191 @@ describe(ruleName, () => {
49844984
],
49854985
},
49864986
)
4987+
4988+
ruleTester.run(`${ruleName}: sorts using default groups`, rule, {
4989+
valid: [],
4990+
invalid: [
4991+
{
4992+
code: dedent`
4993+
class Class {
4994+
set setMethod() {}
4995+
4996+
get getMethod() {}
4997+
4998+
public publicMethod() {}
4999+
5000+
private privateMethod() {}
5001+
5002+
protected protectedMethod() {}
5003+
5004+
static staticMethod() {}
5005+
5006+
constructor() {}
5007+
5008+
public accessor publicAccessorProperty
5009+
5010+
public publicProperty
5011+
5012+
private accessor privateAccessorProperty
5013+
5014+
private privateProperty
5015+
5016+
protected accessor protectedAccessorProperty
5017+
5018+
protected protectedProperty
5019+
5020+
static staticProperty
5021+
5022+
[key: string]: string
5023+
5024+
static {}
5025+
}
5026+
`,
5027+
output: dedent`
5028+
class Class {
5029+
static {}
5030+
5031+
[key: string]: string
5032+
5033+
static staticProperty
5034+
5035+
protected accessor protectedAccessorProperty
5036+
5037+
protected protectedProperty
5038+
5039+
private accessor privateAccessorProperty
5040+
5041+
private privateProperty
5042+
5043+
public accessor publicAccessorProperty
5044+
5045+
public publicProperty
5046+
5047+
constructor() {}
5048+
5049+
static staticMethod() {}
5050+
5051+
protected protectedMethod() {}
5052+
5053+
private privateMethod() {}
5054+
5055+
public publicMethod() {}
5056+
5057+
get getMethod() {}
5058+
5059+
set setMethod() {}
5060+
}
5061+
`,
5062+
errors: [
5063+
{
5064+
messageId: 'unexpectedClassesOrder',
5065+
data: {
5066+
left: 'setMethod',
5067+
right: 'getMethod',
5068+
},
5069+
},
5070+
{
5071+
messageId: 'unexpectedClassesGroupOrder',
5072+
data: {
5073+
left: 'getMethod',
5074+
leftGroup: 'get-method',
5075+
right: 'publicMethod',
5076+
rightGroup: 'method',
5077+
},
5078+
},
5079+
{
5080+
messageId: 'unexpectedClassesGroupOrder',
5081+
data: {
5082+
left: 'publicMethod',
5083+
leftGroup: 'method',
5084+
right: 'privateMethod',
5085+
rightGroup: 'private-method',
5086+
},
5087+
},
5088+
{
5089+
messageId: 'unexpectedClassesGroupOrder',
5090+
data: {
5091+
left: 'privateMethod',
5092+
leftGroup: 'private-method',
5093+
right: 'protectedMethod',
5094+
rightGroup: 'protected-method',
5095+
},
5096+
},
5097+
{
5098+
messageId: 'unexpectedClassesGroupOrder',
5099+
data: {
5100+
left: 'protectedMethod',
5101+
leftGroup: 'protected-method',
5102+
right: 'staticMethod',
5103+
rightGroup: 'static-method',
5104+
},
5105+
},
5106+
{
5107+
messageId: 'unexpectedClassesGroupOrder',
5108+
data: {
5109+
left: 'staticMethod',
5110+
leftGroup: 'static-method',
5111+
right: 'constructor',
5112+
rightGroup: 'constructor',
5113+
},
5114+
},
5115+
{
5116+
messageId: 'unexpectedClassesGroupOrder',
5117+
data: {
5118+
left: 'constructor',
5119+
leftGroup: 'constructor',
5120+
right: 'publicAccessorProperty',
5121+
rightGroup: 'accessor-property',
5122+
},
5123+
},
5124+
{
5125+
messageId: 'unexpectedClassesGroupOrder',
5126+
data: {
5127+
left: 'publicProperty',
5128+
leftGroup: 'property',
5129+
right: 'privateAccessorProperty',
5130+
rightGroup: 'private-accessor-property',
5131+
},
5132+
},
5133+
{
5134+
messageId: 'unexpectedClassesGroupOrder',
5135+
data: {
5136+
left: 'privateProperty',
5137+
leftGroup: 'private-property',
5138+
right: 'protectedAccessorProperty',
5139+
rightGroup: 'protected-accessor-property',
5140+
},
5141+
},
5142+
{
5143+
messageId: 'unexpectedClassesGroupOrder',
5144+
data: {
5145+
left: 'protectedProperty',
5146+
leftGroup: 'protected-property',
5147+
right: 'staticProperty',
5148+
rightGroup: 'static-property',
5149+
},
5150+
},
5151+
{
5152+
messageId: 'unexpectedClassesGroupOrder',
5153+
data: {
5154+
left: 'staticProperty',
5155+
leftGroup: 'static-property',
5156+
right: '[key: string]',
5157+
rightGroup: 'index-signature',
5158+
},
5159+
},
5160+
{
5161+
messageId: 'unexpectedClassesGroupOrder',
5162+
data: {
5163+
left: '[key: string]',
5164+
leftGroup: 'index-signature',
5165+
right: 'static',
5166+
rightGroup: 'static-block',
5167+
},
5168+
},
5169+
],
5170+
},
5171+
],
5172+
})
49875173
})
49885174
})

0 commit comments

Comments
 (0)