Skip to content

Commit

Permalink
feat(sort-union-types): adds newlinesBetween
Browse files Browse the repository at this point in the history
  • Loading branch information
hugop95 committed Oct 18, 2024
1 parent da784e7 commit 9f8addb
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 11 deletions.
18 changes: 16 additions & 2 deletions docs/content/rules/sort-union-types.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,18 @@ type CarBrand =
Each group of union types (separated by empty lines) is treated independently, and the order within each group is preserved.
### newlinesBetween
<sub>default: `'ignore'`</sub>
Specifies how new lines should be handled between union type groups.
- `ignore` — Do not report errors related to new lines between union type groups.
- `always` — Enforce one new line between each group, and forbid new lines inside a group.
- `never` — No new lines are allowed in union types.
This options is only applicable when `partitionByNewLine` is `false`.
### groups
<sub>
Expand Down Expand Up @@ -314,8 +326,9 @@ Determines the matcher used for patterns in the `partitionByComment` option.
order: 'asc',
ignoreCase: true,
specialCharacters: 'keep',
partitionByNewLine: false,
partitionByComment: false,
partitionByNewLine: false,
newlinesBetween: 'ignore',
matcher: 'minimatch',
groups: [],
},
Expand All @@ -342,8 +355,9 @@ Determines the matcher used for patterns in the `partitionByComment` option.
order: 'asc',
ignoreCase: true,
specialCharacters: 'keep',
partitionByNewLine: false,
partitionByComment: false,
partitionByNewLine: false,
newlinesBetween: 'ignore',
matcher: 'minimatch',
groups: [],
},
Expand Down
70 changes: 61 additions & 9 deletions rules/sort-union-types.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import type { SortingNode } from '../typings'

import { validateNewlinesAndPartitionConfiguration } from '../utils/validate-newlines-and-partition-configuration'
import { validateGroupsConfiguration } from '../utils/validate-groups-configuration'
import { hasPartitionComment } from '../utils/is-partition-comment'
import { sortNodesByGroups } from '../utils/sort-nodes-by-groups'
import { getCommentsBefore } from '../utils/get-comments-before'
import { makeNewlinesFixes } from '../utils/make-newlines-fixes'
import { getNewlinesErrors } from '../utils/get-newlines-errors'
import { createEslintRule } from '../utils/create-eslint-rule'
import { getLinesBetween } from '../utils/get-lines-between'
import { getGroupNumber } from '../utils/get-group-number'
Expand All @@ -16,7 +19,11 @@ import { makeFixes } from '../utils/make-fixes'
import { complete } from '../utils/complete'
import { pairwise } from '../utils/pairwise'

type MESSAGE_ID = 'unexpectedUnionTypesGroupOrder' | 'unexpectedUnionTypesOrder'
type MESSAGE_ID =
| 'missedSpacingBetweenUnionTypes'
| 'unexpectedUnionTypesGroupOrder'
| 'extraSpacingBetweenUnionTypes'
| 'unexpectedUnionTypesOrder'

type Group =
| 'intersection'
Expand All @@ -37,6 +44,7 @@ type Options = [
Partial<{
type: 'alphabetical' | 'line-length' | 'natural'
partitionByComment: string[] | boolean | string
newlinesBetween: 'ignore' | 'always' | 'never'
specialCharacters: 'remove' | 'trim' | 'keep'
matcher: 'minimatch' | 'regex'
groups: (Group[] | Group)[]
Expand Down Expand Up @@ -125,6 +133,12 @@ export default createEslintRule<Options, MESSAGE_ID>({
'Allows to use spaces to separate the nodes into logical groups.',
type: 'boolean',
},
newlinesBetween: {
description:
'Specifies how new lines should be handled between object types groups.',
enum: ['ignore', 'always', 'never'],
type: 'string',
},
},
additionalProperties: false,
},
Expand All @@ -134,6 +148,10 @@ export default createEslintRule<Options, MESSAGE_ID>({
'Expected "{{right}}" ({{rightGroup}}) to come before "{{left}}" ({{leftGroup}}).',
unexpectedUnionTypesOrder:
'Expected "{{right}}" to come before "{{left}}".',
missedSpacingBetweenUnionTypes:
'Missed spacing between "{{left}}" and "{{right}}" types.',
extraSpacingBetweenUnionTypes:
'Extra spacing between "{{left}}" and "{{right}}" types.',
},
},
defaultOptions: [
Expand All @@ -159,6 +177,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
order: 'asc',
groups: [],
matcher: 'minimatch',
newlinesBetween: 'ignore',
partitionByNewLine: false,
partitionByComment: false,
} as const)
Expand All @@ -182,6 +201,7 @@ export default createEslintRule<Options, MESSAGE_ID>({
],
[],
)
validateNewlinesAndPartitionConfiguration(options)

let sourceCode = getSourceCode(context)
let partitionComment = options.partitionByComment
Expand Down Expand Up @@ -277,26 +297,58 @@ export default createEslintRule<Options, MESSAGE_ID>({

for (let nodes of formattedMembers) {
let sortedNodes = sortNodesByGroups(nodes, options)

pairwise(nodes, (left, right) => {
let leftNum = getGroupNumber(options.groups, left)
let rightNum = getGroupNumber(options.groups, right)

let indexOfLeft = sortedNodes.indexOf(left)
let indexOfRight = sortedNodes.indexOf(right)

let messageIds: MESSAGE_ID[] = []

if (indexOfLeft > indexOfRight) {
let leftNum = getGroupNumber(options.groups, left)
let rightNum = getGroupNumber(options.groups, right)
messageIds.push(
leftNum !== rightNum
? 'unexpectedUnionTypesGroupOrder'
: 'unexpectedUnionTypesOrder',
)
}

messageIds = [
...messageIds,
...getNewlinesErrors({
left,
leftNum,
right,
rightNum,
sourceCode,
missedSpacingError: 'missedSpacingBetweenUnionTypes',
extraSpacingError: 'extraSpacingBetweenUnionTypes',
options,
}),
]

for (let messageId of messageIds) {
context.report({
messageId:
leftNum !== rightNum
? 'unexpectedUnionTypesGroupOrder'
: 'unexpectedUnionTypesOrder',
messageId,
data: {
left: toSingleLine(left.name),
leftGroup: left.group,
right: toSingleLine(right.name),
rightGroup: right.group,
},
node: right.node,
fix: fixer =>
makeFixes(fixer, nodes, sortedNodes, sourceCode, options),
fix: fixer => [
...makeFixes(fixer, nodes, sortedNodes, sourceCode, options),
...makeNewlinesFixes(
fixer,
nodes,
sortedNodes,
sourceCode,
options,
),
],
})
}
})
Expand Down

0 comments on commit 9f8addb

Please sign in to comment.