|
1 | 1 | import * as recast from "recast";
|
2 | 2 | import { MagicastError } from "../error";
|
3 | 3 | import type { ESNode } from "../types";
|
4 |
| -import { proxifyArray } from "./array"; |
5 |
| -import { proxifyFunctionCall } from "./function-call"; |
6 |
| -import { proxifyObject } from "./object"; |
7 |
| -import { ProxyUtils, Proxified, ProxifiedModule } from "./types"; |
| 4 | +import { ProxyUtils, Proxified } from "./types"; |
8 | 5 |
|
9 | 6 | const b = recast.types.builders;
|
10 | 7 |
|
11 |
| -const literalTypes = new Set([ |
12 |
| - "Literal", |
13 |
| - "StringLiteral", |
14 |
| - "NumericLiteral", |
15 |
| - "BooleanLiteral", |
16 |
| - "NullLiteral", |
17 |
| - "RegExpLiteral", |
18 |
| - "BigIntLiteral", |
19 |
| -]); |
20 |
| - |
21 |
| -const literals = new Set([ |
22 |
| - "string", |
23 |
| - "number", |
24 |
| - "boolean", |
25 |
| - "bigint", |
26 |
| - "symbol", |
27 |
| - "undefined", |
28 |
| -]); |
29 |
| - |
30 |
| -const _cache = new WeakMap<ESNode, Proxified<any>>(); |
31 |
| - |
32 | 8 | export function isValidPropName(name: string) {
|
33 | 9 | return /^[$A-Z_a-z][\w$]*$/.test(name);
|
34 | 10 | }
|
35 | 11 |
|
36 |
| -export function proxify<T>(node: ESNode, mod?: ProxifiedModule): Proxified<T> { |
37 |
| - if (literals.has(typeof node)) { |
38 |
| - return node as any; |
39 |
| - } |
40 |
| - if (literalTypes.has(node.type)) { |
41 |
| - return (node as any).value as any; |
42 |
| - } |
43 |
| - |
44 |
| - if (_cache.has(node)) { |
45 |
| - return _cache.get(node) as Proxified<T>; |
46 |
| - } |
47 |
| - |
48 |
| - let proxy: Proxified<T>; |
49 |
| - switch (node.type) { |
50 |
| - case "ObjectExpression": { |
51 |
| - proxy = proxifyObject<T>(node, mod); |
52 |
| - break; |
53 |
| - } |
54 |
| - case "ArrayExpression": { |
55 |
| - proxy = proxifyArray<T>(node, mod); |
56 |
| - break; |
57 |
| - } |
58 |
| - case "CallExpression": { |
59 |
| - proxy = proxifyFunctionCall(node, mod); |
60 |
| - break; |
61 |
| - } |
62 |
| - default: |
63 |
| - throw new MagicastError(`Casting "${node.type}" is not supported`, { |
64 |
| - ast: node, |
65 |
| - code: mod?.$code, |
66 |
| - }); |
67 |
| - } |
68 |
| - |
69 |
| - _cache.set(node, proxy); |
70 |
| - return proxy; |
71 |
| -} |
72 |
| - |
73 | 12 | const PROXY_KEY = "__magicast_proxy";
|
74 | 13 |
|
75 | 14 | export function literalToAst(value: any, seen = new Set()): ESNode {
|
|
0 commit comments