forked from benjamn/ast-types
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsx.ts
139 lines (119 loc) · 4.15 KB
/
jsx.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import { Fork } from "../types";
import esProposalsDef from "./es-proposals";
import typesPlugin from "../lib/types";
import sharedPlugin from "../lib/shared";
import { namedTypes as N } from "../gen/namedTypes";
export default function (fork: Fork) {
fork.use(esProposalsDef);
const types = fork.use(typesPlugin);
const def = types.Type.def;
const or = types.Type.or;
const defaults = fork.use(sharedPlugin).defaults;
def("JSXAttribute")
.bases("Node")
.build("name", "value")
.field("name", or(def("JSXIdentifier"), def("JSXNamespacedName")))
.field("value", or(
def("Literal"), // attr="value"
def("JSXExpressionContainer"), // attr={value}
def("JSXElement"), // attr=<div />
def("JSXFragment"), // attr=<></>
null // attr= or just attr
), defaults["null"]);
def("JSXIdentifier")
.bases("Identifier")
.build("name")
.field("name", String);
def("JSXNamespacedName")
.bases("Node")
.build("namespace", "name")
.field("namespace", def("JSXIdentifier"))
.field("name", def("JSXIdentifier"));
def("JSXMemberExpression")
.bases("MemberExpression")
.build("object", "property")
.field("object", or(def("JSXIdentifier"), def("JSXMemberExpression")))
.field("property", def("JSXIdentifier"))
.field("computed", Boolean, defaults.false);
const JSXElementName = or(
def("JSXIdentifier"),
def("JSXNamespacedName"),
def("JSXMemberExpression")
);
def("JSXSpreadAttribute")
.bases("Node")
.build("argument")
.field("argument", def("Expression"));
const JSXAttributes = [or(
def("JSXAttribute"),
def("JSXSpreadAttribute")
)];
def("JSXExpressionContainer")
.bases("Expression")
.build("expression")
.field("expression", or(def("Expression"), def("JSXEmptyExpression")));
const JSXChildren = [or(
def("JSXText"),
def("JSXExpressionContainer"),
def("JSXSpreadChild"),
def("JSXElement"),
def("JSXFragment"),
def("Literal") // Legacy: Esprima should return JSXText instead.
)];
def("JSXElement")
.bases("Expression")
.build("openingElement", "closingElement", "children")
.field("openingElement", def("JSXOpeningElement"))
.field("closingElement", or(def("JSXClosingElement"), null), defaults["null"])
.field("children", JSXChildren, defaults.emptyArray)
.field("name", JSXElementName, function (this: N.JSXElement) {
// Little-known fact: the `this` object inside a default function
// is none other than the partially-built object itself, and any
// fields initialized directly from builder function arguments
// (like openingElement, closingElement, and children) are
// guaranteed to be available.
return this.openingElement.name;
}, true) // hidden from traversal
.field("selfClosing", Boolean, function (this: N.JSXElement) {
return this.openingElement.selfClosing;
}, true) // hidden from traversal
.field("attributes", JSXAttributes, function (this: N.JSXElement) {
return this.openingElement.attributes;
}, true); // hidden from traversal
def("JSXOpeningElement")
.bases("Node")
.build("name", "attributes", "selfClosing")
.field("name", JSXElementName)
.field("attributes", JSXAttributes, defaults.emptyArray)
.field("selfClosing", Boolean, defaults["false"]);
def("JSXClosingElement")
.bases("Node")
.build("name")
.field("name", JSXElementName);
def("JSXFragment")
.bases("Expression")
.build("openingFragment", "closingFragment", "children")
.field("openingFragment", def("JSXOpeningFragment"))
.field("closingFragment", def("JSXClosingFragment"))
.field("children", JSXChildren, defaults.emptyArray);
def("JSXOpeningFragment")
.bases("Node")
.build();
def("JSXClosingFragment")
.bases("Node")
.build();
def("JSXText")
.bases("Literal")
.build("value", "raw")
.field("value", String)
.field("raw", String, function (this: N.JSXText) {
return this.value;
});
def("JSXEmptyExpression")
.bases("Node")
.build();
def("JSXSpreadChild")
.bases("Node")
.build("expression")
.field("expression", def("Expression"));
};