Skip to content

Commit

Permalink
refactor: reduce code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
hugop95 authored Jan 19, 2025
1 parent 231af42 commit e8a1552
Show file tree
Hide file tree
Showing 33 changed files with 667 additions and 1,051 deletions.
51 changes: 14 additions & 37 deletions rules/sort-array-includes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,10 @@ import {
buildCustomGroupsArrayJsonSchema,
partitionByCommentJsonSchema,
partitionByNewLineJsonSchema,
specialCharactersJsonSchema,
newlinesBetweenJsonSchema,
ignoreCaseJsonSchema,
buildTypeJsonSchema,
alphabetJsonSchema,
localesJsonSchema,
commonJsonSchemas,
groupsJsonSchema,
orderJsonSchema,
} from '../utils/common-json-schemas'
import { validateNewlinesAndPartitionConfiguration } from '../utils/validate-newlines-and-partition-configuration'
import { validateGeneratedGroupsConfiguration } from '../utils/validate-generated-groups-configuration'
Expand All @@ -42,11 +38,10 @@ 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 { reportErrors } from '../utils/report-errors'
import { rangeToDiff } from '../utils/range-to-diff'
import { getSettings } from '../utils/get-settings'
import { isSortable } from '../utils/is-sortable'
import { makeFixes } from '../utils/make-fixes'
import { useGroups } from '../utils/use-groups'
import { complete } from '../utils/complete'
import { pairwise } from '../utils/pairwise'
Expand Down Expand Up @@ -86,11 +81,7 @@ export let defaultOptions: Required<Options[0]> = {
export let jsonSchema: JSONSchema4 = {
items: {
properties: {
partitionByComment: {
...partitionByCommentJsonSchema,
description:
'Allows you to use comments to separate the array members into logical groups.',
},
...commonJsonSchemas,
groupKind: {
enum: ['mixed', 'literals-first', 'spreads-first'],
description: 'Specifies top-level groups.',
Expand All @@ -101,14 +92,10 @@ export let jsonSchema: JSONSchema4 = {
}),
useConfigurationIf: buildUseConfigurationIfJsonSchema(),
type: buildTypeJsonSchema({ withUnsorted: true }),
partitionByComment: partitionByCommentJsonSchema,
partitionByNewLine: partitionByNewLineJsonSchema,
specialCharacters: specialCharactersJsonSchema,
newlinesBetween: newlinesBetweenJsonSchema,
ignoreCase: ignoreCaseJsonSchema,
alphabet: alphabetJsonSchema,
locales: localesJsonSchema,
groups: groupsJsonSchema,
order: orderJsonSchema,
},
additionalProperties: false,
type: 'object',
Expand Down Expand Up @@ -377,26 +364,16 @@ export let sortArray = <MessageIds extends string>({
}),
]

for (let messageId of messageIds) {
context.report({
fix: fixer =>
makeFixes({
sortedNodes: sortedNodesExcludingEslintDisabled,
sourceCode,
options,
fixer,
nodes,
}),
data: {
right: toSingleLine(right.name),
left: toSingleLine(left.name),
rightGroup: right.group,
leftGroup: left.group,
},
node: right.node,
messageId,
})
}
reportErrors({
sortedNodes: sortedNodesExcludingEslintDisabled,
sourceCode,
messageIds,
options,
context,
nodes,
right,
left,
})
})
}
}
Expand Down
54 changes: 23 additions & 31 deletions rules/sort-array-includes/types.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,33 @@
import type { JSONSchema4 } from '@typescript-eslint/utils/json-schema'

import type {
PartitionByCommentOption,
CommonOptions,
GroupsOptions,
} from '../../types/common-options'

import {
buildCustomGroupSelectorJsonSchema,
elementNamePatternJsonSchema,
} from '../../utils/common-json-schemas'

export type Options = Partial<{
partitionByComment:
| {
block?: string[] | boolean | string
line?: string[] | boolean | string
}
| string[]
| boolean
| string
groups: (
| { newlinesBetween: 'ignore' | 'always' | 'never' }
| Group[]
| Group
)[]
type: 'alphabetical' | 'line-length' | 'unsorted' | 'natural' | 'custom'
useConfigurationIf: {
allNamesMatchPattern?: string
}
/**
* @deprecated for {@link `groups`}
*/
groupKind: 'literals-first' | 'spreads-first' | 'mixed'
newlinesBetween: 'ignore' | 'always' | 'never'
specialCharacters: 'remove' | 'trim' | 'keep'
locales: NonNullable<Intl.LocalesArgument>
customGroups: CustomGroup[]
partitionByNewLine: boolean
order: 'desc' | 'asc'
ignoreCase: boolean
alphabet: string
}>[]
export type Options = Partial<
{
type: 'alphabetical' | 'line-length' | 'unsorted' | 'natural' | 'custom'
useConfigurationIf: {
allNamesMatchPattern?: string
}
/**
* @deprecated for {@link `groups`}
*/
groupKind: 'literals-first' | 'spreads-first' | 'mixed'
newlinesBetween: 'ignore' | 'always' | 'never'
partitionByComment: PartitionByCommentOption
groups: GroupsOptions<Group>
customGroups: CustomGroup[]
partitionByNewLine: boolean
} & CommonOptions
>[]

