Skip to content

Commit

Permalink
feat: add partition by new line option in sort-enums
Browse files Browse the repository at this point in the history
  • Loading branch information
hugop95 authored Sep 23, 2024
1 parent 69b643e commit defd370
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 42 deletions.
29 changes: 27 additions & 2 deletions docs/content/rules/sort-enums.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,29 @@ Allows you to use comments to separate the members of enums into logical groups.

- `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 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 an enum if there is an empty line between them. This can be useful for keeping logically separated groups of members in their defined order.

```ts
enum Enum {
// Group 1
C = 'C',
D = 'D',

// Group 2
B = 'B',

// Group 3
A = 'A',
E = 'E',
}
```

## Usage

Expand All @@ -164,7 +186,9 @@ Allows you to use comments to separate the members of enums into logical groups.
order: 'asc',
ignoreCase: true,
partitionByComment: false,
sortByValue: false
partitionByNewLine: false,
sortByValue: false,
forceNumericSort: false
},
],
},
Expand All @@ -189,6 +213,7 @@ Allows you to use comments to separate the members of enums into logical groups.
order: 'asc',
ignoreCase: true,
partitionByComment: false,
partitionByNewLine: false,
sortByValue: false,
forceNumericSort: false
},
Expand Down
44 changes: 28 additions & 16 deletions rules/sort-enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
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 @@ -25,6 +26,7 @@ export type Options = [
Partial<{
type: 'alphabetical' | 'line-length' | 'natural'
partitionByComment: string[] | boolean | string
partitionByNewLine: boolean
forceNumericSort: boolean
order: 'desc' | 'asc'
sortByValue: boolean
Expand Down Expand Up @@ -87,6 +89,11 @@ export default createEslintRule<Options, MESSAGE_ID>({
},
],
},
partitionByNewLine: {
description:
'Allows to use spaces to separate the nodes into logical groups.',
type: 'boolean',
},
},
additionalProperties: false,
},
Expand All @@ -104,6 +111,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
ignoreCase: true,
sortByValue: false,
partitionByComment: false,
partitionByNewLine: false,
forceNumericSort: false,
},
],
Expand All @@ -122,6 +130,7 @@ export default createEslintRule<Options, MESSAGE_ID>({

let options = complete(context.options.at(0), settings, {
partitionByComment: false,
partitionByNewLine: false,
type: 'alphabetical',
ignoreCase: true,
order: 'asc',
Expand Down Expand Up @@ -181,34 +190,37 @@ export default createEslintRule<Options, MESSAGE_ID>({

let formattedMembers: SortingNodeWithDependencies[][] = members.reduce(
(accumulator: SortingNodeWithDependencies[][], member) => {
let comments = getCommentsBefore(member, sourceCode)

if (
partitionComment &&
hasPartitionComment(partitionComment, comments)
) {
accumulator.push([])
}

let name =
member.id.type === 'Literal'
? `${member.id.value}`
: `${sourceCode.text.slice(...member.id.range)}`

let dependencies: string[] = []
if (member.initializer) {
dependencies = extractDependencies(
member.initializer,
node.id.name,
)
}

let lastSortingNode = accumulator.at(-1)?.at(-1)
let sortingNode: SortingNodeWithDependencies = {
size: rangeToDiff(member.range),
node: member,
dependencies,
name,
name:
member.id.type === 'Literal'
? `${member.id.value}`
: `${sourceCode.text.slice(...member.id.range)}`,
}

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

accumulator.at(-1)!.push(sortingNode)
return accumulator
},
Expand Down
104 changes: 80 additions & 24 deletions test/sort-enums.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1977,12 +1977,15 @@ describe(ruleName, () => {
)
})

describe('handles complex comment cases', () => {
ruleTester.run(`keeps comments associated to their node`, rule, {
valid: [],
invalid: [
{
code: dedent`
describe(`${ruleName}: handles complex comment cases`, () => {
ruleTester.run(
`${ruleName}: keeps comments associated to their node`,
rule,
{
valid: [],
invalid: [
{
code: dedent`
enum Enum {
// Ignore this comment
Expand All @@ -2002,7 +2005,7 @@ describe(ruleName, () => {
A = 'A',
}
`,
output: dedent`
output: dedent`
enum Enum {
// Ignore this comment
Expand All @@ -2022,25 +2025,26 @@ describe(ruleName, () => {
B = 'B',
}
`,
options: [
{
type: 'alphabetical',
},
],
errors: [
{
messageId: 'unexpectedEnumsOrder',
data: {
left: 'B',
right: 'A',
options: [
{
type: 'alphabetical',
},
},
],
},
],
})
],
errors: [
{
messageId: 'unexpectedEnumsOrder',
data: {
left: 'B',
right: 'A',
},
},
],
},
],
},
)

ruleTester.run(`handles partition comments`, rule, {
ruleTester.run(`${ruleName}: handles partition comments`, rule, {
valid: [],
invalid: [
{
Expand Down Expand Up @@ -2120,5 +2124,57 @@ describe(ruleName, () => {
],
})
})

ruleTester.run(`${ruleName}: allows to use new line as partition`, rule, {
valid: [],
invalid: [
{
code: dedent`
enum Enum {
D = 'D',
C = 'C',
B = 'B',
E = 'E',
A = 'A',
}
`,
output: dedent`
enum Enum {
C = 'C',
D = 'D',
B = 'B',
A = 'A',
E = 'E',
}
`,
options: [
{
type: 'alphabetical',
partitionByNewLine: true,
},
],
errors: [
{
messageId: 'unexpectedEnumsOrder',
data: {
left: 'D',
right: 'C',
},
},
{
messageId: 'unexpectedEnumsOrder',
data: {
left: 'E',
right: 'A',
},
},
],
},
],
})
})
})

0 comments on commit defd370

Please sign in to comment.