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(eslint-plugin): [member-ordering] add support for grouping readonly fields #6349

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
39 changes: 38 additions & 1 deletion packages/eslint-plugin/docs/rules/member-ordering.md
Expand Up @@ -58,7 +58,7 @@ The supported member attributes are, in order:

- **Accessibility** (`'public' | 'protected' | 'private' | '#private'`)
- **Decoration** (`'decorated'`): Whether the member has an explicit accessibility decorator
- **Kind** (`'call-signature' | 'constructor' | 'field' | 'get' | 'method' | 'set' | 'signature'`)
- **Kind** (`'call-signature' | 'constructor' | 'field' | 'readonly-field' | 'get' | 'method' | 'set' | 'signature'`)

Member attributes may be joined with a `'-'` to combine into more specific groups.
For example, `'public-field'` would come before `'private-field'`.
Expand Down Expand Up @@ -1017,34 +1017,56 @@ The most explicit and granular form is the following:

// Fields
"public-static-field",
"public-static-readonly-field",
"protected-static-field",
"protected-static-readonly-field",
"private-static-field",
"private-static-readonly-field",
"#private-static-field",
"#private-static-readonly-field",

"public-decorated-field",
"public-decorated-readonly-field",
"protected-decorated-field",
"protected-decorated-readonly-field",
"private-decorated-field",
"private-decorated-readonly-field",

"public-instance-field",
"public-instance-readonly-field",
"protected-instance-field",
"protected-instance-readonly-field",
"private-instance-field",
"private-instance-readonly-field",
"#private-instance-field",
"#private-instance-readonly-field",

"public-abstract-field",
"public-abstract-readonly-field",
"protected-abstract-field",
"protected-abstract-readonly-field",

"public-field",
"public-readonly-field",
"protected-field",
"protected-readonly-field",
"private-field",
"private-readonly-field"
"#private-field",
"#private-readonly-field"

"static-field",
"static-readonly-field",
"instance-field",
"instance-readonly-field"
"abstract-field",
"abstract-readonly-field",

"decorated-field",
"decorated-readonly-field",

"field",
"readonly-field",

