|
| 1 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
1 | 2 | import { throwInternalCompilerError } from "../errors";
|
2 | 3 |
|
3 | 4 | /**
|
@@ -44,19 +45,103 @@ type Handlers<I, O> = Unwrap<Intersect<Inputs<I>>> & Outputs<O>;
|
44 | 45 | */
|
45 | 46 | export const makeVisitor =
|
46 | 47 | <I>() =>
|
47 |
| - <O>(handlers: Handlers<I, O>) => { |
48 |
| - return (input: Extract<I, { kind: string }>): O[keyof O] => { |
49 |
| - const handler = ( |
50 |
| - handlers as Record<string, (input: I) => O[keyof O]> |
51 |
| - )[input.kind]; |
52 |
| - |
53 |
| - // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition |
54 |
| - if (handler) { |
55 |
| - return handler(input); |
56 |
| - } else { |
57 |
| - throwInternalCompilerError( |
58 |
| - `Reached impossible case: ${input.kind}`, |
59 |
| - ); |
60 |
| - } |
61 |
| - }; |
| 48 | + <O>(handlers: Handlers<I, O>) => |
| 49 | + (input: Extract<I, { kind: string }>): O[keyof O] => { |
| 50 | + const handler = (handlers as Record<string, (input: I) => O[keyof O]>)[ |
| 51 | + input.kind |
| 52 | + ]; |
| 53 | + |
| 54 | + // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition |
| 55 | + if (handler) { |
| 56 | + return handler(input); |
| 57 | + } else { |
| 58 | + throwInternalCompilerError( |
| 59 | + `Reached impossible case: ${input.kind}`, |
| 60 | + ); |
| 61 | + } |
62 | 62 | };
|
| 63 | + |
| 64 | +type Extend<T extends any[], H> = H extends infer A ? [...T, A] : never; |
| 65 | +type Flat<TS extends any[], R extends any[] = []> = TS extends [ |
| 66 | + infer H, |
| 67 | + ...infer T, |
| 68 | +] |
| 69 | + ? Flat<T, Extend<R, H>> |
| 70 | + : R; |
| 71 | + |
| 72 | +declare const NoSuchCase: unique symbol; |
| 73 | +interface NoSuchCaseBug<L> extends Array<never> { |
| 74 | + [NoSuchCase]: L; |
| 75 | +} |
| 76 | +type On<V, I extends any[], O> = { |
| 77 | + on: <const DI extends any[]>( |
| 78 | + ...key: I extends Flat<DI> ? DI : NoSuchCaseBug<DI> |
| 79 | + ) => <const DO>( |
| 80 | + handler: (...args: Extract<I, Flat<DI>>) => DO, |
| 81 | + ) => MV<V, Exclude<I, Flat<DI>>, O | DO>; |
| 82 | +}; |
| 83 | + |
| 84 | +declare const CasesAreNotExhaustive: unique symbol; |
| 85 | +interface NonExhaustiveBug<L> { |
| 86 | + [CasesAreNotExhaustive]: L; |
| 87 | +} |
| 88 | +type End<I, O> = [I] extends [never] |
| 89 | + ? EndInternal<O> |
| 90 | + : { |
| 91 | + end: NonExhaustiveBug<I>; |
| 92 | + }; |
| 93 | +type MV<V, I extends any[], O> = End<I, O> & On<V, I, O>; |
| 94 | + |
| 95 | +type OnInternal<V, I extends any[], O> = { |
| 96 | + on: <const DI extends any[]>( |
| 97 | + ...key: DI |
| 98 | + ) => <const DO>( |
| 99 | + handler: (...args: Extract<I, Flat<DI>>) => DO, |
| 100 | + ) => MVInternal<V, Exclude<I, Flat<DI>>, O | DO>; |
| 101 | +}; |
| 102 | +type EndInternal<O> = { |
| 103 | + end: () => O; |
| 104 | +}; |
| 105 | +type MVInternal<V, I extends any[], O> = EndInternal<O> & OnInternal<V, I, O>; |
| 106 | + |
| 107 | +const deepMatch = (a: unknown, b: unknown): boolean => { |
| 108 | + if ( |
| 109 | + a === b && |
| 110 | + ["number", "string", "boolean", "bigint"].includes(typeof a) && |
| 111 | + typeof a === typeof b |
| 112 | + ) { |
| 113 | + return true; |
| 114 | + } |
| 115 | + if (a === null || b === null) { |
| 116 | + return a === b; |
| 117 | + } |
| 118 | + if (typeof a === "object" && typeof b === "object") { |
| 119 | + if (Array.isArray(a) && Array.isArray(b) && a.length === b.length) { |
| 120 | + return a.every((a, i) => deepMatch(a, b[i])); |
| 121 | + } else { |
| 122 | + return Object.entries(b).every(([k, b]) => |
| 123 | + deepMatch(k in a ? (a as any)[k] : undefined, b), |
| 124 | + ); |
| 125 | + } |
| 126 | + } |
| 127 | + return false; |
| 128 | +}; |
| 129 | + |
| 130 | +export const match = <I extends any[]>(...args: I): MV<I, Flat<I>, never> => { |
| 131 | + const rec = <V, I extends any[], O>(end: () => O): MVInternal<V, I, O> => ({ |
| 132 | + end, |
| 133 | + on: |
| 134 | + <const DI extends any[]>(...match: DI) => |
| 135 | + <const DO>(handler: (...args: Extract<I, Flat<DI>>) => DO) => |
| 136 | + rec<V, Exclude<I, Flat<DI>>, O | DO>(() => |
| 137 | + deepMatch(args, match) |
| 138 | + ? handler( |
| 139 | + ...(args as unknown as Extract<I, Flat<DI, []>>), |
| 140 | + ) |
| 141 | + : end(), |
| 142 | + ), |
| 143 | + }); |
| 144 | + return rec<I, Flat<I>, never>(() => { |
| 145 | + throw new Error("Not exhaustive"); |
| 146 | + }) as MV<I, Flat<I>, never>; |
| 147 | +}; |
0 commit comments