Skip to content

Commit

Permalink
feat: add method group in sort-interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
azat-io committed Oct 10, 2024
1 parent 28f65a6 commit b797371
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 2 deletions.
2 changes: 2 additions & 0 deletions docs/content/rules/sort-interfaces.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ Allows you to specify a list of interface member groups for sorting. Groups help
Predefined groups:

- `'multiline'` — Members with multiline definitions, such as methods or properties with complex types.
- `'method'` - Members that are methods.
- `'unknown'` — Interface members that don’t fit into any group specified in the `groups` option.

If the `unknown` group is not specified in the `groups` option, it will automatically be added to the end of the list.
Expand Down Expand Up @@ -270,6 +271,7 @@ interface User {
{
groups: [
'unknown',
'method',
'multiline',
]
}
Expand Down
13 changes: 11 additions & 2 deletions rules/sort-interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type MESSAGE_ID =
| 'unexpectedInterfacePropertiesGroupOrder'
| 'unexpectedInterfacePropertiesOrder'

type Group<T extends string[]> = 'multiline' | 'unknown' | T[number]
type Group<T extends string[]> = 'multiline' | 'unknown' | T[number] | 'method'

type Options<T extends string[]> = [
Partial<{
Expand Down Expand Up @@ -195,7 +195,7 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({

validateGroupsConfiguration(
options.groups,
['multiline', 'unknown'],
['multiline', 'method', 'unknown'],
Object.keys(options.customGroups),
)

Expand Down Expand Up @@ -246,6 +246,15 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({

setCustomGroups(options.customGroups, name)

if (
element.type === 'TSMethodSignature' ||
(element.type === 'TSPropertySignature' &&
element.typeAnnotation?.typeAnnotation.type ===
'TSFunctionType')
) {
defineGroup('method')
}

if (element.loc.start.line !== element.loc.end.line) {
defineGroup('multiline')
}
Expand Down
66 changes: 66 additions & 0 deletions test/sort-interfaces.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1025,6 +1025,28 @@ describe(ruleName, () => {
invalid: [],
},
)

ruleTester.run(`${ruleName}(${type}): allows to use method group`, rule, {
valid: [
{
code: dedent`
interface Interface {
b(): void
c: () => void
a: string
d: string
}
`,
options: [
{
...options,
groups: ['method', 'unknown'],
},
],
},
],
invalid: [],
})
})

describe(`${ruleName}: sorting by natural order`, () => {
Expand Down Expand Up @@ -1782,6 +1804,28 @@ describe(ruleName, () => {
],
},
)

ruleTester.run(`${ruleName}(${type}): allows to use method group`, rule, {
valid: [
{
code: dedent`
interface Interface {
b(): void
c: () => void
a: string
d: string
}
`,
options: [
{
...options,
groups: ['method', 'unknown'],
},
],
},
],
invalid: [],
})
})

describe(`${ruleName}: sorting by line length`, () => {
Expand Down Expand Up @@ -2746,6 +2790,28 @@ describe(ruleName, () => {
],
},
)

ruleTester.run(`${ruleName}(${type}): allows to use method group`, rule, {
valid: [
{
code: dedent`
interface Interface {
c: () => void
b(): void
a: string
d: string
}
`,
options: [
{
...options,
groups: ['method', 'unknown'],
},
],
},
],
invalid: [],
})
})

describe(`${ruleName}: validating group configuration`, () => {
Expand Down

0 comments on commit b797371

Please sign in to comment.