-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
51 lines (44 loc) · 1.2 KB
/
index.js
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
import serialize from "babel-literal-to-ast";
export default function ({types: t}) {
function runtimeType(identifier, type) {
const variable = t.variableDeclarator(
t.identifier(`${identifier}Type`),
serialize(type)
);
return t.variableDeclaration("const", [variable]);
}
function parse(typeDef) {
delete typeDef["start"];
delete typeDef["end"];
delete typeDef["loc"];
delete typeDef["extra"];
if (typeDef["id"]) {
typeDef["id"] = parse(typeDef["id"]);
}
if (typeDef["key"]) {
typeDef["key"] = parse(typeDef["key"]);
}
if (typeDef["value"]) {
typeDef["value"] = parse(typeDef["value"]);
}
if (typeDef["types"]) {
typeDef["types"] = typeDef["types"].map(parse);
}
if (typeDef["properties"]) {
typeDef["properties"] = typeDef["properties"].map(parse);
}
return typeDef;
}
return {
visitor: {
TypeAlias(path) {
const identifier = path.node.id.name;
const parsed = parse(path.node.right);
const variable = runtimeType(identifier, parsed);
path.replaceWith(
t.exportNamedDeclaration(variable, [])
);
}
}
};
}