fork of hey-api/openapi-ts because I need some additional things
1import type { AttrCtor } from '../expr/attr';
2import type { CallCtor } from '../expr/call';
3import type { SubscriptCtor } from '../expr/subscript';
4import type { ReturnCtor } from '../stmt/return';
5
6type Ctor = (...args: Array<any>) => any;
7
8type Factory<T extends Ctor> = {
9 (...args: Parameters<T>): ReturnType<T>;
10 /** Sets the implementation of this factory. */
11 set(fn: T): void;
12};
13
14function createFactory<T extends Ctor>(name: string): Factory<T> {
15 let impl: T | undefined;
16
17 const slot = ((...args: Parameters<T>) => {
18 if (!impl) throw new Error(`${name} factory not registered`);
19 return impl(...args);
20 }) as Factory<T>;
21
22 slot.set = (fn: T) => {
23 impl = fn;
24 };
25
26 return slot;
27}
28
29export const f = {
30 /** Factory for creating property access expressions (e.g., `obj.foo`). */
31 attr: createFactory<AttrCtor>('attr'),
32
33 /** Factory for creating function or method call expressions (e.g., `fn(arg)`). */
34 call: createFactory<CallCtor>('call'),
35
36 /** Factory for creating return statements. */
37 return: createFactory<ReturnCtor>('return'),
38
39 /** Factory for creating slice expressions. */
40 slice: createFactory<SubscriptCtor>('slice'),
41};