Skip to content

Commit

Permalink
feat: add partition by comment and partition by new line in sort-maps
Browse files Browse the repository at this point in the history
  • Loading branch information
hugop95 authored Sep 22, 2024
1 parent 928246e commit 7bf6756
Show file tree
Hide file tree
Showing 3 changed files with 321 additions and 22 deletions.
41 changes: 41 additions & 0 deletions docs/content/rules/sort-maps.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,43 @@ Controls whether sorting should be case-sensitive or not.
- `true` — Ignore case when sorting alphabetically or naturally (e.g., “A” and “a” are the same).
- `false` — Consider case when sorting (e.g., “A” comes before “a”).

### partitionByComment

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

Allows you to use comments to separate the members of maps 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 a map if there is an empty line between them. This can be useful for keeping logically separated groups of members in their defined order.

```ts
new Map([
// Group 1
['Drone', 0],
['Keyboard', 1],
['Mouse', 3],
['Smartphone', 4],

// Group 2
['Laptop', 5],
['Monitor', 6],
['Smartwatch', 7],
['Tablet', 8],

// Group 3
['Headphones', 9],
['Router', 10],
])
```

## Usage

<CodeTabs
Expand All @@ -128,6 +165,8 @@ Controls whether sorting should be case-sensitive or not.
type: 'alphabetical',
order: 'asc',
ignoreCase: true,
partitionByNewLine: false,
partitionByComment: false,
},
],
},
Expand All @@ -151,6 +190,8 @@ Controls whether sorting should be case-sensitive or not.
type: 'alphabetical',
order: 'asc',
ignoreCase: true,
partitionByNewLine: false,
partitionByComment: false,
},
],
},
Expand Down
99 changes: 77 additions & 22 deletions rules/sort-maps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import type { TSESTree } from '@typescript-eslint/types'

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 { getSourceCode } from '../utils/get-source-code'
import { toSingleLine } from '../utils/to-single-line'
import { rangeToDiff } from '../utils/range-to-diff'
Expand All @@ -19,6 +22,8 @@ type MESSAGE_ID = 'unexpectedMapElementsOrder'
type Options = [
Partial<{
type: 'alphabetical' | 'line-length' | 'natural'
partitionByComment: string[] | boolean | string
partitionByNewLine: boolean
order: 'desc' | 'asc'
ignoreCase: boolean
}>,
Expand Down Expand Up @@ -52,6 +57,29 @@ export default createEslintRule<Options, MESSAGE_ID>({
'Controls whether sorting should be case-sensitive or not.',
type: 'boolean',
},
partitionByComment: {
description:
'Allows you to use comments to separate the maps 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 @@ -66,6 +94,8 @@ export default createEslintRule<Options, MESSAGE_ID>({
type: 'alphabetical',
order: 'asc',
ignoreCase: true,
partitionByComment: false,
partitionByNewLine: false,
},
],
create: context => ({
Expand All @@ -85,9 +115,12 @@ export default createEslintRule<Options, MESSAGE_ID>({
type: 'alphabetical',
ignoreCase: true,
order: 'asc',
partitionByComment: false,
partitionByNewLine: false,
} as const)

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

let parts: TSESTree.Expression[][] = elements.reduce(
(
Expand All @@ -105,7 +138,8 @@ export default createEslintRule<Options, MESSAGE_ID>({
)

for (let part of parts) {
let nodes: SortingNode[] = part.map(element => {
let formattedMembers: SortingNode[][] = [[]]
for (let element of part) {
let name: string

if (element.type === 'ArrayExpression') {
Expand All @@ -122,32 +156,53 @@ export default createEslintRule<Options, MESSAGE_ID>({
name = sourceCode.text.slice(...element.range)
}

return {
let lastSortingNode = formattedMembers.at(-1)?.at(-1)
let sortingNode: SortingNode = {
size: rangeToDiff(element.range),
node: element,
name,
}
})

pairwise(nodes, (left, right) => {
if (isPositive(compare(left, right, options))) {
context.report({
messageId: 'unexpectedMapElementsOrder',
data: {
left: toSingleLine(left.name),
right: toSingleLine(right.name),
},
node: right.node,
fix: fixer =>
makeFixes(
fixer,
nodes,
sortNodes(nodes, options),
sourceCode,
),
})

if (
(partitionComment &&
hasPartitionComment(
partitionComment,
getCommentsBefore(element, sourceCode),
)) ||
(options.partitionByNewLine &&
lastSortingNode &&
getLinesBetween(sourceCode, lastSortingNode, sortingNode))
) {
formattedMembers.push([])
}
})

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

for (let nodes of formattedMembers) {
pairwise(nodes, (left, right) => {
if (isPositive(compare(left, right, options))) {
context.report({
messageId: 'unexpectedMapElementsOrder',
data: {
left: toSingleLine(left.name),
right: toSingleLine(right.name),
},
node: right.node,
fix: fixer =>
makeFixes(
fixer,
nodes,
sortNodes(nodes, options),
sourceCode,
{
partitionComment,
},
),
})
}
})
}
}
}
}
Expand Down
Loading

0 comments on commit 7bf6756

Please sign in to comment.