|
1 | | -import { type AstNode } from '../ast'; |
2 | | - |
3 | | -export type ContainerProps<T extends AstNode | undefined> = { |
4 | | - $container: T; |
5 | | - $containerProperty?: string; |
6 | | - $containerIndex?: number; |
7 | | -}; |
8 | | - |
9 | | -type NodeFactoriesFor<N> = { |
10 | | - [K in keyof N as {} extends Pick<N, K> ? never : K]: N[K] extends (infer U)[] |
11 | | - ? (AstFactory<U extends AstNode ? U : AstNode> | U)[] |
12 | | - : AstFactory<N[K] extends AstNode ? N[K] : AstNode> | N[K]; |
13 | | -} & { |
14 | | - [K in keyof N as {} extends Pick<N, K> ? K : never]?: N[K] extends (infer U)[] |
15 | | - ? (AstFactory<U extends AstNode ? U : AstNode> | U)[] |
16 | | - : AstFactory<N[K] extends AstNode ? N[K] : AstNode> | N[K]; |
17 | | -}; |
18 | | - |
19 | | -export abstract class AstFactory<T extends AstNode = AstNode> { |
20 | | - node = {} as T; |
21 | | - constructor({ type, node }: { type: T['$type']; node?: Partial<T> }) { |
22 | | - (this.node as any).$type = type; |
23 | | - if (node) { |
24 | | - this.update(node); |
25 | | - } |
26 | | - } |
27 | | - setContainer(container: T['$container']) { |
28 | | - (this.node as any).$container = container; |
29 | | - return this; |
30 | | - } |
31 | | - |
32 | | - get(params?: ContainerProps<T['$container']>): T { |
33 | | - if (params) this.update(params as any); |
34 | | - return this.node; |
35 | | - } |
36 | | - update(nodeArg: Partial<T | NodeFactoriesFor<T>>): T { |
37 | | - const keys = Object.keys(nodeArg as object); |
38 | | - keys.forEach((key) => { |
39 | | - const child = (nodeArg as any)[key]; |
40 | | - if (child instanceof AstFactory) { |
41 | | - (this.node as any)[key] = child.get({ $container: this.node as any }); |
42 | | - } else if (Array.isArray(child)) { |
43 | | - (this.node as any)[key] = child.map((item: any) => |
44 | | - item instanceof AstFactory ? item.get({ $container: this.node as any }) : item, |
45 | | - ); |
46 | | - } else { |
47 | | - (this.node as any)[key] = child; |
48 | | - } |
49 | | - }); |
50 | | - return this.node; |
51 | | - } |
52 | | - |
53 | | - resolveChilds(nodeArg: T | NodeFactoriesFor<T>): T { |
54 | | - return this.update(nodeArg); |
55 | | - } |
56 | | -} |
57 | | - |
| 1 | +export * from './ast-factory'; |
58 | 2 | export * from './primitives'; |
59 | 3 | export * from './expression'; |
60 | 4 | export * from './declaration'; |
|
0 commit comments