Skip to content

Commit

Permalink
feat: add partition by comment and partition by new line in sort-inte…
Browse files Browse the repository at this point in the history
…rfaces
  • Loading branch information
chirokas authored Sep 22, 2024
1 parent aa29335 commit fae756a
Show file tree
Hide file tree
Showing 4 changed files with 476 additions and 10 deletions.
11 changes: 11 additions & 0 deletions docs/content/rules/sort-interfaces.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,17 @@ Allows you to specify names or patterns for interfaces that should be ignored by

You can specify their names or a glob pattern to ignore, for example: `'Component*'` to ignore all interfaces whose names begin with the word “Component”.

### partitionByComment

<sub>default: `false`</sub>

Allows you to use comments to separate the properties of interfaces into logical groups. This can help in organizing and maintaining large interfaces by creating partitions within the interface based on comments.

- `true` — All comments will be treated as delimiters, creating partitions.
- `false` — Comments will not be used as delimiters.
- `string` — A glob pattern to specify which comments should act as delimiters.
- `string[]` — An array of glob patterns to specify which comments should act as delimiters.

### partitionByNewLine

<sub>default: `false`</sub>
Expand Down
39 changes: 35 additions & 4 deletions rules/sort-interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { minimatch } from 'minimatch'
import type { SortingNode } from '../typings'

import { validateGroupsConfiguration } from '../utils/validate-groups-configuration'
import { hasPartitionComment } from '../utils/is-partition-comment'
import { getCommentsBefore } from '../utils/get-comments-before'
import { createEslintRule } from '../utils/create-eslint-rule'
import { isMemberOptional } from '../utils/is-member-optional'
import { getLinesBetween } from '../utils/get-lines-between'
Expand Down Expand Up @@ -30,6 +32,7 @@ type Options<T extends string[]> = [
groupKind: 'optional-first' | 'required-first' | 'mixed'
customGroups: { [key: string]: string[] | string }
type: 'alphabetical' | 'line-length' | 'natural'
partitionByComment: string[] | boolean | string
groups: (Group<T>[] | Group<T>)[]
partitionByNewLine: boolean
ignorePattern: string[]
Expand Down Expand Up @@ -74,6 +77,24 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
},
type: 'array',
},
partitionByComment: {
description:
'Allows you to use comments to separate the interface properties into logical groups.',
anyOf: [
{
type: 'boolean',
},
{
type: 'string',
},
{
type: 'array',
items: {
type: 'string',
},
},
],
},
partitionByNewLine: {
description:
'Allows to use spaces to separate the nodes into logical groups.',
Expand Down Expand Up @@ -135,6 +156,7 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
order: 'asc',
ignoreCase: true,
ignorePattern: [],
partitionByComment: false,
partitionByNewLine: false,
groupKind: 'mixed',
groups: [],
Expand All @@ -146,6 +168,7 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
if (node.body.body.length > 1) {
let settings = getSettings(context.settings)
let options = complete(context.options.at(0), settings, {
partitionByComment: false,
partitionByNewLine: false,
type: 'alphabetical',
groupKind: 'mixed',
Expand All @@ -163,6 +186,7 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
)

let sourceCode = getSourceCode(context)
let partitionComment = options.partitionByComment

if (
!options.ignorePattern.some(pattern =>
Expand Down Expand Up @@ -216,9 +240,14 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
}

if (
options.partitionByNewLine &&
lastElement &&
getLinesBetween(sourceCode, lastElement, elementSortingNode)
(partitionComment &&
hasPartitionComment(
partitionComment,
getCommentsBefore(element, sourceCode),
)) ||
(options.partitionByNewLine &&
lastElement &&
getLinesBetween(sourceCode, lastElement, elementSortingNode))
) {
accumulator.push([])
}
Expand Down Expand Up @@ -353,7 +382,9 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
sortedNodes = toSorted(nodes)
}

return makeFixes(fixer, nodes, sortedNodes, sourceCode)
return makeFixes(fixer, nodes, sortedNodes, sourceCode, {
partitionComment,
})
},
})
}
Expand Down
Loading

0 comments on commit fae756a

Please sign in to comment.