From f60c402192f6f76651b5fd75d3352db618edea83 Mon Sep 17 00:00:00 2001 From: Valentin Agachi Date: Mon, 25 Oct 2021 18:06:14 +0200 Subject: [PATCH] fix(eslint-plugin): order literal names correctly in member-ordering --- .../src/rules/member-ordering.ts | 29 ++++++- .../tests/rules/member-ordering.test.ts | 80 +++++++++++++++++++ 2 files changed, 107 insertions(+), 2 deletions(-) diff --git a/packages/eslint-plugin/src/rules/member-ordering.ts b/packages/eslint-plugin/src/rules/member-ordering.ts index c1e30adc2ef5..f1e4badc417f 100644 --- a/packages/eslint-plugin/src/rules/member-ordering.ts +++ b/packages/eslint-plugin/src/rules/member-ordering.ts @@ -265,6 +265,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. * @@ -280,12 +305,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: diff --git a/packages/eslint-plugin/tests/rules/member-ordering.test.ts b/packages/eslint-plugin/tests/rules/member-ordering.test.ts index 97b05cfdeffc..979c62103485 100644 --- a/packages/eslint-plugin/tests/rules/member-ordering.test.ts +++ b/packages/eslint-plugin/tests/rules/member-ordering.test.ts @@ -3871,6 +3871,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: ` @@ -3907,6 +3919,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: ` @@ -4042,6 +4066,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: ` @@ -4100,6 +4152,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: `