This commit is contained in:
73
src/Trait.ts
73
src/Trait.ts
@@ -1,17 +1,13 @@
|
|||||||
import { Fn, Pipe, Tuples } from "hotscript"
|
import { Fn, Pipe, Tuples } from "hotscript"
|
||||||
import { AbstractClass, Class, Opaque } from "type-fest"
|
import { AbstractClass, Class } from "type-fest"
|
||||||
import { TraitExpression, emptyTraitExpression } from "./TraitExpression"
|
import { TraitExpression } from "./TraitExpression"
|
||||||
import { AbstractTag } from "./abstract"
|
|
||||||
import { ExtendFn, SimplifyFn, StaticMembers, StaticMembersFn } from "./util"
|
import { ExtendFn, SimplifyFn, StaticMembers, StaticMembersFn } from "./util"
|
||||||
|
|
||||||
|
|
||||||
export type TraitApplierSuperTag = "@thilawyn/traitify-ts/Super"
|
|
||||||
|
|
||||||
|
|
||||||
export type AddAbstractToImplClass<
|
export type AddAbstractToImplClass<
|
||||||
ImplClass extends Class<{}, []>,
|
ImplClass extends Class<object, []>,
|
||||||
Abstract extends {},
|
Abstract extends object,
|
||||||
StaticAbstract extends {},
|
StaticAbstract extends object,
|
||||||
> = (
|
> = (
|
||||||
Class<
|
Class<
|
||||||
Abstract &
|
Abstract &
|
||||||
@@ -23,45 +19,32 @@ export type AddAbstractToImplClass<
|
|||||||
StaticMembers<ImplClass>
|
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<
|
export class Trait<
|
||||||
Supertraits extends TraitExpression<
|
SuperExpression extends TraitExpression<
|
||||||
typeof TraitExpression.NullSuperclass,
|
typeof TraitExpression.NullSuperclass,
|
||||||
Trait<any, any, any, any>[],
|
|
||||||
Trait<any, any, any, any>[]
|
Trait<any, any, any, any>[]
|
||||||
>,
|
>,
|
||||||
Abstract extends {},
|
Abstract extends object,
|
||||||
StaticAbstract extends {},
|
StaticAbstract extends object,
|
||||||
ImplClass extends Class<{}, []>,
|
ImplClass extends Class<object, []>,
|
||||||
> {
|
> {
|
||||||
constructor(
|
constructor(
|
||||||
readonly supertraits: Supertraits,
|
readonly superExpression: SuperExpression,
|
||||||
readonly abstract: Abstract,
|
readonly abstract: Abstract,
|
||||||
readonly staticAbstract: StaticAbstract,
|
readonly staticAbstract: StaticAbstract,
|
||||||
readonly apply: (Super: AbstractClass<{}>) => ImplClass,
|
readonly apply: (Super: AbstractClass<object>) => ImplClass,
|
||||||
) {}
|
) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
export namespace Trait {
|
export namespace Trait {
|
||||||
export type OwnSupertraits<T> = (
|
export type OwnSuperExpression<T> = (
|
||||||
T extends Trait<infer Supertraits, any, any, any>
|
T extends Trait<infer SuperExpression, any, any, any>
|
||||||
? Supertraits
|
? SuperExpression
|
||||||
: never
|
: never
|
||||||
)
|
)
|
||||||
export interface OwnSupertraitsFn extends Fn {
|
export interface OwnSuperExpressionFn extends Fn {
|
||||||
return: Trait.OwnSupertraits<this["arg0"]>
|
return: Trait.OwnSuperExpression<this["arg0"]>
|
||||||
}
|
}
|
||||||
|
|
||||||
export type OwnAbstract<T> = (
|
export type OwnAbstract<T> = (
|
||||||
@@ -113,7 +96,7 @@ export namespace Trait {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export type Supertraits<T> = (
|
export type Supertraits<T> = (
|
||||||
TraitExpression.AllTraits<Trait.OwnSupertraits<T>>
|
TraitExpression.Traits<Trait.OwnSuperExpression<T>>
|
||||||
)
|
)
|
||||||
export interface SupertraitsFn extends Fn {
|
export interface SupertraitsFn extends Fn {
|
||||||
return: Trait.Supertraits<this["arg0"]>
|
return: Trait.Supertraits<this["arg0"]>
|
||||||
@@ -150,21 +133,3 @@ export namespace Trait {
|
|||||||
return: Trait.Instance<this["arg0"]>
|
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<
|
export class TraitBuilder<
|
||||||
SuperExpression extends TraitExpression<
|
SuperExpression extends TraitExpression<
|
||||||
typeof TraitExpression.NullSuperclass,
|
typeof TraitExpression.NullSuperclass,
|
||||||
Trait<any, any, any, any>[],
|
|
||||||
Trait<any, any, any, any>[]
|
Trait<any, any, any, any>[]
|
||||||
>,
|
>,
|
||||||
Abstract extends object,
|
Abstract extends object,
|
||||||
@@ -91,9 +90,8 @@ export class TraitBuilder<
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
export const trait = new TraitBuilder(
|
export const trait = new TraitBuilder(
|
||||||
new TraitExpression(TraitExpression.NullSuperclass, [], []),
|
new TraitExpression(TraitExpression.NullSuperclass, []),
|
||||||
{},
|
{},
|
||||||
{},
|
{},
|
||||||
Super => class extends Super {},
|
Super => class extends Super {},
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { Fn, Pipe, Tuples } from "hotscript"
|
import { Fn, Pipe, Tuples } from "hotscript"
|
||||||
import { AbstractClass, Class, Opaque } from "type-fest"
|
import { AbstractClass } from "type-fest"
|
||||||
import { RemoveAbstractFromImplClass, Trait, TraitApplierSuperTag } from "./Trait"
|
import { Trait } from "./Trait"
|
||||||
import { AbstractTag } from "./abstract"
|
import { TraitBuilder } from "./TraitBuilder"
|
||||||
import { ExtendFn, SimplifyFn, StaticMembersFn } from "./util"
|
import { ExtendFn, SimplifyFn, StaticMembersFn } from "./util"
|
||||||
|
|
||||||
|
|
||||||
@@ -13,19 +13,17 @@ import { ExtendFn, SimplifyFn, StaticMembersFn } from "./util"
|
|||||||
|
|
||||||
|
|
||||||
export class TraitExpression<
|
export class TraitExpression<
|
||||||
Superclass extends AbstractClass<{}>,
|
Superclass extends AbstractClass<object>,
|
||||||
const OwnTraits extends Trait<any, any, any, any>[],
|
const Traits extends Trait<any, any, any, any>[],
|
||||||
const AllTraits extends Trait<any, any, any, any>[],
|
|
||||||
> {
|
> {
|
||||||
constructor(
|
constructor(
|
||||||
readonly superclass: Superclass,
|
readonly superclass: Superclass,
|
||||||
readonly ownTraits: OwnTraits,
|
readonly traits: Traits,
|
||||||
readonly allTraits: AllTraits,
|
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
get extends(): (
|
get extends(): (
|
||||||
AbstractClass<
|
AbstractClass<
|
||||||
Pipe<AllTraits, [
|
Pipe<Traits, [
|
||||||
Tuples.Map<Trait.OwnImplInstanceFn>, // Map all the traits to the instance of their implementation class
|
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
|
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
|
ExtendFn, // Reduce to a single instance that extends all the instances in the list
|
||||||
@@ -35,7 +33,7 @@ export class TraitExpression<
|
|||||||
ConstructorParameters<Superclass>
|
ConstructorParameters<Superclass>
|
||||||
> &
|
> &
|
||||||
|
|
||||||
Pipe<AllTraits, [
|
Pipe<Traits, [
|
||||||
Tuples.Map<Trait.OwnImplClassFn>, // Map all the traits to their implementation class
|
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.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
|
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
|
SimplifyFn, // Make readable for IDEs
|
||||||
]>
|
]>
|
||||||
) {
|
) {
|
||||||
return this.allTraits.reduce(
|
return this.traits.reduce(
|
||||||
(previous, trait) => trait.apply(previous),
|
(previous, trait) => trait.apply(previous),
|
||||||
this.superclass,
|
this.superclass,
|
||||||
) as any
|
) as any
|
||||||
@@ -52,11 +50,15 @@ export class TraitExpression<
|
|||||||
implementsStatic(target: ImplementsStatic<typeof this>, context: any) {}
|
implementsStatic(target: ImplementsStatic<typeof this>, context: any) {}
|
||||||
|
|
||||||
subtrait<
|
subtrait<
|
||||||
This extends TraitExpression<typeof TraitExpression.NullSuperclass, any, any>
|
This extends TraitExpression<typeof TraitExpression.NullSuperclass, any>
|
||||||
>(
|
>(
|
||||||
this: This
|
this: This
|
||||||
) {
|
) {
|
||||||
return new Trait(
|
return new TraitBuilder(
|
||||||
|
this,
|
||||||
|
{},
|
||||||
|
{},
|
||||||
|
Super => class extends Super {},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -67,7 +69,7 @@ export namespace TraitExpression {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export type Superclass<T> = (
|
export type Superclass<T> = (
|
||||||
T extends TraitExpression<infer Superclass, any, any>
|
T extends TraitExpression<infer Superclass, any>
|
||||||
? Superclass
|
? Superclass
|
||||||
: never
|
: never
|
||||||
)
|
)
|
||||||
@@ -75,40 +77,29 @@ export namespace TraitExpression {
|
|||||||
return: TraitExpression.Superclass<this["arg0"]>
|
return: TraitExpression.Superclass<this["arg0"]>
|
||||||
}
|
}
|
||||||
|
|
||||||
export type OwnTraits<T> = (
|
export type Traits<T> = (
|
||||||
T extends TraitExpression<any, infer OwnTraits, any>
|
T extends TraitExpression<any, infer Traits>
|
||||||
? OwnTraits
|
? Traits
|
||||||
: never
|
: never
|
||||||
)
|
)
|
||||||
export interface OwnTraitsFn extends Fn {
|
export interface TraitsFn extends Fn {
|
||||||
return: TraitExpression.OwnTraits<this["arg0"]>
|
return: TraitExpression.Traits<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>> = (
|
||||||
export type Implements<Exp extends TraitExpression<any, any, any>> = (
|
|
||||||
Pipe<Exp, [
|
Pipe<Exp, [
|
||||||
TraitExpression.AllTraitsFn,
|
TraitExpression.TraitsFn,
|
||||||
Tuples.Map<Trait.OwnAbstractFn>,
|
Tuples.Map<Trait.OwnAbstractFn>,
|
||||||
ExtendFn,
|
ExtendFn,
|
||||||
SimplifyFn,
|
SimplifyFn,
|
||||||
]>
|
]>
|
||||||
)
|
)
|
||||||
|
|
||||||
export type ImplementsStatic<Exp extends TraitExpression<any, any, any>> = (
|
export type ImplementsStatic<Exp extends TraitExpression<any, any>> = (
|
||||||
Pipe<Exp, [
|
Pipe<Exp, [
|
||||||
TraitExpression.AllTraitsFn,
|
TraitExpression.TraitsFn,
|
||||||
Tuples.Map<Trait.OwnStaticAbstractFn>,
|
Tuples.Map<Trait.OwnStaticAbstractFn>,
|
||||||
ExtendFn,
|
ExtendFn,
|
||||||
SimplifyFn,
|
SimplifyFn,
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { Call, Fn, Pipe, Tuples } from "hotscript"
|
|||||||
import { uniq } from "lodash-es"
|
import { uniq } from "lodash-es"
|
||||||
import { AbstractClass } from "type-fest"
|
import { AbstractClass } from "type-fest"
|
||||||
import { Trait } from "./Trait"
|
import { Trait } from "./Trait"
|
||||||
import { TraitExpression, emptyTraitExpression } from "./TraitExpression"
|
import { TraitExpression } from "./TraitExpression"
|
||||||
import { ExtendableFn, StaticMembersFn } from "./util"
|
import { ExtendableFn, StaticMembersFn } from "./util"
|
||||||
|
|
||||||
|
|
||||||
@@ -51,76 +51,64 @@ type ImplStaticMembersExtendable<
|
|||||||
|
|
||||||
type BuildTraitExpression<
|
type BuildTraitExpression<
|
||||||
Superclass extends AbstractClass<{}>,
|
Superclass extends AbstractClass<{}>,
|
||||||
OwnTraits extends Trait<any, any, any, any>[],
|
Traits extends Trait<any, any, any, any>[],
|
||||||
AllTraits 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."
|
? "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."
|
? "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."
|
? "Type conflict between the traits implementation static members and/or the superclass static members."
|
||||||
: TraitExpression<Superclass, OwnTraits, AllTraits>
|
: TraitExpression<Superclass, Traits>
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class TraitExpressionBuilder<
|
class TraitExpressionBuilder<
|
||||||
Superclass extends AbstractClass<{}>,
|
Superclass extends AbstractClass<object>,
|
||||||
const OwnTraits extends Trait<any, any, any, any>[],
|
const Traits extends Trait<any, any, any, any>[],
|
||||||
const AllTraits extends Trait<any, any, any, any>[],
|
|
||||||
> {
|
> {
|
||||||
constructor(private readonly expression: TraitExpression<Superclass, OwnTraits, AllTraits>) {}
|
constructor(
|
||||||
|
private readonly expressionSuperclass: Superclass,
|
||||||
|
private readonly expressionTraits: Traits,
|
||||||
|
) {}
|
||||||
|
|
||||||
extends<
|
extends<
|
||||||
Super extends AbstractClass<any>
|
Super extends AbstractClass<object>
|
||||||
>(
|
>(
|
||||||
superclass: Super
|
superclass: Super
|
||||||
) {
|
) {
|
||||||
return new TraitExpressionBuilder(
|
return new TraitExpressionBuilder(
|
||||||
new TraitExpression(
|
superclass,
|
||||||
superclass,
|
this.expressionTraits,
|
||||||
this.expression.ownTraits,
|
|
||||||
this.expression.allTraits,
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
expresses<
|
expresses<
|
||||||
const Traits extends Trait<any, any, any, any>[]
|
const T extends Trait<any, any, any, any>[]
|
||||||
>(
|
>(
|
||||||
...traits: Traits
|
...traits: T
|
||||||
): TraitExpressionBuilder<
|
): TraitExpressionBuilder<
|
||||||
Superclass,
|
Superclass,
|
||||||
[...OwnTraits, ...Traits],
|
[...Traits, ...SpreadSupertraits<T>]
|
||||||
[...AllTraits, ...SpreadSupertraits<Traits>]
|
|
||||||
> {
|
> {
|
||||||
return new TraitExpressionBuilder(
|
return new TraitExpressionBuilder(
|
||||||
new TraitExpression(
|
this.expressionSuperclass,
|
||||||
this.expression.superclass,
|
|
||||||
uniq([...this.expression.ownTraits, ...traits]) as [...OwnTraits, ...Traits],
|
uniq([
|
||||||
uniq([...this.expression.allTraits, ...this.spreadSupertraits(traits)]) as [...AllTraits, ...SpreadSupertraits<Traits>],
|
...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() {
|
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 {
|
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