Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [member-ordering] add accessor support for member-ordering #7927

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
45 changes: 41 additions & 4 deletions packages/eslint-plugin/src/rules/member-ordering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type ReadonlyType = 'readonly-field' | 'readonly-signature';

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

type DecoratedMemberKind =
| Exclude<ReadonlyType, 'readonly-signature'>
| 'accessor'
| 'field'
| 'get'
| 'method'
Expand Down Expand Up @@ -169,6 +171,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 @@ -273,6 +306,7 @@ const allMemberTypes = Array.from(
'method',
'call-signature',
'constructor',
'accessor',
'get',
'set',
'static-initialization',
Expand All @@ -292,12 +326,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 @@ -354,6 +389,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 Expand Up @@ -727,7 +764,7 @@ export default createRule<Options, MessageIds>({
// Find first member which isn't correctly sorted
members.forEach(member => {
const rank = getRank(member, groupOrder, supportsModifiers);
const name = getMemberName(member, context.getSourceCode());
const name = getMemberName(member, context.sourceCode);
const rankLastMember = previousRanks[previousRanks.length - 1];

if (rank === -1) {
Expand Down Expand Up @@ -776,7 +813,7 @@ export default createRule<Options, MessageIds>({

// Find first member which isn't correctly sorted
members.forEach(member => {
const name = getMemberName(member, context.getSourceCode());
const name = getMemberName(member, context.sourceCode);

// Note: Not all members have names
if (name) {
Expand Down Expand Up @@ -846,7 +883,7 @@ export default createRule<Options, MessageIds>({
messageId: 'incorrectRequiredMembersOrder',
loc: member.loc,
data: {
member: getMemberName(member, context.getSourceCode()),
member: getMemberName(member, context.sourceCode),
optionalOrRequired:
optionalityOrder === 'required-first' ? 'required' : 'optional',
},
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