This commit is contained in:
73
src/Trait.ts
73
src/Trait.ts
@@ -1,17 +1,13 @@
|
||||
import { Fn, Pipe, Tuples } from "hotscript"
|
||||
import { AbstractClass, Class, Opaque } from "type-fest"
|
||||
import { TraitExpression, emptyTraitExpression } from "./TraitExpression"
|
||||
import { AbstractTag } from "./abstract"
|
||||
import { AbstractClass, Class } from "type-fest"
|
||||
import { TraitExpression } from "./TraitExpression"
|
||||
import { ExtendFn, SimplifyFn, StaticMembers, StaticMembersFn } from "./util"
|
||||
|
||||
|
||||
export type TraitApplierSuperTag = "@thilawyn/traitify-ts/Super"
|
||||
|
||||
|
||||
export type AddAbstractToImplClass<
|
||||
ImplClass extends Class<{}, []>,
|
||||
Abstract extends {},
|
||||
StaticAbstract extends {},
|
||||
ImplClass extends Class<object, []>,
|
||||
Abstract extends object,
|
||||
StaticAbstract extends object,
|
||||
> = (
|
||||
Class<
|
||||
Abstract &
|
||||
@@ -23,45 +19,32 @@ export type AddAbstractToImplClass<
|
||||
StaticMembers<ImplClass>
|
||||
)
|
||||
|
||||
export type RemoveAbstractFromImplClass<
|
||||
ImplClassWithAbstract extends Class<Abstract, []> & StaticAbstract & { _tag: TraitApplierSuperTag },
|
||||
Abstract extends {},
|
||||
StaticAbstract extends {},
|
||||
> = (
|
||||
Class<
|
||||
Omit<InstanceType<ImplClassWithAbstract>, keyof Abstract>,
|
||||
ConstructorParameters<ImplClassWithAbstract>
|
||||
> &
|
||||
Omit<StaticMembers<ImplClassWithAbstract>, keyof StaticAbstract | "_tag">
|
||||
)
|
||||
|
||||
|
||||
export class Trait<
|
||||
Supertraits extends TraitExpression<
|
||||
SuperExpression extends TraitExpression<
|
||||
typeof TraitExpression.NullSuperclass,
|
||||
Trait<any, any, any, any>[],
|
||||
Trait<any, any, any, any>[]
|
||||
>,
|
||||
Abstract extends {},
|
||||
StaticAbstract extends {},
|
||||
ImplClass extends Class<{}, []>,
|
||||
Abstract extends object,
|
||||
StaticAbstract extends object,
|
||||
ImplClass extends Class<object, []>,
|
||||
> {
|
||||
constructor(
|
||||
readonly supertraits: Supertraits,
|
||||
readonly abstract: Abstract,
|
||||
readonly staticAbstract: StaticAbstract,
|
||||
readonly apply: (Super: AbstractClass<{}>) => ImplClass,
|
||||
readonly superExpression: SuperExpression,
|
||||
readonly abstract: Abstract,
|
||||
readonly staticAbstract: StaticAbstract,
|
||||
readonly apply: (Super: AbstractClass<object>) => ImplClass,
|
||||
) {}
|
||||
}
|
||||
|
||||
export namespace Trait {
|
||||
export type OwnSupertraits<T> = (
|
||||
T extends Trait<infer Supertraits, any, any, any>
|
||||
? Supertraits
|
||||
export type OwnSuperExpression<T> = (
|
||||
T extends Trait<infer SuperExpression, any, any, any>
|
||||
? SuperExpression
|
||||
: never
|
||||
)
|
||||
export interface OwnSupertraitsFn extends Fn {
|
||||
return: Trait.OwnSupertraits<this["arg0"]>
|
||||
export interface OwnSuperExpressionFn extends Fn {
|
||||
return: Trait.OwnSuperExpression<this["arg0"]>
|
||||
}
|
||||
|
||||
export type OwnAbstract<T> = (
|
||||
@@ -113,7 +96,7 @@ export namespace Trait {
|
||||
}
|
||||
|
||||
export type Supertraits<T> = (
|
||||
TraitExpression.AllTraits<Trait.OwnSupertraits<T>>
|
||||
TraitExpression.Traits<Trait.OwnSuperExpression<T>>
|
||||
)
|
||||
export interface SupertraitsFn extends Fn {
|
||||
return: Trait.Supertraits<this["arg0"]>
|
||||
@@ -150,21 +133,3 @@ export namespace Trait {
|
||||
return: Trait.Instance<this["arg0"]>
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export function trait<
|
||||
Abstract extends {},
|
||||
StaticAbstract extends {},
|
||||
ImplClassWithAbstract extends Class<Abstract, []> & StaticAbstract & { _tag: TraitApplierSuperTag },
|
||||
>(
|
||||
abstract: Opaque<Abstract, AbstractTag>,
|
||||
staticAbstract: Opaque<StaticAbstract, AbstractTag>,
|
||||
apply: (Super: AbstractClass<Abstract> & StaticAbstract & { _tag: TraitApplierSuperTag }) => ImplClassWithAbstract,
|
||||
) {
|
||||
return new Trait(
|
||||
emptyTraitExpression,
|
||||
abstract as Abstract,
|
||||
staticAbstract as StaticAbstract,
|
||||
apply as any as (Super: AbstractClass<{}>) => RemoveAbstractFromImplClass<ImplClassWithAbstract, Abstract, StaticAbstract>,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -26,7 +26,6 @@ export type RemoveAbstractFromImplClass<
|
||||
export class TraitBuilder<
|
||||
SuperExpression extends TraitExpression<
|
||||
typeof TraitExpression.NullSuperclass,
|
||||
Trait<any, any, any, any>[],
|
||||
Trait<any, any, any, any>[]
|
||||
>,
|
||||
Abstract extends object,
|
||||
@@ -91,9 +90,8 @@ export class TraitBuilder<
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export const trait = new TraitBuilder(
|
||||
new TraitExpression(TraitExpression.NullSuperclass, [], []),
|
||||
new TraitExpression(TraitExpression.NullSuperclass, []),
|
||||
{},
|
||||
{},
|
||||
Super => class extends Super {},
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -2,7 +2,7 @@ import { Call, Fn, Pipe, Tuples } from "hotscript"
|
||||
import { uniq } from "lodash-es"
|
||||
import { AbstractClass } from "type-fest"
|
||||
import { Trait } from "./Trait"
|
||||
import { TraitExpression, emptyTraitExpression } from "./TraitExpression"
|
||||
import { TraitExpression } from "./TraitExpression"
|
||||
import { ExtendableFn, StaticMembersFn } from "./util"
|
||||
|
||||
|
||||
@@ -51,76 +51,64 @@ type ImplStaticMembersExtendable<
|
||||
|
||||
type BuildTraitExpression<
|
||||
Superclass extends AbstractClass<{}>,
|
||||
OwnTraits extends Trait<any, any, any, any>[],
|
||||
AllTraits extends Trait<any, any, any, any>[],
|
||||
Traits extends Trait<any, any, any, any>[],
|
||||
> = (
|
||||
AbstractMembersExtendable<Superclass, AllTraits> extends false
|
||||
AbstractMembersExtendable<Superclass, Traits> extends false
|
||||
? "Type conflict between the traits abstract members and/or the superclass instance."
|
||||
: ImplInstanceExtendable<Superclass, AllTraits> extends false
|
||||
: ImplInstanceExtendable<Superclass, Traits> extends false
|
||||
? "Type conflict between the traits implementation instances and/or the superclass instance."
|
||||
: ImplStaticMembersExtendable<Superclass, AllTraits> extends false
|
||||
: ImplStaticMembersExtendable<Superclass, Traits> extends false
|
||||
? "Type conflict between the traits implementation static members and/or the superclass static members."
|
||||
: TraitExpression<Superclass, OwnTraits, AllTraits>
|
||||
: TraitExpression<Superclass, Traits>
|
||||
)
|
||||
|
||||
|
||||
class TraitExpressionBuilder<
|
||||
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(private readonly expression: TraitExpression<Superclass, OwnTraits, AllTraits>) {}
|
||||
constructor(
|
||||
private readonly expressionSuperclass: Superclass,
|
||||
private readonly expressionTraits: Traits,
|
||||
) {}
|
||||
|
||||
extends<
|
||||
Super extends AbstractClass<any>
|
||||
Super extends AbstractClass<object>
|
||||
>(
|
||||
superclass: Super
|
||||
) {
|
||||
return new TraitExpressionBuilder(
|
||||
new TraitExpression(
|
||||
superclass,
|
||||
this.expression.ownTraits,
|
||||
this.expression.allTraits,
|
||||
)
|
||||
superclass,
|
||||
this.expressionTraits,
|
||||
)
|
||||
}
|
||||
|
||||
expresses<
|
||||
const Traits extends Trait<any, any, any, any>[]
|
||||
const T extends Trait<any, any, any, any>[]
|
||||
>(
|
||||
...traits: Traits
|
||||
...traits: T
|
||||
): TraitExpressionBuilder<
|
||||
Superclass,
|
||||
[...OwnTraits, ...Traits],
|
||||
[...AllTraits, ...SpreadSupertraits<Traits>]
|
||||
[...Traits, ...SpreadSupertraits<T>]
|
||||
> {
|
||||
return new TraitExpressionBuilder(
|
||||
new TraitExpression(
|
||||
this.expression.superclass,
|
||||
uniq([...this.expression.ownTraits, ...traits]) as [...OwnTraits, ...Traits],
|
||||
uniq([...this.expression.allTraits, ...this.spreadSupertraits(traits)]) as [...AllTraits, ...SpreadSupertraits<Traits>],
|
||||
)
|
||||
this.expressionSuperclass,
|
||||
|
||||
uniq([
|
||||
...this.expressionTraits,
|
||||
...traits.flatMap(trait => [
|
||||
...trait.superExpression.allTraits,
|
||||
trait,
|
||||
]),
|
||||
]) as [...Traits, ...SpreadSupertraits<T>],
|
||||
)
|
||||
}
|
||||
|
||||
private spreadSupertraits<
|
||||
const Traits extends Trait<
|
||||
TraitExpression<any, any, Trait<any, any, any, any>[]>,
|
||||
any,
|
||||
any,
|
||||
any
|
||||
>[]
|
||||
>(
|
||||
traits: Traits
|
||||
) {
|
||||
return traits.flatMap(trait => [
|
||||
...trait.supertraits.allTraits,
|
||||
trait,
|
||||
]) as SpreadSupertraits<Traits>
|
||||
}
|
||||
|
||||
build() {
|
||||
return this.expression as BuildTraitExpression<Superclass, OwnTraits, AllTraits>
|
||||
return new TraitExpression(
|
||||
this.expressionSuperclass,
|
||||
this.expressionTraits,
|
||||
) as BuildTraitExpression<Superclass, Traits>
|
||||
}
|
||||
|
||||
then<V>(fn: (expression: ReturnType<typeof this.build>) => V): V {
|
||||
@@ -128,4 +116,4 @@ class TraitExpressionBuilder<
|
||||
}
|
||||
}
|
||||
|
||||
export const expression = new TraitExpressionBuilder(emptyTraitExpression)
|
||||
export const expression = new TraitExpressionBuilder(TraitExpression.NullSuperclass, [])
|
||||
|
||||
Reference in New Issue
Block a user