132 lines
3.9 KiB
TypeScript
132 lines
3.9 KiB
TypeScript
import { Call, Fn, Pipe, Tuples } from "hotscript"
|
|
import { AbstractClass } from "type-fest"
|
|
import { Trait, TraitExpression, emptyTraitExpression } from "."
|
|
import { ExtendableFn, StaticMembersFn } from "./util"
|
|
|
|
|
|
type SpreadSupertraits<Traits extends Trait<any, any, any>[]> = (
|
|
Call<
|
|
Tuples.FlatMap<PrependTraitSupertraitsFn>,
|
|
Traits
|
|
>
|
|
)
|
|
interface PrependTraitSupertraitsFn extends Fn {
|
|
return: [...Trait.Supertraits<this["arg0"]>, this["arg0"]]
|
|
}
|
|
|
|
|
|
type AbstractMembersExtendable<
|
|
Superclass extends AbstractClass<{}>,
|
|
Traits extends Trait<any, any, any>[],
|
|
> = (
|
|
Call<ExtendableFn, [
|
|
InstanceType<Superclass>,
|
|
...Call<Tuples.Map<Trait.OwnAbstractFn>, Traits>,
|
|
]>
|
|
)
|
|
|
|
type ImplInstanceExtendable<
|
|
Superclass extends AbstractClass<{}>,
|
|
Traits extends Trait<any, any, any>[],
|
|
> = (
|
|
Call<ExtendableFn, [
|
|
InstanceType<Superclass>,
|
|
...Call<Tuples.Map<Trait.OwnImplInstanceFn>, Traits>,
|
|
]>
|
|
)
|
|
|
|
type ImplStaticMembersExtendable<
|
|
Superclass extends AbstractClass<{}>,
|
|
Traits extends Trait<any, any, any>[],
|
|
> = (
|
|
Pipe<[
|
|
Superclass,
|
|
...Call<Tuples.Map<Trait.OwnImplClassFn>, Traits>,
|
|
], [
|
|
Tuples.Map<StaticMembersFn>,
|
|
ExtendableFn,
|
|
]>
|
|
)
|
|
|
|
type BuildTraitExpression<
|
|
Superclass extends AbstractClass<{}>,
|
|
OwnTraits extends Trait<any, any, any>[],
|
|
AllTraits extends Trait<any, any, any>[],
|
|
> = (
|
|
Call<Tuples.IsEmpty, AllTraits> extends true
|
|
? "Cannot express an empty list of traits."
|
|
: AbstractMembersExtendable<Superclass, AllTraits> extends false
|
|
? "Type conflict between the traits abstract members and/or the superclass instance."
|
|
: ImplInstanceExtendable<Superclass, AllTraits> extends false
|
|
? "Type conflict between the traits implementation instances and/or the superclass instance."
|
|
: ImplStaticMembersExtendable<Superclass, AllTraits> extends false
|
|
? "Type conflict between the traits implementation static members and/or the superclass static members."
|
|
: TraitExpression<Superclass, OwnTraits, AllTraits>
|
|
)
|
|
|
|
|
|
class TraitExpressionBuilder<
|
|
Super extends AbstractClass<{}>,
|
|
OwnTraits extends Trait<any, any, any>[],
|
|
AllTraits extends Trait<any, any, any>[],
|
|
> {
|
|
constructor(private expression: TraitExpression<Super, OwnTraits, AllTraits>) {}
|
|
|
|
extends<
|
|
Super extends AbstractClass<any>
|
|
>(
|
|
superclass: Super
|
|
) {
|
|
return new TraitExpressionBuilder(
|
|
new TraitExpression(
|
|
superclass,
|
|
this.expression.ownTraits,
|
|
this.expression.allTraits,
|
|
)
|
|
)
|
|
}
|
|
|
|
expresses<
|
|
Traits extends Trait<any, any, any>[]
|
|
>(
|
|
...traits: Traits
|
|
): TraitExpressionBuilder<
|
|
Super,
|
|
[...OwnTraits, ...Traits],
|
|
[...AllTraits, ...SpreadSupertraits<Traits>]
|
|
> {
|
|
return new TraitExpressionBuilder(
|
|
new TraitExpression(
|
|
this.expression.superclass,
|
|
[...this.expression.ownTraits, ...traits] as const,
|
|
[...this.expression.allTraits, ...this.spreadSupertraits(traits)] as const,
|
|
)
|
|
) as any
|
|
}
|
|
|
|
private spreadSupertraits<
|
|
Traits extends Trait<
|
|
TraitExpression<any, any, Trait<any, any, any>[]>,
|
|
any,
|
|
any
|
|
>[]
|
|
>(
|
|
traits: Traits
|
|
) {
|
|
return traits.flatMap(trait => [
|
|
...trait.supertraits.allTraits,
|
|
trait,
|
|
]) as SpreadSupertraits<Traits>
|
|
}
|
|
|
|
build() {
|
|
return this.expression as BuildTraitExpression<Super, OwnTraits, AllTraits>
|
|
}
|
|
|
|
then<V>(fn: (expression: ReturnType<typeof this.build>) => V): V {
|
|
return fn(this.build())
|
|
}
|
|
}
|
|
|
|
export const expression = new TraitExpressionBuilder(emptyTraitExpression)
|