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

This commit is contained in:
Julien Valverdé
2024-02-14 21:13:02 +01:00
parent 5873926dc5
commit 1758649507
4 changed files with 77 additions and 135 deletions

View File

@@ -1,7 +1,7 @@
import { Fn, Pipe, Tuples } from "hotscript"
import { AbstractClass, Class, Opaque } from "type-fest"
import { RemoveAbstractFromImplClass, Trait, TraitApplierSuperTag } from "./Trait"
import { AbstractTag } from "./abstract"
import { AbstractClass } from "type-fest"
import { Trait } from "./Trait"
import { TraitBuilder } from "./TraitBuilder"
import { ExtendFn, SimplifyFn, StaticMembersFn } from "./util"
@@ -13,19 +13,17 @@ import { ExtendFn, SimplifyFn, StaticMembersFn } from "./util"
export class TraitExpression<
Superclass extends AbstractClass<{}>,
const OwnTraits extends Trait<any, any, any, any>[],
const AllTraits extends Trait<any, 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, [
Pipe<Traits, [
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
@@ -35,7 +33,7 @@ export class TraitExpression<
ConstructorParameters<Superclass>
> &
Pipe<AllTraits, [
Pipe<Traits, [
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
@@ -43,7 +41,7 @@ export class TraitExpression<
SimplifyFn, // Make readable for IDEs
]>
) {
return this.allTraits.reduce(
return this.traits.reduce(
(previous, trait) => trait.apply(previous),
this.superclass,
) as any
@@ -52,11 +50,15 @@ export class TraitExpression<
implementsStatic(target: ImplementsStatic<typeof this>, context: any) {}
subtrait<
This extends TraitExpression<typeof TraitExpression.NullSuperclass, any, any>
This extends TraitExpression<typeof TraitExpression.NullSuperclass, any>
>(
this: This
) {
return new Trait(
return new TraitBuilder(
this,
{},
{},
Super => class extends Super {},
)
}
}
@@ -67,7 +69,7 @@ export namespace TraitExpression {
}
export type Superclass<T> = (
T extends TraitExpression<infer Superclass, any, any>
T extends TraitExpression<infer Superclass, any>
? Superclass
: never
)
@@ -75,40 +77,29 @@ export namespace TraitExpression {
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 interface TraitsFn extends Fn {
return: TraitExpression.Traits<this["arg0"]>
}
}
export const emptyTraitExpression = new TraitExpression(TraitExpression.NullSuperclass, [], [])
export type Implements<Exp extends TraitExpression<any, any, any>> = (
export type Implements<Exp extends TraitExpression<any, any>> = (
Pipe<Exp, [
TraitExpression.AllTraitsFn,
TraitExpression.TraitsFn,
Tuples.Map<Trait.OwnAbstractFn>,
ExtendFn,
SimplifyFn,
]>
)
export type ImplementsStatic<Exp extends TraitExpression<any, any, any>> = (
export type ImplementsStatic<Exp extends TraitExpression<any, any>> = (
Pipe<Exp, [
TraitExpression.AllTraitsFn,
TraitExpression.TraitsFn,
Tuples.Map<Trait.OwnStaticAbstractFn>,
ExtendFn,
SimplifyFn,