Skip to content

Commit 3d9ae44

Browse files
fix(eslint-plugin): [member-ordering] get accessor member name & take into account abstract and decorator (typescript-eslint#9775)
* fix: handle accessor names * fix: abstract not taken into account for accessors * fix: decorator not taken into account for accessors * Add tests * fix format --------- Co-authored-by: Joshua Chen <sidachen2003@gmail.com>
1 parent fbe5f2d commit 3d9ae44

File tree

4 files changed

+93
-5
lines changed

4 files changed

+93
-5
lines changed

packages/eslint-plugin/src/rules/member-ordering.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,9 @@ function getNodeType(node: Member): MemberKind | null {
390390
case AST_NODE_TYPES.TSConstructSignatureDeclaration:
391391
return 'constructor';
392392
case AST_NODE_TYPES.TSAbstractPropertyDefinition:
393+
case AST_NODE_TYPES.TSPropertySignature:
393394
return node.readonly ? 'readonly-field' : 'field';
395+
case AST_NODE_TYPES.TSAbstractAccessorProperty:
394396
case AST_NODE_TYPES.AccessorProperty:
395397
return 'accessor';
396398
case AST_NODE_TYPES.PropertyDefinition:
@@ -399,8 +401,6 @@ function getNodeType(node: Member): MemberKind | null {
399401
: node.readonly
400402
? 'readonly-field'
401403
: 'field';
402-
case AST_NODE_TYPES.TSPropertySignature:
403-
return node.readonly ? 'readonly-field' : 'field';
404404
case AST_NODE_TYPES.TSIndexSignature:
405405
return node.readonly ? 'readonly-signature' : 'signature';
406406
case AST_NODE_TYPES.StaticBlock:
@@ -416,9 +416,11 @@ function getNodeType(node: Member): MemberKind | null {
416416
function getMemberRawName(
417417
member:
418418
| TSESTree.MethodDefinition
419+
| TSESTree.AccessorProperty
419420
| TSESTree.Property
420421
| TSESTree.PropertyDefinition
421422
| TSESTree.TSAbstractMethodDefinition
423+
| TSESTree.TSAbstractAccessorProperty
422424
| TSESTree.TSAbstractPropertyDefinition
423425
| TSESTree.TSMethodSignature
424426
| TSESTree.TSPropertySignature,
@@ -447,7 +449,9 @@ function getMemberName(
447449
switch (node.type) {
448450
case AST_NODE_TYPES.TSPropertySignature:
449451
case AST_NODE_TYPES.TSMethodSignature:
452+
case AST_NODE_TYPES.TSAbstractAccessorProperty:
450453
case AST_NODE_TYPES.TSAbstractPropertyDefinition:
454+
case AST_NODE_TYPES.AccessorProperty:
451455
case AST_NODE_TYPES.PropertyDefinition:
452456
return getMemberRawName(node, sourceCode);
453457
case AST_NODE_TYPES.TSAbstractMethodDefinition:
@@ -479,7 +483,9 @@ function isMemberOptional(node: Member): boolean {
479483
switch (node.type) {
480484
case AST_NODE_TYPES.TSPropertySignature:
481485
case AST_NODE_TYPES.TSMethodSignature:
486+
case AST_NODE_TYPES.TSAbstractAccessorProperty:
482487
case AST_NODE_TYPES.TSAbstractPropertyDefinition:
488+
case AST_NODE_TYPES.AccessorProperty:
483489
case AST_NODE_TYPES.PropertyDefinition:
484490
case AST_NODE_TYPES.TSAbstractMethodDefinition:
485491
case AST_NODE_TYPES.MethodDefinition:
@@ -549,6 +555,7 @@ function getRank(
549555
}
550556

551557
const abstract =
558+
node.type === AST_NODE_TYPES.TSAbstractAccessorProperty ||
552559
node.type === AST_NODE_TYPES.TSAbstractPropertyDefinition ||
553560
node.type === AST_NODE_TYPES.TSAbstractMethodDefinition;
554561

@@ -571,6 +578,7 @@ function getRank(
571578
(type === 'readonly-field' ||
572579
type === 'field' ||
573580
type === 'method' ||
581+
type === 'accessor' ||
574582
type === 'get' ||
575583
type === 'set')
576584
) {

packages/eslint-plugin/src/rules/no-unnecessary-type-parameters.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,7 @@ function isTypeParameterRepeatedInAST(
122122

123123
// If the type parameter is being used as a type argument, then we
124124
// know the type parameter is being reused and can't be reported.
125-
if (
126-
reference.identifier.parent.type === AST_NODE_TYPES.TSTypeReference
127-
) {
125+
if (reference.identifier.parent.type === AST_NODE_TYPES.TSTypeReference) {
128126
const grandparent = skipConstituentsUpward(
129127
reference.identifier.parent.parent,
130128
);

packages/eslint-plugin/src/util/misc.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,11 @@ enum MemberNameType {
110110
function getNameFromMember(
111111
member:
112112
| TSESTree.MethodDefinition
113+
| TSESTree.AccessorProperty
113114
| TSESTree.Property
114115
| TSESTree.PropertyDefinition
115116
| TSESTree.TSAbstractMethodDefinition
117+
| TSESTree.TSAbstractAccessorProperty
116118
| TSESTree.TSAbstractPropertyDefinition
117119
| TSESTree.TSMethodSignature
118120
| TSESTree.TSPropertySignature,

packages/eslint-plugin/tests/rules/member-ordering/member-ordering-alphabetically-order.test.ts

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2609,6 +2609,86 @@ class Foo {
26092609
},
26102610
],
26112611
},
2612+
// default option + accessors
2613+
{
2614+
code: `
2615+
class Foo {
2616+
@Dec() accessor b;
2617+
@Dec() accessor a;
2618+
2619+
accessor d;
2620+
accessor c;
2621+
2622+
abstract accessor f;
2623+
abstract accessor e;
2624+
}
2625+
`,
2626+
options: [
2627+
{ default: { memberTypes: defaultOrder, order: 'alphabetically' } },
2628+
],
2629+
errors: [
2630+
{
2631+
messageId: 'incorrectOrder',
2632+
data: {
2633+
member: 'a',
2634+
beforeMember: 'b',
2635+
},
2636+
},
2637+
{
2638+
messageId: 'incorrectOrder',
2639+
data: {
2640+
member: 'c',
2641+
beforeMember: 'd',
2642+
},
2643+
},
2644+
{
2645+
messageId: 'incorrectOrder',
2646+
data: {
2647+
member: 'e',
2648+
beforeMember: 'f',
2649+
},
2650+
},
2651+
],
2652+
},
2653+
// accessors with wrong group order
2654+
{
2655+
code: `
2656+
class Foo {
2657+
accessor a;
2658+
abstract accessor b;
2659+
accessor c;
2660+
@Dec() accessor d;
2661+
}
2662+
`,
2663+
options: [
2664+
{
2665+
default: {
2666+
memberTypes: [
2667+
'decorated-accessor',
2668+
'accessor',
2669+
'abstract-accessor',
2670+
],
2671+
order: 'alphabetically',
2672+
},
2673+
},
2674+
],
2675+
errors: [
2676+
{
2677+
messageId: 'incorrectGroupOrder',
2678+
data: {
2679+
name: 'c',
2680+
rank: 'abstract accessor',
2681+
},
2682+
},
2683+
{
2684+
messageId: 'incorrectGroupOrder',
2685+
data: {
2686+
name: 'd',
2687+
rank: 'accessor',
2688+
},
2689+
},
2690+
],
2691+
},
26122692
],
26132693
};
26142694

0 commit comments

Comments
 (0)
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