Skip to content

Commit

Permalink
feat: add partition by comment and partition by new line in sort-name…
Browse files Browse the repository at this point in the history
…d-exports
  • Loading branch information
hugop95 authored Sep 22, 2024
1 parent 41e18b9 commit 928246e
Show file tree
Hide file tree
Showing 3 changed files with 324 additions and 28 deletions.
37 changes: 37 additions & 0 deletions docs/content/rules/sort-named-exports.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,43 @@ Allows you to group named exports by their kind, determining whether value expor
- `values-first` — Group all value exports before type exports.
- `types-first` — Group all type exports before value exports.

### partitionByComment

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

Allows you to use comments to separate the members of named exports into logical groups. This can help in organizing and maintaining large enums by creating partitions within the enum 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[]` — A list of glob patterns to specify which comments should act as delimiters.

### partitionByNewLine

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

When `true`, the rule will not sort the members of named exports if there is an empty line between them. This can be useful for keeping logically separated groups of members in their defined order.

```ts
export {
// Group 1
Drone,
Keyboard,
Mouse,
Smartphone,

// Group 2
Laptop,
Monitor,
Smartwatch,
Tablet,

// Group 3
Headphones,
Router,
} from './devices'
```

## Usage

<CodeTabs
Expand Down
111 changes: 83 additions & 28 deletions rules/sort-named-exports.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import type { SortingNode } from '../typings'

import { hasPartitionComment } from '../utils/is-partition-comment'
import { getCommentsBefore } from '../utils/get-comments-before'
import { createEslintRule } from '../utils/create-eslint-rule'
import { getLinesBetween } from '../utils/get-lines-between'
import { getGroupNumber } from '../utils/get-group-number'
import { getSourceCode } from '../utils/get-source-code'
import { rangeToDiff } from '../utils/range-to-diff'
Expand All @@ -18,6 +21,8 @@ type Options = [
Partial<{
groupKind: 'values-first' | 'types-first' | 'mixed'
type: 'alphabetical' | 'line-length' | 'natural'
partitionByComment: string[] | boolean | string
partitionByNewLine: boolean
order: 'desc' | 'asc'
ignoreCase: boolean
}>,
Expand Down Expand Up @@ -56,6 +61,29 @@ export default createEslintRule<Options, MESSAGE_ID>({
enum: ['mixed', 'values-first', 'types-first'],
type: 'string',
},
partitionByComment: {
description:
'Allows you to use comments to separate the named exports members into logical groups.',
anyOf: [
{
type: 'array',
items: {
type: 'string',
},
},
{
type: 'boolean',
},
{
type: 'string',
},
],
},
partitionByNewLine: {
description:
'Allows to use spaces to separate the nodes into logical groups.',
type: 'boolean',
},
},
additionalProperties: false,
},
Expand All @@ -70,6 +98,8 @@ export default createEslintRule<Options, MESSAGE_ID>({
type: 'alphabetical',
order: 'asc',
ignoreCase: true,
partitionByNewLine: false,
partitionByComment: false,
groupKind: 'mixed',
},
],
Expand All @@ -82,59 +112,84 @@ export default createEslintRule<Options, MESSAGE_ID>({
type: 'alphabetical',
groupKind: 'mixed',
ignoreCase: true,
partitionByNewLine: false,
partitionByComment: false,
order: 'asc',
} as const)

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

let nodes: SortingNode[] = node.specifiers.map(specifier => {
let formattedMembers: SortingNode[][] = [[]]
for (let specifier of node.specifiers) {
let group: undefined | 'value' | 'type'
if (specifier.exportKind === 'type') {
group = 'type'
} else {
group = 'value'
}
return {

let lastSortingNode = formattedMembers.at(-1)?.at(-1)
let sortingNode: SortingNode = {
size: rangeToDiff(specifier.range),
name: specifier.local.name,
node: specifier,
group,
}
})
if (
(partitionComment &&
hasPartitionComment(
partitionComment,
getCommentsBefore(specifier, sourceCode),
)) ||
(options.partitionByNewLine &&
lastSortingNode &&
getLinesBetween(sourceCode, lastSortingNode, sortingNode))
) {
formattedMembers.push([])
}

formattedMembers.at(-1)!.push(sortingNode)
}

let shouldGroupByKind = options.groupKind !== 'mixed'
let groupKindOrder =
options.groupKind === 'values-first'
? ['value', 'type']
: ['type', 'value']

pairwise(nodes, (left, right) => {
let leftNum = getGroupNumber(groupKindOrder, left)
let rightNum = getGroupNumber(groupKindOrder, right)
for (let nodes of formattedMembers) {
pairwise(nodes, (left, right) => {
let leftNum = getGroupNumber(groupKindOrder, left)
let rightNum = getGroupNumber(groupKindOrder, right)

if (
(shouldGroupByKind && leftNum > rightNum) ||
((!shouldGroupByKind || leftNum === rightNum) &&
isPositive(compare(left, right, options)))
) {
let sortedNodes = shouldGroupByKind
? groupKindOrder
.map(group => nodes.filter(n => n.group === group))
.map(groupedNodes => sortNodes(groupedNodes, options))
.flat()
: sortNodes(nodes, options)
if (
(shouldGroupByKind && leftNum > rightNum) ||
((!shouldGroupByKind || leftNum === rightNum) &&
isPositive(compare(left, right, options)))
) {
let sortedNodes = shouldGroupByKind
? groupKindOrder
.map(group => nodes.filter(n => n.group === group))
.map(groupedNodes => sortNodes(groupedNodes, options))
.flat()
: sortNodes(nodes, options)

context.report({
messageId: 'unexpectedNamedExportsOrder',
data: {
left: left.name,
right: right.name,
},
node: right.node,
fix: fixer => makeFixes(fixer, nodes, sortedNodes, sourceCode),
})
}
})
context.report({
messageId: 'unexpectedNamedExportsOrder',
data: {
left: left.name,
right: right.name,
},
node: right.node,
fix: fixer =>
makeFixes(fixer, nodes, sortedNodes, sourceCode, {
partitionComment,
}),
})
}
})
}
}
},
}),
Expand Down
Loading

0 comments on commit 928246e

Please sign in to comment.