Co-authored-by: Julien Valverdé <julien.valverde@mailo.com> Reviewed-on: https://git.jvalver.de/Thilawyn/traitify-ts/pulls/4
This commit was merged in pull request #4.
This commit is contained in:
@@ -1,113 +1,163 @@
|
||||
import { Fn, Pipe, Tuples } from "hotscript"
|
||||
import { AbstractClass, Class, Opaque } from "type-fest"
|
||||
import { RemoveAbstractFromImplClass, Trait, TraitApplierSuperTag } from "./Trait"
|
||||
import { AbstractTag } from "./abstract"
|
||||
import { ExtendFn, SimplifyFn, StaticMembersFn } from "./util"
|
||||
|
||||
|
||||
// type RemoveSupertraitsAbstractFromAbstract<Left, Right> = {
|
||||
// [Key in Extract<keyof Left, keyof Right>]: Left[Key]
|
||||
// } & {
|
||||
// [Key in Exclude<keyof Left, keyof Right>]: Left[Key]
|
||||
// }
|
||||
import { AbstractClass, Class, Simplify } from "type-fest"
|
||||
import { Trait, TraitTuple } from "./Trait"
|
||||
import { TraitBuilder } from "./TraitBuilder"
|
||||
import { Extend, StaticMembers } from "./util"
|
||||
|
||||
|
||||
export class TraitExpression<
|
||||
Superclass extends AbstractClass<{}>,
|
||||
const OwnTraits extends Trait<any, any, any>[],
|
||||
const AllTraits extends Trait<any, any, any>[],
|
||||
Superclass extends AbstractClass<object>,
|
||||
const Traits extends Trait<any, any, any, any>[],
|
||||
> {
|
||||
constructor(
|
||||
readonly superclass: Superclass,
|
||||
readonly ownTraits: OwnTraits,
|
||||
readonly allTraits: AllTraits,
|
||||
readonly traits: Traits,
|
||||
) {}
|
||||
|
||||
get extends(): (
|
||||
AbstractClass<
|
||||
Pipe<AllTraits, [
|
||||
Tuples.Map<Trait.OwnImplInstanceFn>, // Map all the traits to the instance of their implementation class
|
||||
Tuples.Prepend<InstanceType<Superclass>>, // Add the instance of the superclass at the top of the list
|
||||
ExtendFn, // Reduce to a single instance that extends all the instances in the list
|
||||
SimplifyFn, // Make readable for IDEs
|
||||
]>,
|
||||
InstanceType<Superclass> & // Keep the instance of the superclass outside of any kind of type manipulation
|
||||
// as it can accidentely remove abstract properties
|
||||
Simplify<
|
||||
Extend<
|
||||
TraitTuple.MapImplInstance<Traits>
|
||||
>
|
||||
>,
|
||||
|
||||
ConstructorParameters<Superclass>
|
||||
> &
|
||||
|
||||
Pipe<AllTraits, [
|
||||
Tuples.Map<Trait.OwnImplClassFn>, // Map all the traits to their implementation class
|
||||
Tuples.Prepend<Superclass>, // Add the superclass at the top of the list
|
||||
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
|
||||
SimplifyFn, // Make readable for IDEs
|
||||
]>
|
||||
Simplify<
|
||||
Extend<[
|
||||
StaticMembers<Superclass>,
|
||||
...TraitTuple.MapImplStaticMembers<Traits>,
|
||||
]>
|
||||
>
|
||||
) {
|
||||
return this.allTraits.reduce(
|
||||
return this.traits.reduce(
|
||||
(previous, trait) => trait.apply(previous),
|
||||
this.superclass,
|
||||
) as any
|
||||
}
|
||||
|
||||
implementsStatic(target: ImplementsStatic<typeof this>, context: any) {}
|
||||
|
||||
subtrait<
|
||||
This extends TraitExpression<typeof TraitExpression.NullSuperclass, any, any>,
|
||||
SubtraitAbstract extends Implements<This>,
|
||||
SubtraitImplClassWithAbstract extends Class<SubtraitAbstract, []> & { _tag: TraitApplierSuperTag },
|
||||
This extends TraitExpression<typeof TraitExpression.NullSuperclass, any>
|
||||
>(
|
||||
this: This,
|
||||
abstract: (expression: This) => Opaque<SubtraitAbstract, AbstractTag>,
|
||||
apply: (Super: AbstractClass<SubtraitAbstract> & { _tag: TraitApplierSuperTag }) => SubtraitImplClassWithAbstract,
|
||||
this: This
|
||||
) {
|
||||
return new Trait(
|
||||
return new TraitBuilder<
|
||||
This,
|
||||
|
||||
Simplify<
|
||||
Extend<TraitTuple.MapAbstract<Traits>>
|
||||
>,
|
||||
Simplify<
|
||||
Extend<TraitTuple.MapStaticAbstract<Traits>>
|
||||
>,
|
||||
|
||||
AbstractClass<
|
||||
Simplify<
|
||||
Extend<TraitTuple.MapImplInstance<Traits>>
|
||||
>
|
||||
> &
|
||||
Simplify<
|
||||
Extend<TraitTuple.MapImplStaticMembers<Traits>>
|
||||
>
|
||||
>(
|
||||
this,
|
||||
// {} as RemoveSupertraitsAbstractFromAbstract<SubtraitAbstract, Implements<typeof this>>,
|
||||
{} as SubtraitAbstract, // TODO: find a way to cleanly substract Implements<typeof this> from this.
|
||||
apply as any as (Super: AbstractClass<{}>) => RemoveAbstractFromImplClass<SubtraitImplClassWithAbstract, SubtraitAbstract>,
|
||||
{} as any,
|
||||
{} as any,
|
||||
Super => class extends Super {} as any,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export namespace TraitExpression {
|
||||
declare const nullSuperclassSymbol: unique symbol
|
||||
export class NullSuperclass {
|
||||
static readonly _tag = "@thilawyn/traitify-ts/TraitExpression.NullSuperclass"
|
||||
static readonly [nullSuperclassSymbol]: true
|
||||
constructor(...args: any[]) {}
|
||||
}
|
||||
|
||||
export type Superclass<T> = (
|
||||
T extends TraitExpression<infer Superclass, any, any>
|
||||
T extends TraitExpression<infer Superclass, any>
|
||||
? Superclass
|
||||
: never
|
||||
)
|
||||
export interface SuperclassFn extends Fn {
|
||||
return: TraitExpression.Superclass<this["arg0"]>
|
||||
}
|
||||
|
||||
export type OwnTraits<T> = (
|
||||
T extends TraitExpression<any, infer OwnTraits, any>
|
||||
? OwnTraits
|
||||
export type Traits<T> = (
|
||||
T extends TraitExpression<any, infer Traits>
|
||||
? Traits
|
||||
: never
|
||||
)
|
||||
export interface OwnTraitsFn extends Fn {
|
||||
return: TraitExpression.OwnTraits<this["arg0"]>
|
||||
}
|
||||
|
||||
export type AllTraits<T> = (
|
||||
T extends TraitExpression<any, any, infer AllTraits>
|
||||
? AllTraits
|
||||
: never
|
||||
)
|
||||
export interface AllTraitsFn extends Fn {
|
||||
return: TraitExpression.AllTraits<this["arg0"]>
|
||||
}
|
||||
}
|
||||
|
||||
export const emptyTraitExpression = new TraitExpression(TraitExpression.NullSuperclass, [], [])
|
||||
|
||||
|
||||
export type Implements<Exp extends TraitExpression<any, any, any>> = (
|
||||
Pipe<Exp, [
|
||||
TraitExpression.AllTraitsFn,
|
||||
Tuples.Map<Trait.OwnAbstractFn>,
|
||||
ExtendFn,
|
||||
SimplifyFn,
|
||||
]>
|
||||
export type Implements<
|
||||
Exp extends TraitExpression<any, Trait<any, any, any, any>[]>
|
||||
> = (
|
||||
Simplify<
|
||||
Extend<
|
||||
TraitTuple.MapAbstract<
|
||||
TraitExpression.Traits<Exp>
|
||||
>
|
||||
>
|
||||
>
|
||||
)
|
||||
|
||||
export type ImplementsStatic<
|
||||
Exp extends TraitExpression<any, Trait<any, any, any, any>[]>
|
||||
> = (
|
||||
Simplify<
|
||||
Extend<
|
||||
TraitTuple.MapStaticAbstract<
|
||||
TraitExpression.Traits<Exp>
|
||||
>
|
||||
>
|
||||
>
|
||||
)
|
||||
|
||||
|
||||
export type TraitExpressionClass<
|
||||
Exp extends TraitExpression<any, Trait<any, any, any, any>[]>
|
||||
> = (
|
||||
AbstractClass<
|
||||
TraitExpressionInstance<Exp>,
|
||||
ConstructorParameters<TraitExpression.Superclass<Exp>>
|
||||
> &
|
||||
TraitExpressionStaticMembers<Exp>
|
||||
)
|
||||
|
||||
export type TraitExpressionConcreteClass<
|
||||
Exp extends TraitExpression<any, Trait<any, any, any, any>[]>
|
||||
> = (
|
||||
Class<
|
||||
TraitExpressionInstance<Exp>,
|
||||
ConstructorParameters<TraitExpression.Superclass<Exp>>
|
||||
> &
|
||||
TraitExpressionStaticMembers<Exp>
|
||||
)
|
||||
|
||||
export type TraitExpressionInstance<
|
||||
Exp extends TraitExpression<any, Trait<any, any, any, any>[]>
|
||||
> = (
|
||||
InstanceType<TraitExpression.Superclass<Exp>> & // Keep the instance of the superclass outside of any kind of type manipulation
|
||||
// as it can accidentely remove abstract properties
|
||||
Simplify<
|
||||
Extend<
|
||||
TraitTuple.MapInstance<TraitExpression.Traits<Exp>>
|
||||
>
|
||||
>
|
||||
)
|
||||
|
||||
export type TraitExpressionStaticMembers<
|
||||
Exp extends TraitExpression<any, Trait<any, any, any, any>[]>
|
||||
> = (
|
||||
Simplify<
|
||||
Extend<[
|
||||
StaticMembers<TraitExpression.Superclass<Exp>>,
|
||||
...TraitTuple.MapStaticMembers<TraitExpression.Traits<Exp>>,
|
||||
]>
|
||||
>
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user