Skip to content

Commit

Permalink
Add option to sort TypeScript interfaces (#96)
Browse files Browse the repository at this point in the history
* Try to sort TypeScript interfaces as well

* Added `sortInterfaces` option to control TSInterfaceDeclaration sorting rule.

* Fixed `TSPropertySignature`s treated as methods & testing
  • Loading branch information
ygrandgirard authored Oct 3, 2023
1 parent b549146 commit a6fb8ac
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/rules/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ export const sortClassMembersSchema = [
stopAfterFirstProblem: {
type: 'boolean',
},
sortInterfaces: {
type: 'boolean',
},
accessorPairPositioning: {
enum: ['getThenSet', 'setThenGet', 'together', 'any'],
},
Expand Down
7 changes: 6 additions & 1 deletion src/rules/sort-class-members.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const sortClassMembersRule = {
create: function sortClassMembersRule(context) {
const options = context.options[0] || {};
const stopAfterFirst = !!options.stopAfterFirstProblem;
const sortInterfaces = !!options.sortInterfaces;
const accessorPairPositioning = options.accessorPairPositioning || 'getThenSet';
const order = options.order || [];
const groups = { ...builtInGroups, ...options.groups };
Expand Down Expand Up @@ -66,6 +67,9 @@ export const sortClassMembersRule = {
};

rules.ClassExpression = rules.ClassDeclaration;
if (sortInterfaces) {
rules.TSInterfaceDeclaration = rules.ClassDeclaration;
}

return rules;
},
Expand Down Expand Up @@ -193,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';

Expand Down
58 changes: 58 additions & 0 deletions test/rules/sort-class-members.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand Down Expand Up @@ -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'),
},
],
});

Expand Down

0 comments on commit a6fb8ac

Please sign in to comment.