Skip to content

Commit

Permalink
feat(sort-object-types): adds newlinesBetween
Browse files Browse the repository at this point in the history
  • Loading branch information
hugop95 committed Oct 15, 2024
1 parent 45e82ac commit 43a2c6c
Showing 1 changed file with 58 additions and 11 deletions.
69 changes: 58 additions & 11 deletions rules/sort-object-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ import { validateGroupsConfiguration } from '../utils/validate-groups-configurat
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'
import { getSourceCode } from '../utils/get-source-code'
import { toSingleLine } from '../utils/to-single-line'
import { rangeToDiff } from '../utils/range-to-diff'
import { getSettings } from '../utils/get-settings'
import { makeFixes } from '../utils/make-fixes'
Expand All @@ -20,6 +21,8 @@ import { pairwise } from '../utils/pairwise'

type MESSAGE_ID =
| 'unexpectedObjectTypesGroupOrder'
| 'missedSpacingBetweenObjectTypes'
| 'extraSpacingBetweenObjectTypes'
| 'unexpectedObjectTypesOrder'

type Group<T extends string[]> = 'multiline' | 'unknown' | T[number] | 'method'
Expand All @@ -30,6 +33,7 @@ type Options<T extends string[]> = [
customGroups: { [key in T[number]]: string[] | string }
type: 'alphabetical' | 'line-length' | 'natural'
partitionByComment: string[] | boolean | string
newlinesBetween: 'ignore' | 'always' | 'never'
specialCharacters: 'remove' | 'trim' | 'keep'
groups: (Group<T>[] | Group<T>)[]
matcher: 'minimatch' | 'regex'
Expand Down Expand Up @@ -103,6 +107,12 @@ export default createEslintRule<Options<string[]>, 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 import groups.',
enum: ['ignore', 'always', 'never'],
type: 'string',
},
groupKind: {
description: 'Specifies top-level groups.',
type: 'string',
Expand Down Expand Up @@ -151,6 +161,10 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
'Expected "{{right}}" ({{rightGroup}}) to come before "{{left}}" ({{leftGroup}}).',
unexpectedObjectTypesOrder:
'Expected "{{right}}" to come before "{{left}}".',
missedSpacingBetweenObjectTypes:
'Missed spacing between "{{left}}" and "{{right}}" types.',
extraSpacingBetweenObjectTypes:
'Extra spacing between "{{left}}" and "{{right}}" types.',
},
},
defaultOptions: [
Expand All @@ -160,6 +174,7 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
ignoreCase: true,
specialCharacters: 'keep',
matcher: 'minimatch',
newlinesBetween: 'never',
partitionByComment: false,
partitionByNewLine: false,
groupKind: 'mixed',
Expand All @@ -179,6 +194,7 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
groupKind: 'mixed',
matcher: 'minimatch',
ignoreCase: true,
newlinesBetween: 'never',
specialCharacters: 'keep',
customGroups: {},
order: 'asc',
Expand Down Expand Up @@ -313,25 +329,56 @@ export default createEslintRule<Options<string[]>, MESSAGE_ID>({
}

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
? 'unexpectedObjectTypesGroupOrder'
: 'unexpectedObjectTypesOrder',
)
}

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

for (let messageId of messageIds) {
context.report({
messageId:
leftNum !== rightNum
? 'unexpectedObjectTypesGroupOrder'
: 'unexpectedObjectTypesOrder',
messageId,
data: {
left: toSingleLine(left.name),
left: left.name,
leftGroup: left.group,
right: toSingleLine(right.name),
right: 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 43a2c6c

Please sign in to comment.