Skip to content

Commit

Permalink
Fix propertyType detection on TSPropertySignatures. (#101)
Browse files Browse the repository at this point in the history
* Fix `propertyType` detection on `TSPropertySignature`s.

* Add missing `sortInterfaces` option in README.md.

* Run prettier

* Forgot to remove a console.log
  • Loading branch information
ygrandgirard authored Oct 21, 2024
1 parent 8e8fcf6 commit b40d40d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
6 changes: 5 additions & 1 deletion src/rules/sort-class-members.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
36 changes: 36 additions & 0 deletions test/rules/sort-class-members.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
{
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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'),
},
],
});

Expand Down

0 comments on commit b40d40d

Please sign in to comment.