forked from babel/babel
/
define-map.ts
66 lines (51 loc) · 1.49 KB
/
define-map.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import { types as t } from "@babel/core";
type DefineMap = {
_inherits: t.Node[];
_key: t.Expression;
get?: t.Expression;
set?: t.Expression;
kind: "get" | "set";
};
export type MutatorMap = Record<string, DefineMap>;
export function pushAccessor(
mutatorMap: MutatorMap,
node: t.ObjectMethod & { kind: "get" | "set"; computed: false },
) {
const alias = t.toKeyAlias(node);
const map = (mutatorMap[alias] ??= {
_inherits: [],
_key: node.key,
} as DefineMap);
map._inherits.push(node);
const value = t.functionExpression(
null,
node.params,
node.body,
node.generator,
node.async,
);
value.returnType = node.returnType;
t.inheritsComments(value, node);
map[node.kind] = value;
return map;
}
export function toDefineObject(mutatorMap: any) {
const objExpr = t.objectExpression([]);
Object.keys(mutatorMap).forEach(function (mutatorMapKey) {
const map = mutatorMap[mutatorMapKey];
map.configurable = t.booleanLiteral(true);
map.enumerable = t.booleanLiteral(true);
const mapNode = t.objectExpression([]);
const propNode = t.objectProperty(map._key, mapNode, map._computed);
Object.keys(map).forEach(function (key) {
const node = map[key];
if (key[0] === "_") return;
const prop = t.objectProperty(t.identifier(key), node);
t.inheritsComments(prop, node);
t.removeComments(node);
mapNode.properties.push(prop);
});
objExpr.properties.push(propNode);
});
return objExpr;
}