Skip to content

Commit

Permalink
refactor: use join type for other rules
Browse files Browse the repository at this point in the history
  • Loading branch information
hugop95 authored Dec 14, 2024
1 parent e8dd3e9 commit 7fdadd7
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 142 deletions.
139 changes: 56 additions & 83 deletions rules/sort-classes.types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { JSONSchema4 } from '@typescript-eslint/utils/json-schema'

import type { JoinWithDash } from '../typings'

import {
buildCustomGroupModifiersJsonSchema,
buildCustomGroupSelectorJsonSchema,
Expand Down Expand Up @@ -34,42 +36,18 @@ export type SingleCustomGroup =
| BaseSingleCustomGroup<ConstructorSelector>
| AdvancedSingleCustomGroup<MethodSelector>

export type NonDeclarePropertyGroup = Join<
export type NonDeclarePropertyGroup = JoinWithDash<
[
PublicOrProtectedOrPrivateModifierPrefix,
StaticOrAbstractModifierPrefix,
OverrideModifierPrefix,
ReadonlyModifierPrefix,
DecoratedModifierPrefix,
OptionalModifierPrefix,
PublicOrProtectedOrPrivateModifier,
StaticOrAbstractModifier,
OverrideModifier,
ReadonlyModifier,
DecoratedModifier,
OptionalModifier,
PropertySelector,
]
>

export type FunctionPropertyGroup = Join<
[
PublicOrProtectedOrPrivateModifierPrefix,
StaticModifierPrefix,
OverrideModifierPrefix,
ReadonlyModifierPrefix,
DecoratedModifierPrefix,
AsyncModifierPrefix,
FunctionPropertySelector,
]
>

export type MethodGroup = Join<
[
PublicOrProtectedOrPrivateModifierPrefix,
StaticOrAbstractModifierPrefix,
OverrideModifierPrefix,
DecoratedModifierPrefix,
AsyncModifierPrefix,
OptionalModifierPrefix,
MethodSelector,
]
>

export type Selector =
| AccessorPropertySelector
| FunctionPropertySelector
Expand All @@ -81,24 +59,27 @@ export type Selector =
| PropertySelector
| MethodSelector

export type DeclarePropertyGroup = Join<
export type FunctionPropertyGroup = JoinWithDash<
[
DeclareModifierPrefix,
PublicOrProtectedOrPrivateModifierPrefix,
StaticOrAbstractModifierPrefix,
ReadonlyModifierPrefix,
OptionalModifierPrefix,
PropertySelector,
PublicOrProtectedOrPrivateModifier,
StaticModifier,
OverrideModifier,
ReadonlyModifier,
DecoratedModifier,
AsyncModifier,
FunctionPropertySelector,
]
>

export type GetMethodOrSetMethodGroup = Join<
export type MethodGroup = JoinWithDash<
[
PublicOrProtectedOrPrivateModifierPrefix,
StaticOrAbstractModifierPrefix,
OverrideModifierPrefix,
DecoratedModifierPrefix,
GetMethodOrSetMethodSelector,
PublicOrProtectedOrPrivateModifier,
StaticOrAbstractModifier,
OverrideModifier,
DecoratedModifier,
AsyncModifier,
OptionalModifier,
MethodSelector,
]
>

Expand All @@ -113,22 +94,43 @@ export type Modifier =
| StaticModifier
| AsyncModifier

export type AccessorPropertyGroup = Join<
export type DeclarePropertyGroup = JoinWithDash<
[
DeclareModifier,
PublicOrProtectedOrPrivateModifier,
StaticOrAbstractModifier,
ReadonlyModifier,
OptionalModifier,
PropertySelector,
]
>

export type GetMethodOrSetMethodGroup = JoinWithDash<
[
PublicOrProtectedOrPrivateModifier,
StaticOrAbstractModifier,
OverrideModifier,
DecoratedModifier,
GetMethodOrSetMethodSelector,
]
>

export type AccessorPropertyGroup = JoinWithDash<
[
PublicOrProtectedOrPrivateModifierPrefix,
StaticOrAbstractModifierPrefix,
OverrideModifierPrefix,
DecoratedModifierPrefix,
PublicOrProtectedOrPrivateModifier,
StaticOrAbstractModifier,
OverrideModifier,
DecoratedModifier,
AccessorPropertySelector,
]
>

export type IndexSignatureGroup = Join<
[StaticModifierPrefix, ReadonlyModifierPrefix, IndexSignatureSelector]
export type IndexSignatureGroup = JoinWithDash<
[StaticModifier, ReadonlyModifier, IndexSignatureSelector]
>

export type ConstructorGroup = Join<
[PublicOrProtectedOrPrivateModifierPrefix, ConstructorSelector]
export type ConstructorGroup = JoinWithDash<
[PublicOrProtectedOrPrivateModifier, ConstructorSelector]
>

export interface AnyOfCustomGroup {
Expand Down Expand Up @@ -219,17 +221,6 @@ type AdvancedSingleCustomGroup<T extends Selector> = {
elementNamePattern?: string
} & BaseSingleCustomGroup<T>

type Join<T extends string[]> = T extends [
infer First extends string,
...infer Rest extends string[],
]
? `${First}${Join<Rest>}`
: ''

type PublicOrProtectedOrPrivateModifierPrefix = WithDashSuffixOrEmpty<
ProtectedModifier | PrivateModifier | PublicModifier
>

interface BaseSingleCustomGroup<T extends Selector> {
modifiers?: AllowedModifiersPerSelector[T][]
selector?: T
Expand All @@ -240,27 +231,9 @@ type PublicOrProtectedOrPrivateModifier =
| PrivateModifier
| PublicModifier

type StaticOrAbstractModifierPrefix = WithDashSuffixOrEmpty<
AbstractModifier | StaticModifier
>

type GetMethodOrSetMethodSelector = GetMethodSelector | SetMethodSelector

type DecoratedModifierPrefix = WithDashSuffixOrEmpty<DecoratedModifier>

type OverrideModifierPrefix = WithDashSuffixOrEmpty<OverrideModifier>

type OptionalModifierPrefix = WithDashSuffixOrEmpty<OptionalModifier>

type ReadonlyModifierPrefix = WithDashSuffixOrEmpty<ReadonlyModifier>

type DeclareModifierPrefix = WithDashSuffixOrEmpty<DeclareModifier>

type StaticModifierPrefix = WithDashSuffixOrEmpty<StaticModifier>

type AsyncModifierPrefix = WithDashSuffixOrEmpty<AsyncModifier>

type WithDashSuffixOrEmpty<T extends string> = `${T}-` | ''
type StaticOrAbstractModifier = AbstractModifier | StaticModifier

type FunctionPropertySelector = 'function-property'

Expand Down
52 changes: 23 additions & 29 deletions rules/sort-modules.types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { JSONSchema4 } from '@typescript-eslint/utils/json-schema'

import type { JoinWithDash } from '../typings'

import {
buildCustomGroupModifiersJsonSchema,
buildCustomGroupSelectorJsonSchema,
Expand Down Expand Up @@ -92,34 +94,34 @@ type Group =
| 'unknown'
| string

type NonDefaultClassGroup =
`${ExportModifierPrefix}${DeclareModifierPrefix}${DecoratedModifierPrefix}${ClassSelector}`

type DefaultFunctionGroup =
`${ExportModifierPrefix}${DefaultModifierPrefix}${AsyncModifierPrefix}${FunctionSelector}`

type DefaultClassGroup =
`${ExportModifierPrefix}${DefaultModifierPrefix}${DecoratedModifierPrefix}${ClassSelector}`

interface BaseSingleCustomGroup<T extends Selector> {
modifiers?: AllowedModifiersPerSelector[T][]
selector?: T
}

type NonDefaultInterfaceGroup =
`${ExportModifierPrefix}${DeclareModifierPrefix}${InterfaceSelector}`
type NonDefaultClassGroup = JoinWithDash<
[ExportModifier, DeclareModifier, DecoratedModifier, ClassSelector]
>

type DefaultFunctionGroup = JoinWithDash<
[ExportModifier, DefaultModifier, AsyncModifier, FunctionSelector]
>

type NonDefaultFunctionGroup =
`${ExportModifierPrefix}${DeclareModifierPrefix}${FunctionSelector}`
type DefaultClassGroup = JoinWithDash<
[ExportModifier, DefaultModifier, DecoratedModifier, ClassSelector]
>

type DefaultInterfaceGroup =
`${ExportModifierPrefix}${DefaultModifierPrefix}${InterfaceSelector}`
type NonDefaultInterfaceGroup = JoinWithDash<
[ExportModifier, DeclareModifier, InterfaceSelector]
>

type TypeGroup =
`${ExportModifierPrefix}${DeclareModifierPrefix}${TypeSelector}`
type NonDefaultFunctionGroup = JoinWithDash<
[ExportModifier, DeclareModifier, FunctionSelector]
>

type EnumGroup =
`${ExportModifierPrefix}${DeclareModifierPrefix}${EnumSelector}`
type DefaultInterfaceGroup = JoinWithDash<
[ExportModifier, DefaultModifier, InterfaceSelector]
>

interface DecoratorNamePatternFilterCustomGroup {
decoratorNamePattern?: string
Expand All @@ -129,17 +131,9 @@ interface ElementNamePatternFilterCustomGroup {
elementNamePattern?: string
}

type DecoratedModifierPrefix = WithDashSuffixOrEmpty<DecoratedModifier>

type DeclareModifierPrefix = WithDashSuffixOrEmpty<DeclareModifier>

type DefaultModifierPrefix = WithDashSuffixOrEmpty<DefaultModifier>

type ExportModifierPrefix = WithDashSuffixOrEmpty<ExportModifier>

type AsyncModifierPrefix = WithDashSuffixOrEmpty<AsyncModifier>
type TypeGroup = JoinWithDash<[ExportModifier, DeclareModifier, TypeSelector]>

type WithDashSuffixOrEmpty<T extends string> = `${T}-` | ''
type EnumGroup = JoinWithDash<[ExportModifier, DeclareModifier, EnumSelector]>

type DecoratedModifier = 'decorated'

Expand Down
45 changes: 26 additions & 19 deletions rules/sort-object-types.types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import type { JSONSchema4 } from '@typescript-eslint/utils/json-schema'

import type { JoinWithDash } from '../typings'

import {
buildCustomGroupModifiersJsonSchema,
buildCustomGroupSelectorJsonSchema,
Expand Down Expand Up @@ -73,8 +75,14 @@ type CustomGroup = (
groupName: string
}

type IndexSignatureGroup =
`${OptionalModifierPrefix | RequiredModifierPrefix}${MultilineModifierPrefix}${IndexSignatureSelector}`
type IndexSignatureGroup = JoinWithDash<
[
OptionalModifier,
RequiredModifier,
MultilineModifier,
IndexSignatureSelector,
]
>

/**
* Only used in code, so I don't know if it's worth maintaining this.
Expand All @@ -88,35 +96,34 @@ type Group =
| 'unknown'
| string

type PropertyGroup =
`${OptionalModifierPrefix | RequiredModifierPrefix}${MultilineModifierPrefix}${PropertySelector}`

interface BaseSingleCustomGroup<T extends Selector> {
modifiers?: AllowedModifiersPerSelector[T][]
selector?: T
}

type MemberGroup =
`${OptionalModifierPrefix | RequiredModifierPrefix}${MultilineModifierPrefix}${MemberSelector}`
type PropertyGroup = JoinWithDash<
[OptionalModifier, RequiredModifier, MultilineModifier, PropertySelector]
>

type MemberGroup = JoinWithDash<
[OptionalModifier, RequiredModifier, MultilineModifier, MemberSelector]
>

type MethodGroup =
`${OptionalModifierPrefix | RequiredModifierPrefix}${MultilineModifierPrefix}${MethodSelector}`
type MethodGroup = JoinWithDash<
[OptionalModifier, RequiredModifier, MultilineModifier, MethodSelector]
>

type MultilineGroup =
`${OptionalModifierPrefix | RequiredModifierPrefix}${MultilineSelector}`
/**
* @deprecated For {@link `MultilineModifier`}
*/
type MultilineGroup = JoinWithDash<
[OptionalModifier, RequiredModifier, MultilineSelector]
>

interface ElementNamePatternFilterCustomGroup {
elementNamePattern?: string
}

type MultilineModifierPrefix = WithDashSuffixOrEmpty<MultilineModifier>

type RequiredModifierPrefix = WithDashSuffixOrEmpty<RequiredModifier>

type OptionalModifierPrefix = WithDashSuffixOrEmpty<OptionalModifier>

type WithDashSuffixOrEmpty<T extends string> = `${T}-` | ''

type IndexSignatureSelector = 'index-signature'

/**
Expand Down
13 changes: 2 additions & 11 deletions typings/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,2 @@
import type { TSESTree } from '@typescript-eslint/types'

export interface SortingNode<Node extends TSESTree.Node = TSESTree.Node> {
hasMultipleImportDeclarations?: boolean
addSafetySemicolonWhenInline?: boolean
isEslintDisabled: boolean
group?: string
name: string
size: number
node: Node
}
export type { JoinWithDash } from './join-with-dash'
export type { SortingNode } from './sorting-node'
3 changes: 3 additions & 0 deletions typings/join-with-dash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import type { Join } from './join'

export type JoinWithDash<T extends string[]> = Join<T, '-'>
12 changes: 12 additions & 0 deletions typings/join.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export type Join<T extends string[], Separator extends string> = T extends [
infer First extends string,
...infer Rest extends string[],
]
? Rest extends []
? `${First}`
: `${WithSeparatorOrEmpty<First, Separator>}${Join<Rest, Separator>}`
: never

type WithSeparatorOrEmpty<T extends string, Separator extends string> =
| `${T}${Separator}`
| ''
Loading

0 comments on commit 7fdadd7

Please sign in to comment.