export interface SingleCustomGroup {
elementNamePattern?: string
Expand Down
54 changes: 15 additions & 39 deletions rules/sort-classes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { TSESTree } from '@typescript-eslint/types'
import type { TSESLint } from '@typescript-eslint/utils'

import type {
SortClassesOptions,
Expand All @@ -12,14 +11,10 @@ import {
buildCustomGroupsArrayJsonSchema,
partitionByCommentJsonSchema,
partitionByNewLineJsonSchema,
specialCharactersJsonSchema,
newlinesBetweenJsonSchema,
ignoreCaseJsonSchema,
buildTypeJsonSchema,
alphabetJsonSchema,
localesJsonSchema,
commonJsonSchemas,
groupsJsonSchema,
orderJsonSchema,
} from '../utils/common-json-schemas'
import {
getFirstUnorderedNodeDependentOn,
Expand Down Expand Up @@ -47,11 +42,10 @@ 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 { reportErrors } from '../utils/report-errors'
import { rangeToDiff } from '../utils/range-to-diff'
import { getSettings } from '../utils/get-settings'
import { isSortable } from '../utils/is-sortable'
import { makeFixes } from '../utils/make-fixes'
import { useGroups } from '../utils/use-groups'
import { complete } from '../utils/complete'
import { pairwise } from '../utils/pairwise'
Expand Down Expand Up @@ -649,34 +643,25 @@ export default createEslintRule<SortClassesOptions, MESSAGE_ID>({
}),
]

for (let messageId of messageIds) {
context.report({
data: {
nodeDependentOnRight: firstUnorderedNodeDependentOnRight?.name,
right: toSingleLine(right.name),
left: toSingleLine(left.name),
rightGroup: right.group,
leftGroup: left.group,
},
fix: (fixer: TSESLint.RuleFixer) =>
makeFixes({
sortedNodes: sortedNodesExcludingEslintDisabled,
sourceCode,
options,
fixer,
nodes,
}),
node: right.node,
messageId,
})
}
reportErrors({
sortedNodes: sortedNodesExcludingEslintDisabled,
firstUnorderedNodeDependentOnRight,
sourceCode,
messageIds,
options,
context,
nodes,
right,
left,
})
})
},
}),
meta: {
schema: [
{
properties: {
...commonJsonSchemas,
ignoreCallbackDependenciesPatterns: {
description:
'Patterns that should be ignored when detecting dependencies in method callbacks.',
Expand All @@ -685,23 +670,14 @@ export default createEslintRule<SortClassesOptions, MESSAGE_ID>({
},
type: 'array',
},
partitionByComment: {
...partitionByCommentJsonSchema,
description:
'Allows to use comments to separate the class members into logical groups.',
},
customGroups: buildCustomGroupsArrayJsonSchema({
singleCustomGroupJsonSchema,
}),
partitionByComment: partitionByCommentJsonSchema,
partitionByNewLine: partitionByNewLineJsonSchema,
specialCharacters: specialCharactersJsonSchema,
newlinesBetween: newlinesBetweenJsonSchema,
ignoreCase: ignoreCaseJsonSchema,
alphabet: alphabetJsonSchema,
type: buildTypeJsonSchema(),
locales: localesJsonSchema,
groups: groupsJsonSchema,
order: orderJsonSchema,
},
additionalProperties: false,
type: 'object',
Expand Down
47 changes: 19 additions & 28 deletions rules/sort-classes/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import type { JSONSchema4 } from '@typescript-eslint/utils/json-schema'

import type {
PartitionByCommentOption,
CommonOptions,
GroupsOptions,
} from '../../types/common-options'
import type { JoinWithDash } from '../../types/join-with-dash'

import {
Expand All @@ -9,34 +14,6 @@ import {
elementNamePatternJsonSchema,
} from '../../utils/common-json-schemas'

export type SortClassesOptions = [
Partial<{
partitionByComment:
| {
block?: string[] | boolean | string
line?: string[] | boolean | string
}
| string[]
| boolean
| string
groups: (
| { newlinesBetween: 'ignore' | 'always' | 'never' }
| Group[]
| Group
)[]
type: 'alphabetical' | 'line-length' | 'natural' | 'custom'
newlinesBetween: 'ignore' | 'always' | 'never'
specialCharacters: 'remove' | 'trim' | 'keep'
ignoreCallbackDependenciesPatterns: string[]
locales: NonNullable<Intl.LocalesArgument>
partitionByNewLine: boolean
customGroups: CustomGroup[]
order: 'desc' | 'asc'
ignoreCase: boolean
alphabet: string
}>,
]

export type SingleCustomGroup =
| AdvancedSingleCustomGroup<FunctionPropertySelector>
| AdvancedSingleCustomGroup<AccessorPropertySelector>
Expand All @@ -48,6 +25,20 @@ export type SingleCustomGroup =
| BaseSingleCustomGroup<ConstructorSelector>
| AdvancedSingleCustomGroup<MethodSelector>

export type SortClassesOptions = [
Partial<
{
type: 'alphabetical' | 'line-length' | 'natural' | 'custom'
newlinesBetween: 'ignore' | 'always' | 'never'
partitionByComment: PartitionByCommentOption
ignoreCallbackDependenciesPatterns: string[]
groups: GroupsOptions<Group>
partitionByNewLine: boolean
customGroups: CustomGroup[]
} & CommonOptions
>,
]

export type CustomGroup = (
| {
order?: SortClassesOptions[0]['order']
Expand Down
Loading

0 comments on commit e8a1552

Please sign in to comment.