From 89aae109230af9ea036d2ff679505c5a12ab568e Mon Sep 17 00:00:00 2001 From: ygrandgirard Date: Tue, 3 Oct 2023 18:21:12 +0200 Subject: [PATCH] Fixed `TSPropertySignature`s treated as methods & testing --- src/rules/sort-class-members.js | 3 +- test/rules/sort-class-members.spec.js | 58 +++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/src/rules/sort-class-members.js b/src/rules/sort-class-members.js index 92523fa..918149e 100644 --- a/src/rules/sort-class-members.js +++ b/src/rules/sort-class-members.js @@ -197,7 +197,8 @@ function getMemberInfo(node, sourceCode) { node.type === 'ClassPrivateProperty' || node.type === 'PropertyDefinition' || node.type === 'PrivateIdentifier' || - node.type === 'TSAbstractPropertyDefinition' + node.type === 'TSAbstractPropertyDefinition' || + node.type === 'TSPropertySignature' ) { type = 'property'; diff --git a/test/rules/sort-class-members.spec.js b/test/rules/sort-class-members.spec.js index c58a9bf..5af3ff2 100644 --- a/test/rules/sort-class-members.spec.js +++ b/test/rules/sort-class-members.spec.js @@ -308,6 +308,27 @@ const typescriptKeywordsOptions = [ }, ]; +const typescriptInterfaceOptions = [ + { + order: ['[properties]', '[accessors]', '[non-accessors]'], + groups: { + accessors: [ + { + type: 'method', + kind: 'accessor', + }, + ], + 'non-accessors': [ + { + type: 'method', + kind: 'nonAccessor', + }, + ], + }, + sortInterfaces: true, + }, +]; + ruleTester.run('sort-class-members', rule, { valid: [ { code: 'class A {}', options: defaultOptions }, @@ -1008,6 +1029,43 @@ ruleTester.run('sort-class-members', rule, { options: typescriptKeywordsOptions, parser: require.resolve('@typescript-eslint/parser'), }, + // Interface sorting + { + code: 'interface A { get a(); b; }', + output: 'interface A { b; get a(); }', + errors: [ + { + message: 'Expected property b to come before getter a.', + type: 'TSPropertySignature', + }, + ], + options: typescriptInterfaceOptions, + parser: require.resolve('@typescript-eslint/parser'), + }, + { + code: 'interface A { a(); b; }', + output: 'interface A { b; a(); }', + errors: [ + { + message: 'Expected property b to come before method a.', + type: 'TSPropertySignature', + }, + ], + options: typescriptInterfaceOptions, + parser: require.resolve('@typescript-eslint/parser'), + }, + { + code: 'interface A { a(); get b(); }', + output: 'interface A { get b(); a(); }', + errors: [ + { + message: 'Expected getter b to come before method a.', + type: 'TSMethodSignature', + }, + ], + options: typescriptInterfaceOptions, + parser: require.resolve('@typescript-eslint/parser'), + }, ], });