Skip to content

Commit b40d40d

Browse files
authored
Fix propertyType detection on TSPropertySignatures. (#101)
* Fix `propertyType` detection on `TSPropertySignature`s. * Add missing `sortInterfaces` option in README.md. * Run prettier * Forgot to remove a console.log
1 parent 8e8fcf6 commit b40d40d

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ The rule accepts the following configuration properties:
101101
- `accessorPairPositioning`: Used to specify the required positioning of get/set pairs. Available values: `getThenSet`, `setThenGet`, `together`, `any`.
102102
- `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`.
103103
- `locale`: Used to specify the locale for the name comparison method. The default is `en-US`.
104+
- `sortInterfaces`: Used to specify whether to sort TypeScript interfaces as well. The default is `false`.
104105

105106
```js
106107
{

src/rules/sort-class-members.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,11 @@ function getMemberInfo(node, sourceCode) {
209209
name = second && second.type === 'Identifier' ? second.value : first.value;
210210
}
211211

212-
propertyType = node.value ? node.value.type : node.value;
212+
if (node.typeAnnotation) {
213+
propertyType = node.typeAnnotation.typeAnnotation.type;
214+
} else {
215+
propertyType = node.value ? node.value.type : node.value;
216+
}
213217
} else {
214218
if (node.computed) {
215219
const keyBeforeToken = sourceCode.getTokenBefore(node.key);

test/rules/sort-class-members.spec.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,25 @@ const propertyTypeOptions = [
142142
],
143143
},
144144
];
145+
const typescriptPropertyTypeOptions = [
146+
{
147+
order: [
148+
{ type: 'property', propertyType: 'TSLiteralType' },
149+
{ type: 'property', propertyType: 'TSFunctionType' },
150+
'[everything-else]',
151+
],
152+
},
153+
];
154+
const typescriptInterfacePropertyTypeOptions = [
155+
{
156+
order: [
157+
{ type: 'property', propertyType: 'TSLiteralType' },
158+
{ type: 'property', propertyType: 'TSFunctionType' },
159+
'[everything-else]',
160+
],
161+
sortInterfaces: true,
162+
},
163+
];
145164

146165
const decoratorOptions = [
147166
{
@@ -461,6 +480,11 @@ ruleTester.run('sort-class-members', rule, {
461480
{ code: 'class { [k: string]: any; }', parser: require.resolve('@typescript-eslint/parser') },
462481

463482
// TS accessibility
483+
{
484+
code: 'class A { foo: 1; bar: () => 2 }',
485+
options: typescriptPropertyTypeOptions,
486+
parser: require.resolve('@typescript-eslint/parser'),
487+
},
464488
{
465489
code: 'class { private a: any; constructor(){} b(){} }',
466490
options: typescriptAccessibilityOptions,
@@ -1066,6 +1090,18 @@ ruleTester.run('sort-class-members', rule, {
10661090
options: typescriptInterfaceOptions,
10671091
parser: require.resolve('@typescript-eslint/parser'),
10681092
},
1093+
{
1094+
code: 'interface A { bar: () => 2; foo: 1; }',
1095+
output: 'interface A { foo: 1; bar: () => 2; }',
1096+
errors: [
1097+
{
1098+
message: 'Expected property foo to come before property bar.',
1099+
type: 'TSPropertySignature',
1100+
},
1101+
],
1102+
options: typescriptInterfacePropertyTypeOptions,
1103+
parser: require.resolve('@typescript-eslint/parser'),
1104+
},
10691105
],
10701106
});
10711107

0 commit comments

Comments
 (0)