Skip to content

fix(eslint-plugin): [member-ordering] get accessor member name & take into account abstract and decorator #9775

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

Merged
merged 6 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions packages/eslint-plugin/src/rules/member-ordering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,9 @@ function getNodeType(node: Member): MemberKind | null {
case AST_NODE_TYPES.TSConstructSignatureDeclaration:
return 'constructor';
case AST_NODE_TYPES.TSAbstractPropertyDefinition:
case AST_NODE_TYPES.TSPropertySignature:
return node.readonly ? 'readonly-field' : 'field';
case AST_NODE_TYPES.TSAbstractAccessorProperty:
case AST_NODE_TYPES.AccessorProperty:
return 'accessor';
case AST_NODE_TYPES.PropertyDefinition:
Expand All @@ -399,8 +401,6 @@ function getNodeType(node: Member): MemberKind | null {
: node.readonly
? 'readonly-field'
: 'field';
case AST_NODE_TYPES.TSPropertySignature:
return node.readonly ? 'readonly-field' : 'field';
case AST_NODE_TYPES.TSIndexSignature:
return node.readonly ? 'readonly-signature' : 'signature';
case AST_NODE_TYPES.StaticBlock:
Expand All @@ -416,9 +416,11 @@ function getNodeType(node: Member): MemberKind | null {
function getMemberRawName(
member:
| TSESTree.MethodDefinition
| TSESTree.AccessorProperty
| TSESTree.Property
| TSESTree.PropertyDefinition
| TSESTree.TSAbstractMethodDefinition
| TSESTree.TSAbstractAccessorProperty
| TSESTree.TSAbstractPropertyDefinition
| TSESTree.TSMethodSignature
| TSESTree.TSPropertySignature,
Expand Down Expand Up @@ -447,7 +449,9 @@ function getMemberName(
switch (node.type) {
case AST_NODE_TYPES.TSPropertySignature:
case AST_NODE_TYPES.TSMethodSignature:
case AST_NODE_TYPES.TSAbstractAccessorProperty:
case AST_NODE_TYPES.TSAbstractPropertyDefinition:
case AST_NODE_TYPES.AccessorProperty:
case AST_NODE_TYPES.PropertyDefinition:
return getMemberRawName(node, sourceCode);
case AST_NODE_TYPES.TSAbstractMethodDefinition:
Expand Down Expand Up @@ -479,7 +483,9 @@ function isMemberOptional(node: Member): boolean {
switch (node.type) {
case AST_NODE_TYPES.TSPropertySignature:
case AST_NODE_TYPES.TSMethodSignature:
case AST_NODE_TYPES.TSAbstractAccessorProperty:
case AST_NODE_TYPES.TSAbstractPropertyDefinition:
case AST_NODE_TYPES.AccessorProperty:
case AST_NODE_TYPES.PropertyDefinition:
case AST_NODE_TYPES.TSAbstractMethodDefinition:
case AST_NODE_TYPES.MethodDefinition:
Expand Down Expand Up @@ -549,6 +555,7 @@ function getRank(
}

const abstract =
node.type === AST_NODE_TYPES.TSAbstractAccessorProperty ||
node.type === AST_NODE_TYPES.TSAbstractPropertyDefinition ||
node.type === AST_NODE_TYPES.TSAbstractMethodDefinition;

Expand All @@ -571,6 +578,7 @@ function getRank(
(type === 'readonly-field' ||
type === 'field' ||
type === 'method' ||
type === 'accessor' ||
type === 'get' ||
type === 'set')
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,7 @@ function isTypeParameterRepeatedInAST(

// If the type parameter is being used as a type argument, then we
// know the type parameter is being reused and can't be reported.
if (
reference.identifier.parent.type === AST_NODE_TYPES.TSTypeReference
) {
if (reference.identifier.parent.type === AST_NODE_TYPES.TSTypeReference) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm, why is this changed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Josh-Cena, explained there

const grandparent = skipConstituentsUpward(
reference.identifier.parent.parent,
);
Expand Down
2 changes: 2 additions & 0 deletions packages/eslint-plugin/src/util/misc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,11 @@ enum MemberNameType {
function getNameFromMember(
member:
| TSESTree.MethodDefinition
| TSESTree.AccessorProperty
| TSESTree.Property
| TSESTree.PropertyDefinition
| TSESTree.TSAbstractMethodDefinition
| TSESTree.TSAbstractAccessorProperty
| TSESTree.TSAbstractPropertyDefinition
| TSESTree.TSMethodSignature
| TSESTree.TSPropertySignature,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2609,6 +2609,86 @@ class Foo {
},
],
},
// default option + accessors
{
code: `
class Foo {
@Dec() accessor b;
@Dec() accessor a;

accessor d;
accessor c;

abstract accessor f;
abstract accessor e;
}
`,
options: [
{ default: { memberTypes: defaultOrder, order: 'alphabetically' } },
],
errors: [
{
messageId: 'incorrectOrder',
data: {
member: 'a',
beforeMember: 'b',
},
},
{
messageId: 'incorrectOrder',
data: {
member: 'c',
beforeMember: 'd',
},
},
{
messageId: 'incorrectOrder',
data: {
member: 'e',
beforeMember: 'f',
},
},
],
},
// accessors with wrong group order
{
code: `
class Foo {
accessor a;
abstract accessor b;
accessor c;
@Dec() accessor d;
}
`,
options: [
{
default: {
memberTypes: [
'decorated-accessor',
'accessor',
'abstract-accessor',
],
order: 'alphabetically',
},
},
],
errors: [
{
messageId: 'incorrectGroupOrder',
data: {
name: 'c',
rank: 'abstract accessor',
},
},
{
messageId: 'incorrectGroupOrder',
data: {
name: 'd',
rank: 'accessor',
},
},
],
},
],
};

Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy