Skip to content

Commit fecdee1

Browse files
committed
refactor: break down files
1 parent 5af3f8c commit fecdee1

File tree

5 files changed

+71
-65
lines changed

5 files changed

+71
-65
lines changed

src/proxy/_utils.ts

Lines changed: 1 addition & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,75 +1,14 @@
11
import * as recast from "recast";
22
import { MagicastError } from "../error";
33
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";
85

96
const b = recast.types.builders;
107

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-
328
export function isValidPropName(name: string) {
339
return /^[$A-Z_a-z][\w$]*$/.test(name);
3410
}
3511

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-
7312
const PROXY_KEY = "__magicast_proxy";
7413

7514
export function literalToAst(value: any, seen = new Set()): ESNode {

src/proxy/array.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { ESNode } from "../types";
2-
import { literalToAst, createProxy, proxify } from "./_utils";
2+
import { literalToAst, createProxy } from "./_utils";
3+
import { proxify } from "./proxify";
34
import { Proxified, ProxifiedModule } from "./types";
45

56
export function proxifyArrayElements<T extends object>(

src/proxy/exports.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as recast from "recast";
22
import { Program } from "@babel/types";
3-
import { createProxy, literalToAst, proxify } from "./_utils";
3+
import { createProxy, literalToAst } from "./_utils";
4+
import { proxify } from "./proxify";
45
import { ProxifiedModule } from "./types";
56

67
const b = recast.types.builders;

src/proxy/object.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import * as recast from "recast";
22
import { ESNode } from "../types";
33
import { MagicastError } from "../error";
4-
import { literalToAst, createProxy, proxify, isValidPropName } from "./_utils";
4+
import { literalToAst, createProxy, isValidPropName } from "./_utils";
5+
import { proxify } from "./proxify";
56
import { Proxified, ProxifiedModule } from "./types";
67

78
const b = recast.types.builders;

src/proxy/proxify.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import { MagicastError } from "../error";
2+
import { ESNode } from "../types";
3+
import { proxifyArray } from "./array";
4+
import { proxifyFunctionCall } from "./function-call";
5+
import { proxifyObject } from "./object";
6+
import { Proxified, ProxifiedModule } from "./types";
7+
8+
const _literalTypes = new Set([
9+
"Literal",
10+
"StringLiteral",
11+
"NumericLiteral",
12+
"BooleanLiteral",
13+
"NullLiteral",
14+
"RegExpLiteral",
15+
"BigIntLiteral",
16+
]);
17+
18+
const _literals = new Set([
19+
"string",
20+
"number",
21+
"boolean",
22+
"bigint",
23+
"symbol",
24+
"undefined",
25+
]);
26+
27+
const _cache = new WeakMap<ESNode, Proxified<any>>();
28+
29+
export function proxify<T>(node: ESNode, mod?: ProxifiedModule): Proxified<T> {
30+
if (_literals.has(typeof node)) {
31+
return node as any;
32+
}
33+
if (_literalTypes.has(node.type)) {
34+
return (node as any).value as any;
35+
}
36+
37+
if (_cache.has(node)) {
38+
return _cache.get(node) as Proxified<T>;
39+
}
40+
41+
let proxy: Proxified<T>;
42+
switch (node.type) {
43+
case "ObjectExpression": {
44+
proxy = proxifyObject<T>(node, mod);
45+
break;
46+
}
47+
case "ArrayExpression": {
48+
proxy = proxifyArray<T>(node, mod);
49+
break;
50+
}
51+
case "CallExpression": {
52+
proxy = proxifyFunctionCall(node, mod);
53+
break;
54+
}
55+
default:
56+
throw new MagicastError(`Casting "${node.type}" is not supported`, {
57+
ast: node,
58+
code: mod?.$code,
59+
});
60+
}
61+
62+
_cache.set(node, proxy);
63+
return proxy;
64+
}

0 commit comments

Comments
 (0)