Refactoring
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
Julien Valverdé
2024-02-16 01:14:36 +01:00
parent af8884cdee
commit 9f2c42db14
2 changed files with 63 additions and 61 deletions

View File

@@ -1,23 +1,7 @@
import { Fn, Pipe, Tuples } from "hotscript" import { Fn, Pipe, Tuples } from "hotscript"
import { AbstractClass, Class } from "type-fest" import { AbstractClass, Class } from "type-fest"
import { TraitExpression } from "./TraitExpression" import { TraitExpression } from "./TraitExpression"
import { ExtendFn, SimplifyFn, StaticMembers, StaticMembersFn } from "./util" import { Extend, ExtendFn, SimplifyFn, StaticMembers, StaticMembersFn } from "./util"
type AddAbstractToImplClass<
ImplClass extends Class<object, []>,
Abstract extends object,
StaticAbstract extends object,
> = (
Class<
Abstract &
InstanceType<ImplClass>,
ConstructorParameters<ImplClass>
> &
StaticAbstract &
StaticMembers<ImplClass>
)
export class Trait< export class Trait<
@@ -82,14 +66,18 @@ export namespace Trait {
} }
export type OwnClass<T> = ( export type OwnClass<T> = (
AddAbstractToImplClass<Trait.OwnImplClass<T>, Trait.OwnAbstract<T>, Trait.OwnStaticAbstract<T>> AbstractClass<Trait.OwnInstance<T>, any[]> &
Extend<[
Trait.OwnStaticAbstract<T>,
StaticMembers<Trait.OwnImplClass<T>>,
]>
) )
export interface OwnClassFn extends Fn { export interface OwnClassFn extends Fn {
return: Trait.OwnClass<this["arg0"]> return: Trait.OwnClass<this["arg0"]>
} }
export type OwnInstance<T> = ( export type OwnInstance<T> = (
InstanceType<Trait.OwnClass<T>> Extend<[Trait.OwnAbstract<T>, Trait.OwnImplInstance<T>]>
) )
export interface OwnInstanceFn extends Fn { export interface OwnInstanceFn extends Fn {
return: Trait.OwnInstance<this["arg0"]> return: Trait.OwnInstance<this["arg0"]>
@@ -101,12 +89,30 @@ export namespace Trait {
export interface SupertraitsFn extends Fn { export interface SupertraitsFn extends Fn {
return: Trait.Supertraits<this["arg0"]> return: Trait.Supertraits<this["arg0"]>
} }
}
export type Class<T> = (
AbstractClass< export type TraitClass<T extends Trait<any, any, any, any>> = (
Trait.Instance<T>, AbstractClass<TraitInstance<T>, any[]> &
any[] TraitStaticMembers<T>
> & )
export type TraitConcreteClass<T extends Trait<any, any, any, any>> = (
Class<TraitInstance<T>, any[]> &
TraitStaticMembers<T>
)
export type TraitInstance<T extends Trait<any, any, any, any>> = (
Pipe<T, [
Trait.SupertraitsFn,
Tuples.Append<T>,
Tuples.Map<Trait.OwnInstanceFn>,
ExtendFn,
SimplifyFn,
]>
)
export type TraitStaticMembers<T extends Trait<any, any, any, any>> = (
Pipe<T, [ Pipe<T, [
Trait.SupertraitsFn, Trait.SupertraitsFn,
Tuples.Append<T>, Tuples.Append<T>,
@@ -116,20 +122,3 @@ export namespace Trait {
SimplifyFn, SimplifyFn,
]> ]>
) )
export interface ClassFn extends Fn {
return: Trait.Class<this["arg0"]>
}
export type Instance<T> = (
Pipe<T, [
Trait.SupertraitsFn,
Tuples.Append<T>,
Tuples.Map<Trait.OwnInstanceFn>,
ExtendFn,
SimplifyFn,
]>
)
export interface InstanceFn extends Fn {
return: Trait.Instance<this["arg0"]>
}
}

View File

@@ -1,5 +1,5 @@
import { Fn, Pipe, Tuples } from "hotscript" import { Fn, Pipe, Tuples } from "hotscript"
import { AbstractClass } from "type-fest" import { AbstractClass, Class } from "type-fest"
import { Trait } from "./Trait" import { Trait } from "./Trait"
import { TraitBuilder } from "./TraitBuilder" import { TraitBuilder } from "./TraitBuilder"
import { ExtendFn, SimplifyFn, StaticMembersFn } from "./util" import { ExtendFn, SimplifyFn, StaticMembersFn } from "./util"
@@ -59,6 +59,7 @@ export class TraitExpression<
export namespace TraitExpression { export namespace TraitExpression {
export class NullSuperclass { export class NullSuperclass {
static readonly _tag = "@thilawyn/traitify-ts/TraitExpression.NullSuperclass" static readonly _tag = "@thilawyn/traitify-ts/TraitExpression.NullSuperclass"
constructor(...args: any[]) {}
} }
export type Superclass<T> = ( export type Superclass<T> = (
@@ -102,23 +103,35 @@ export type ImplementsStatic<Exp extends TraitExpression<any, any>> = (
export type TraitExpressionClass<Exp extends TraitExpression<any, any>> = ( export type TraitExpressionClass<Exp extends TraitExpression<any, any>> = (
AbstractClass< AbstractClass<
TraitExpressionInstance<Exp>,
ConstructorParameters<TraitExpression.Superclass<Exp>>
> & > &
TraitExpressionStaticMembers<Exp>
) )
export type TraitExpressionConcreteClass<Exp extends TraitExpression<any, any>> = ( export type TraitExpressionConcreteClass<Exp extends TraitExpression<any, any>> = (
Class<
TraitExpressionInstance<Exp>,
ConstructorParameters<TraitExpression.Superclass<Exp>>
> &
TraitExpressionStaticMembers<Exp>
) )
export type TraitExpressionInstance<Exp extends TraitExpression<any, any>> = ( export type TraitExpressionInstance<Exp extends TraitExpression<any, any>> = (
Pipe<TraitExpression.Traits<Exp>, [
Tuples.Map<Trait.OwnInstanceFn>, // Map all the traits to their instance representation
Tuples.Prepend< // Add the instance of the superclass at the top of the list
InstanceType<TraitExpression.Superclass<Exp>>
>,
ExtendFn, // Reduce to a single object that extends all the objects in the list
SimplifyFn, // Make readable for IDEs
]>
) )
export type TraitExpressionStaticMembers<Exp extends TraitExpression<any, any>> = ( export type TraitExpressionStaticMembers<Exp extends TraitExpression<any, any>> = (
Pipe<Traits, [ Pipe<TraitExpression.Traits<Exp>, [
Tuples.Map<Trait.OwnClassFn>, // Map all the traits to their implementation class Tuples.Map<Trait.OwnClassFn>, // Map all the traits to their class representation
Tuples.Prepend<Superclass>, // Add the superclass at the top of the list Tuples.Prepend<TraitExpression.Superclass<Exp>>, // Add the superclass at the top of the list
Tuples.Map<StaticMembersFn>, // Map all the classes to an object containing their static members Tuples.Map<StaticMembersFn>, // Map all the classes to an object containing their static members
ExtendFn, // Reduce to a single object that extends all the objects in the list ExtendFn, // Reduce to a single object that extends all the objects in the list
SimplifyFn, // Make readable for IDEs SimplifyFn, // Make readable for IDEs