Skip to content

Commit

Permalink
feat: [member-ordering] add accessor support for member-ordering (#7927)
Browse files Browse the repository at this point in the history
* Add accessor support for member-ordering

* Add test and docs

---------

Co-authored-by: Josh Goldberg <git@joshuakgoldberg.com>
  • Loading branch information
vilicvane and JoshuaKGoldberg authored Nov 14, 2023
1 parent 7e353cb commit 3c8312d
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 1 deletion.
31 changes: 31 additions & 0 deletions packages/eslint-plugin/docs/rules/member-ordering.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,37 @@ The default configuration looks as follows:

"constructor",

// Accessors
"public-static-accessor",
"protected-static-accessor",
"private-static-accessor",
"#private-static-accessor",

"public-decorated-accessor",
"protected-decorated-accessor",
"private-decorated-accessor",

"public-instance-accessor",
"protected-instance-accessor",
"private-instance-accessor",
"#private-instance-accessor",

"public-abstract-accessor",
"protected-abstract-accessor",

"public-accessor",
"protected-accessor",
"private-accessor",
"#private-accessor",

"static-accessor",
"instance-accessor",
"abstract-accessor",

"decorated-accessor",

"accessor",

// Getters
"public-static-get",
"protected-static-get",
Expand Down
39 changes: 38 additions & 1 deletion packages/eslint-plugin/src/rules/member-ordering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type ReadonlyType = 'readonly-field' | 'readonly-signature';

type MemberKind =
| ReadonlyType
| 'accessor'
| 'call-signature'
| 'constructor'
| 'field'
Expand All @@ -30,6 +31,7 @@ type MemberKind =

type DecoratedMemberKind =
| Exclude<ReadonlyType, 'readonly-signature'>
| 'accessor'
| 'field'
| 'get'
| 'method'
Expand Down Expand Up @@ -170,6 +172,37 @@ export const defaultOrder: MemberType[] = [

'constructor',

// Accessors
'public-static-accessor',
'protected-static-accessor',
'private-static-accessor',
'#private-static-accessor',

'public-decorated-accessor',
'protected-decorated-accessor',
'private-decorated-accessor',

'public-instance-accessor',
'protected-instance-accessor',
'private-instance-accessor',
'#private-instance-accessor',

'public-abstract-accessor',
'protected-abstract-accessor',

'public-accessor',
'protected-accessor',
'private-accessor',
'#private-accessor',

'static-accessor',
'instance-accessor',
'abstract-accessor',

'decorated-accessor',

'accessor',

// Getters
'public-static-get',
'protected-static-get',
Expand Down Expand Up @@ -274,6 +307,7 @@ const allMemberTypes = Array.from(
'method',
'call-signature',
'constructor',
'accessor',
'get',
'set',
'static-initialization',
Expand All @@ -293,12 +327,13 @@ const allMemberTypes = Array.from(
all.add(`${accessibility}-${type}`); // e.g. `public-field`
}

// Only class instance fields, methods, get and set can have decorators attached to them
// Only class instance fields, methods, accessors, get and set can have decorators attached to them
if (
accessibility !== '#private' &&
(type === 'readonly-field' ||
type === 'field' ||
type === 'method' ||
type === 'accessor' ||
type === 'get' ||
type === 'set')
) {
Expand Down Expand Up @@ -355,6 +390,8 @@ function getNodeType(node: Member): MemberKind | null {
return 'constructor';
case AST_NODE_TYPES.TSAbstractPropertyDefinition:
return node.readonly ? 'readonly-field' : 'field';
case AST_NODE_TYPES.AccessorProperty:
return 'accessor';
case AST_NODE_TYPES.PropertyDefinition:
return node.value && functionExpressions.includes(node.value.type)
? 'method'
Expand Down
39 changes: 39 additions & 0 deletions packages/eslint-plugin/tests/rules/member-ordering.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2055,6 +2055,20 @@ class Foo {
},
],
},
{
code: `
class Foo {
accessor bar;
baz() {}
}
`,
options: [
{
default: ['accessor', 'method'],
},
],
},
],
invalid: [
{
Expand Down Expand Up @@ -5065,6 +5079,31 @@ interface Foo {
},
],
},
{
code: `
class Foo {
accessor bar;
baz() {}
}
`,
options: [
{
default: ['method', 'accessor'],
},
],
errors: [
{
messageId: 'incorrectGroupOrder',
data: {
name: 'baz',
rank: 'accessor',
},
line: 5,
column: 3,
},
],
},
],
};

Expand Down
Loading

0 comments on commit 3c8312d

Please sign in to comment.