Skip to content

Commit

Permalink
fix(eslint-plugin): [member-ordering] order literal names correctly in (
Browse files Browse the repository at this point in the history
  • Loading branch information
avaly committed Nov 17, 2021
1 parent e734a74 commit d57141a
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 2 deletions.
29 changes: 27 additions & 2 deletions packages/eslint-plugin/src/rules/member-ordering.ts
Expand Up @@ -270,6 +270,31 @@ function getNodeType(node: Member): string | null {
}
}

/**
* Gets the raw string value of a member's name
*/
function getMemberRawName(
member:
| TSESTree.MethodDefinition
| TSESTree.TSMethodSignature
| TSESTree.TSAbstractMethodDefinition
| TSESTree.PropertyDefinition
| TSESTree.TSAbstractPropertyDefinition
| TSESTree.Property
| TSESTree.TSPropertySignature,
sourceCode: TSESLint.SourceCode,
): string {
const { name, type } = util.getNameFromMember(member, sourceCode);

if (type === util.MemberNameType.Quoted) {
return name.substr(1, name.length - 2);
}
if (type === util.MemberNameType.Private) {
return name.substr(1);
}
return name;
}

/**
* Gets the member name based on the member type.
*
Expand All @@ -285,12 +310,12 @@ function getMemberName(
case AST_NODE_TYPES.TSMethodSignature:
case AST_NODE_TYPES.TSAbstractPropertyDefinition:
case AST_NODE_TYPES.PropertyDefinition:
return util.getNameFromMember(node, sourceCode).name;
return getMemberRawName(node, sourceCode);
case AST_NODE_TYPES.TSAbstractMethodDefinition:
case AST_NODE_TYPES.MethodDefinition:
return node.kind === 'constructor'
? 'constructor'
: util.getNameFromMember(node, sourceCode).name;
: getMemberRawName(node, sourceCode);
case AST_NODE_TYPES.TSConstructSignatureDeclaration:
return 'new';
case AST_NODE_TYPES.TSCallSignatureDeclaration:
Expand Down
Expand Up @@ -50,6 +50,18 @@ interface Foo {
options: [{ default: { memberTypes: 'never', order: 'alphabetically' } }],
},

// default option + interface + literal properties
{
code: `
interface Foo {
a : Foo;
'b.c' : Foo;
"b.d" : Foo;
}
`,
options: [{ default: { order: 'alphabetically' } }],
},

// default option + type literal + multiple types
{
code: `
Expand Down Expand Up @@ -86,6 +98,18 @@ type Foo = {
options: [{ default: { memberTypes: 'never', order: 'alphabetically' } }],
},

// default option + type + literal properties
{
code: `
type Foo = {
a : Foo;
'b.c' : Foo;
"b.d" : Foo;
}
`,
options: [{ default: { order: 'alphabetically' } }],
},

// default option + class + multiple types
{
code: `
Expand Down Expand Up @@ -221,6 +245,34 @@ interface Foo {
],
},

// default option + interface + literal properties
{
code: `
interface Foo {
"b.d" : Foo;
'b.c' : Foo;
a : Foo;
}
`,
options: [{ default: { order: 'alphabetically' } }],
errors: [
{
messageId: 'incorrectOrder',
data: {
member: 'b.c',
beforeMember: 'b.d',
},
},
{
messageId: 'incorrectOrder',
data: {
member: 'a',
beforeMember: 'b.c',
},
},
],
},

// default option + interface + wrong order (multiple)
{
code: `
Expand Down Expand Up @@ -279,6 +331,34 @@ type Foo = {
],
},

// default option + type + literal properties
{
code: `
type Foo = {
"b.d" : Foo;
'b.c' : Foo;
a : Foo;
}
`,
options: [{ default: { order: 'alphabetically' } }],
errors: [
{
messageId: 'incorrectOrder',
data: {
member: 'b.c',
beforeMember: 'b.d',
},
},
{
messageId: 'incorrectOrder',
data: {
member: 'a',
beforeMember: 'b.c',
},
},
],
},

// default option + type literal + wrong order (multiple)
{
code: `
Expand Down

0 comments on commit d57141a

Please sign in to comment.