From b40d40dc94d271335b268d9d4ad7d8d6ae27dd19 Mon Sep 17 00:00:00 2001 From: ygrandgirard <101559504+ygrandgirard@users.noreply.github.com> Date: Mon, 21 Oct 2024 19:55:55 +0200 Subject: [PATCH] Fix `propertyType` detection on `TSPropertySignature`s. (#101) * Fix `propertyType` detection on `TSPropertySignature`s. * Add missing `sortInterfaces` option in README.md. * Run prettier * Forgot to remove a console.log --- README.md | 1 + src/rules/sort-class-members.js | 6 ++++- test/rules/sort-class-members.spec.js | 36 +++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 152ad27..883533e 100644 --- a/README.md +++ b/README.md @@ -101,6 +101,7 @@ The rule accepts the following configuration properties: - `accessorPairPositioning`: Used to specify the required positioning of get/set pairs. Available values: `getThenSet`, `setThenGet`, `together`, `any`. - `stopAfterFirstProblem`: Only report the first sort problem in each class (plus the number of problems found). Useful if you only want to know that the class has sort problems without spamming error messages. The default is `false`. - `locale`: Used to specify the locale for the name comparison method. The default is `en-US`. +- `sortInterfaces`: Used to specify whether to sort TypeScript interfaces as well. The default is `false`. ```js { diff --git a/src/rules/sort-class-members.js b/src/rules/sort-class-members.js index 918149e..761bcef 100644 --- a/src/rules/sort-class-members.js +++ b/src/rules/sort-class-members.js @@ -209,7 +209,11 @@ function getMemberInfo(node, sourceCode) { name = second && second.type === 'Identifier' ? second.value : first.value; } - propertyType = node.value ? node.value.type : node.value; + if (node.typeAnnotation) { + propertyType = node.typeAnnotation.typeAnnotation.type; + } else { + propertyType = node.value ? node.value.type : node.value; + } } else { if (node.computed) { const keyBeforeToken = sourceCode.getTokenBefore(node.key); diff --git a/test/rules/sort-class-members.spec.js b/test/rules/sort-class-members.spec.js index 5af3ff2..f0f0c04 100644 --- a/test/rules/sort-class-members.spec.js +++ b/test/rules/sort-class-members.spec.js @@ -142,6 +142,25 @@ const propertyTypeOptions = [ ], }, ]; +const typescriptPropertyTypeOptions = [ + { + order: [ + { type: 'property', propertyType: 'TSLiteralType' }, + { type: 'property', propertyType: 'TSFunctionType' }, + '[everything-else]', + ], + }, +]; +const typescriptInterfacePropertyTypeOptions = [ + { + order: [ + { type: 'property', propertyType: 'TSLiteralType' }, + { type: 'property', propertyType: 'TSFunctionType' }, + '[everything-else]', + ], + sortInterfaces: true, + }, +]; const decoratorOptions = [ { @@ -461,6 +480,11 @@ ruleTester.run('sort-class-members', rule, { { code: 'class { [k: string]: any; }', parser: require.resolve('@typescript-eslint/parser') }, // TS accessibility + { + code: 'class A { foo: 1; bar: () => 2 }', + options: typescriptPropertyTypeOptions, + parser: require.resolve('@typescript-eslint/parser'), + }, { code: 'class { private a: any; constructor(){} b(){} }', options: typescriptAccessibilityOptions, @@ -1066,6 +1090,18 @@ ruleTester.run('sort-class-members', rule, { options: typescriptInterfaceOptions, parser: require.resolve('@typescript-eslint/parser'), }, + { + code: 'interface A { bar: () => 2; foo: 1; }', + output: 'interface A { foo: 1; bar: () => 2; }', + errors: [ + { + message: 'Expected property foo to come before property bar.', + type: 'TSPropertySignature', + }, + ], + options: typescriptInterfacePropertyTypeOptions, + parser: require.resolve('@typescript-eslint/parser'), + }, ], });