From 7fdadd7dcdf96fc259973fad79071d54ceac060f Mon Sep 17 00:00:00 2001 From: Hugo <60015232+hugop95@users.noreply.github.com> Date: Sat, 14 Dec 2024 15:53:49 +0100 Subject: [PATCH] refactor: use join type for other rules --- rules/sort-classes.types.ts | 139 +++++++++++++------------------ rules/sort-modules.types.ts | 52 +++++------- rules/sort-object-types.types.ts | 45 +++++----- typings/index.ts | 13 +-- typings/join-with-dash.ts | 3 + typings/join.ts | 12 +++ typings/sorting-node.ts | 11 +++ 7 files changed, 133 insertions(+), 142 deletions(-) create mode 100644 typings/join-with-dash.ts create mode 100644 typings/join.ts create mode 100644 typings/sorting-node.ts diff --git a/rules/sort-classes.types.ts b/rules/sort-classes.types.ts index 8044df813..78839c14a 100644 --- a/rules/sort-classes.types.ts +++ b/rules/sort-classes.types.ts @@ -1,5 +1,7 @@ import type { JSONSchema4 } from '@typescript-eslint/utils/json-schema' +import type { JoinWithDash } from '../typings' + import { buildCustomGroupModifiersJsonSchema, buildCustomGroupSelectorJsonSchema, @@ -34,42 +36,18 @@ export type SingleCustomGroup = | BaseSingleCustomGroup | AdvancedSingleCustomGroup -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 @@ -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, ] > @@ -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 { @@ -219,17 +221,6 @@ type AdvancedSingleCustomGroup = { elementNamePattern?: string } & BaseSingleCustomGroup -type Join = T extends [ - infer First extends string, - ...infer Rest extends string[], -] - ? `${First}${Join}` - : '' - -type PublicOrProtectedOrPrivateModifierPrefix = WithDashSuffixOrEmpty< - ProtectedModifier | PrivateModifier | PublicModifier -> - interface BaseSingleCustomGroup { modifiers?: AllowedModifiersPerSelector[T][] selector?: T @@ -240,27 +231,9 @@ type PublicOrProtectedOrPrivateModifier = | PrivateModifier | PublicModifier -type StaticOrAbstractModifierPrefix = WithDashSuffixOrEmpty< - AbstractModifier | StaticModifier -> - type GetMethodOrSetMethodSelector = GetMethodSelector | SetMethodSelector -type DecoratedModifierPrefix = WithDashSuffixOrEmpty - -type OverrideModifierPrefix = WithDashSuffixOrEmpty - -type OptionalModifierPrefix = WithDashSuffixOrEmpty - -type ReadonlyModifierPrefix = WithDashSuffixOrEmpty - -type DeclareModifierPrefix = WithDashSuffixOrEmpty - -type StaticModifierPrefix = WithDashSuffixOrEmpty - -type AsyncModifierPrefix = WithDashSuffixOrEmpty - -type WithDashSuffixOrEmpty = `${T}-` | '' +type StaticOrAbstractModifier = AbstractModifier | StaticModifier type FunctionPropertySelector = 'function-property' diff --git a/rules/sort-modules.types.ts b/rules/sort-modules.types.ts index cf73cc1aa..fa1af6bb5 100644 --- a/rules/sort-modules.types.ts +++ b/rules/sort-modules.types.ts @@ -1,5 +1,7 @@ import type { JSONSchema4 } from '@typescript-eslint/utils/json-schema' +import type { JoinWithDash } from '../typings' + import { buildCustomGroupModifiersJsonSchema, buildCustomGroupSelectorJsonSchema, @@ -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 { 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 @@ -129,17 +131,9 @@ interface ElementNamePatternFilterCustomGroup { elementNamePattern?: string } -type DecoratedModifierPrefix = WithDashSuffixOrEmpty - -type DeclareModifierPrefix = WithDashSuffixOrEmpty - -type DefaultModifierPrefix = WithDashSuffixOrEmpty - -type ExportModifierPrefix = WithDashSuffixOrEmpty - -type AsyncModifierPrefix = WithDashSuffixOrEmpty +type TypeGroup = JoinWithDash<[ExportModifier, DeclareModifier, TypeSelector]> -type WithDashSuffixOrEmpty = `${T}-` | '' +type EnumGroup = JoinWithDash<[ExportModifier, DeclareModifier, EnumSelector]> type DecoratedModifier = 'decorated' diff --git a/rules/sort-object-types.types.ts b/rules/sort-object-types.types.ts index e2414f1a3..5f26f6023 100644 --- a/rules/sort-object-types.types.ts +++ b/rules/sort-object-types.types.ts @@ -1,5 +1,7 @@ import type { JSONSchema4 } from '@typescript-eslint/utils/json-schema' +import type { JoinWithDash } from '../typings' + import { buildCustomGroupModifiersJsonSchema, buildCustomGroupSelectorJsonSchema, @@ -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. @@ -88,35 +96,34 @@ type Group = | 'unknown' | string -type PropertyGroup = - `${OptionalModifierPrefix | RequiredModifierPrefix}${MultilineModifierPrefix}${PropertySelector}` - interface BaseSingleCustomGroup { 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 - -type RequiredModifierPrefix = WithDashSuffixOrEmpty - -type OptionalModifierPrefix = WithDashSuffixOrEmpty - -type WithDashSuffixOrEmpty = `${T}-` | '' - type IndexSignatureSelector = 'index-signature' /** diff --git a/typings/index.ts b/typings/index.ts index 72d53c5eb..6beb32cd7 100644 --- a/typings/index.ts +++ b/typings/index.ts @@ -1,11 +1,2 @@ -import type { TSESTree } from '@typescript-eslint/types' - -export interface SortingNode { - 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' diff --git a/typings/join-with-dash.ts b/typings/join-with-dash.ts new file mode 100644 index 000000000..9fd41f182 --- /dev/null +++ b/typings/join-with-dash.ts @@ -0,0 +1,3 @@ +import type { Join } from './join' + +export type JoinWithDash = Join diff --git a/typings/join.ts b/typings/join.ts new file mode 100644 index 000000000..6256565cf --- /dev/null +++ b/typings/join.ts @@ -0,0 +1,12 @@ +export type Join = T extends [ + infer First extends string, + ...infer Rest extends string[], +] + ? Rest extends [] + ? `${First}` + : `${WithSeparatorOrEmpty}${Join}` + : never + +type WithSeparatorOrEmpty = + | `${T}${Separator}` + | '' diff --git a/typings/sorting-node.ts b/typings/sorting-node.ts new file mode 100644 index 000000000..72d53c5eb --- /dev/null +++ b/typings/sorting-node.ts @@ -0,0 +1,11 @@ +import type { TSESTree } from '@typescript-eslint/types' + +export interface SortingNode { + hasMultipleImportDeclarations?: boolean + addSafetySemicolonWhenInline?: boolean + isEslintDisabled: boolean + group?: string + name: string + size: number + node: Node +}