// Static initialization
"static-initialization",
Expand Down Expand Up @@ -1290,6 +1312,21 @@ The third grouping option is to ignore both scope and accessibility.
]
```

### Member Group Types (Readonly Fields)

It is possible to group fields by their `readonly` modifiers.

```jsonc
[
// Index signature
// No grouping for index signature.
IronGeek marked this conversation as resolved.
Show resolved Hide resolved

// Fields
"readonly-field", // = ["public-static-readonly-field", "protected-static-readonly-field", "private-static-readonly-field", "public-instance-readonly-field", "protected-instance-readonly-field", "private-instance-readonly-field", "public-abstract-readonly-field", "protected-abstract-readonly-field"]
"field" // = ["public-static-field", "protected-static-field", "private-static-field", "public-instance-field", "protected-instance-field", "private-instance-field", "public-abstract-field", "protected-abstract-field"]
]
```

### Grouping Different Member Types at the Same Rank

It is also possible to group different member types at the same rank.
Expand Down
72 changes: 63 additions & 9 deletions packages/eslint-plugin/src/rules/member-ordering.ts
Expand Up @@ -9,17 +9,20 @@ export type MessageIds =
| 'incorrectOrder'
| 'incorrectRequiredMembersOrder';

type ReadonlyType = 'readonly-field';

type MemberKind =
| 'call-signature'
| 'constructor'
| ReadonlyType
| 'field'
| 'get'
| 'method'
| 'set'
| 'signature'
| 'static-initialization';

type DecoratedMemberKind = 'field' | 'method' | 'get' | 'set';
type DecoratedMemberKind = ReadonlyType | 'field' | 'method' | 'get' | 'set';

type NonCallableMemberKind = Exclude<MemberKind, 'constructor' | 'signature'>;

Expand Down Expand Up @@ -259,6 +262,7 @@ const allMemberTypes = Array.from(
(
[
'signature',
'readonly-field',
'field',
'method',
'call-signature',
Expand All @@ -284,7 +288,8 @@ const allMemberTypes = Array.from(
// Only class instance fields, methods, get and set can have decorators attached to them
if (
accessibility !== '#private' &&
(type === 'field' ||
(type === 'readonly-field' ||
type === 'field' ||
type === 'method' ||
type === 'get' ||
type === 'set')
Expand Down Expand Up @@ -329,6 +334,8 @@ const functionExpressions = [
* @param node the node to be evaluated.
*/
function getNodeType(node: Member): MemberKind | null {
const readonly = 'readonly' in node && node.readonly === true;
IronGeek marked this conversation as resolved.
Show resolved Hide resolved

switch (node.type) {
case AST_NODE_TYPES.TSAbstractMethodDefinition:
case AST_NODE_TYPES.MethodDefinition:
Expand All @@ -340,13 +347,15 @@ function getNodeType(node: Member): MemberKind | null {
case AST_NODE_TYPES.TSConstructSignatureDeclaration:
return 'constructor';
case AST_NODE_TYPES.TSAbstractPropertyDefinition:
return 'field';
return readonly ? 'readonly-field' : 'field';
IronGeek marked this conversation as resolved.
Show resolved Hide resolved
case AST_NODE_TYPES.PropertyDefinition:
return node.value && functionExpressions.includes(node.value.type)
? 'method'
: readonly
? 'readonly-field'
: 'field';
case AST_NODE_TYPES.TSPropertySignature:
return 'field';
return readonly ? 'readonly-field' : 'field';
case AST_NODE_TYPES.TSIndexSignature:
return 'signature';
case AST_NODE_TYPES.StaticBlock:
Expand Down Expand Up @@ -514,27 +523,48 @@ function getRank(
const decorated = 'decorators' in node && node.decorators!.length > 0;
if (
decorated &&
(type === 'field' ||
(type === 'readonly-field' ||
type === 'field' ||
type === 'method' ||
type === 'get' ||
type === 'set')
) {
memberGroups.push(`${accessibility}-decorated-${type}`);
memberGroups.push(`decorated-${type}`);

if (type === 'readonly-field') {
// if its readonly-field, push also its superset type
JoshuaKGoldberg marked this conversation as resolved.
Show resolved Hide resolved
memberGroups.push(`${accessibility}-decorated-field`);
memberGroups.push(`decorated-field`);
}
}

if (type !== 'signature' && type !== 'static-initialization') {
if (type !== 'constructor') {
// Constructors have no scope
memberGroups.push(`${accessibility}-${scope}-${type}`);
memberGroups.push(`${scope}-${type}`);

if (type === 'readonly-field') {
// if its readonly-field, push also its superset type
memberGroups.push(`${accessibility}-${scope}-field`);
memberGroups.push(`${scope}-field`);
}
}

memberGroups.push(`${accessibility}-${type}`);
if (type === 'readonly-field') {
// if its readonly-field, push also its superset type
memberGroups.push(`${accessibility}-field`);
}
}
}

memberGroups.push(type);
if (type === 'readonly-field') {
// if its readonly-field, push also its superset type
memberGroups.push('field');
}

// ...then get the rank order for those member groups based on the node
return getRankOrder(memberGroups, orderConfig);
Expand Down Expand Up @@ -621,15 +651,39 @@ export default util.createRule<Options, MessageIds>({
interfaces: {
oneOf: [
neverConfig,
arrayConfig(['signature', 'field', 'method', 'constructor']),
objectConfig(['signature', 'field', 'method', 'constructor']),
arrayConfig([
'signature',
'readonly-field',
'field',
'method',
'constructor',
]),
objectConfig([
'signature',
'readonly-field',
'field',
'method',
'constructor',
]),
],
},
typeLiterals: {
oneOf: [
neverConfig,
arrayConfig(['signature', 'field', 'method', 'constructor']),
objectConfig(['signature', 'field', 'method', 'constructor']),
arrayConfig([
'signature',
'readonly-field',
'field',
'method',
'constructor',
]),
objectConfig([
'signature',
'readonly-field',
'field',
'method',
'constructor',
]),
],
},
},
Expand Down