-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
/
babel-restore-jsx.ts
219 lines (196 loc) · 5.85 KB
/
babel-restore-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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/**
* https://github.com/flying-sheep/babel-plugin-transform-react-createelement-to-jsx
* @license GNU General Public License v3.0
*/
import type * as babel from '@babel/core'
/**
* Visitor factory for babel, converting React.createElement(...) to <jsx ...>...</jsx>
*
* What we want to handle here is this CallExpression:
*
* React.createElement(
* type: StringLiteral|Identifier|MemberExpression,
* [props: ObjectExpression|Expression],
* [...children: StringLiteral|Expression]
* )
*
* Any of those arguments might also be missing (undefined) and/or invalid.
*/
export default function ({ types: t }: typeof babel): babel.PluginObj {
/**
* Get a `JSXElement` from a `CallExpression`.
* Returns `null` if this impossible.
*/
function getJSXNode(node: any): any {
if (!isReactCreateElement(node)) {
return null
}
//nameNode and propsNode may be undefined, getJSX* need to handle that
const [nameNode, propsNode, ...childNodes] = node.arguments
const name = getJSXName(nameNode)
if (name == null) {
return null //name is required
}
const props = getJSXProps(propsNode)
if (props == null) {
return null //no props → [], invalid → null
}
const children = getJSXChildren(childNodes)
if (children == null) {
return null //no children → [], invalid → null
}
if (
t.isJSXMemberExpression(name) &&
t.isJSXIdentifier(name.object) &&
name.object.name === 'React' &&
name.property.name === 'Fragment'
) {
return t.jsxFragment(
t.jsxOpeningFragment(),
t.jsxClosingFragment(),
children
)
}
// self-closing tag if no children
const selfClosing = children.length === 0
const startTag = t.jsxOpeningElement(name, props, selfClosing)
startTag.loc = node.loc
const endTag = selfClosing ? null : t.jsxClosingElement(name)
return t.jsxElement(startTag, endTag, children, selfClosing)
}
/**
* Get a JSXIdentifier or JSXMemberExpression from a Node of known type.
* Returns null if a unknown node type, null or undefined is passed.
*/
function getJSXName(node: any): any {
if (node == null) {
return null
}
const name = getJSXIdentifier(node, true)
if (name != null) {
return name
}
if (!t.isMemberExpression(node)) {
return null
}
const object = getJSXName(node.object)
const property = getJSXName(node.property)
if (object == null || property == null) {
return null
}
return t.jsxMemberExpression(object, property)
}
/**
* Get a array of JSX(Spread)Attribute from a props ObjectExpression.
* Handles the _extends Expression babel creates from SpreadElement nodes.
* Returns null if a validation error occurs.
*/
function getJSXProps(node: any): any[] | null {
if (node == null || isNullLikeNode(node)) {
return []
}
if (
t.isCallExpression(node) &&
t.isIdentifier(node.callee, { name: '_extends' })
) {
const props: any[] = node.arguments.map(getJSXProps)
//if calling this recursively works, flatten.
if (props.every((prop) => prop != null)) {
return [].concat(...props)
}
}
if (!t.isObjectExpression(node) && t.isExpression(node))
return [t.jsxSpreadAttribute(node)]
if (!isPlainObjectExpression(node)) {
return null
}
return node.properties.map((prop: any) =>
t.isObjectProperty(prop)
? t.jsxAttribute(
getJSXIdentifier(prop.key)!,
getJSXAttributeValue(prop.value)
)
: t.jsxSpreadAttribute(prop.argument)
)
}
function getJSXChild(node: any) {
if (t.isStringLiteral(node)) {
return t.jsxText(node.value)
}
if (isReactCreateElement(node)) {
return getJSXNode(node)
}
if (t.isExpression(node)) {
return t.jsxExpressionContainer(node)
}
return null
}
function getJSXChildren(nodes: any[]) {
const children = nodes
.filter((node) => !isNullLikeNode(node))
.map(getJSXChild)
if (children.some((child) => child == null)) {
return null
}
return children
}
function getJSXIdentifier(node: any, tag = false) {
//TODO: JSXNamespacedName
if (t.isIdentifier(node) && (!tag || node.name.match(/^[A-Z]/))) {
return t.jsxIdentifier(node.name)
}
if (t.isStringLiteral(node)) {
return t.jsxIdentifier(node.value)
}
return null
}
function getJSXAttributeValue(node: any) {
if (t.isStringLiteral(node)) {
return node
}
if (t.isJSXElement(node)) {
return node
}
if (t.isExpression(node)) {
return t.jsxExpressionContainer(node)
}
return null
}
/**
* Tests if a node is a CallExpression with callee `React.createElement`
*/
const isReactCreateElement = (node: any) =>
t.isCallExpression(node) &&
t.isMemberExpression(node.callee) &&
t.isIdentifier(node.callee.object, { name: 'React' }) &&
t.isIdentifier(node.callee.property, { name: 'createElement' }) &&
!node.callee.computed
/**
* Tests if a node is `null` or `undefined`
*/
const isNullLikeNode = (node: any) =>
t.isNullLiteral(node) || t.isIdentifier(node, { name: 'undefined' })
/**
* Tests if a node is an object expression with noncomputed, nonmethod attrs
*/
const isPlainObjectExpression = (node: any) =>
t.isObjectExpression(node) &&
node.properties.every(
(property) =>
t.isSpreadElement(property) ||
(t.isObjectProperty(property, { computed: false }) &&
getJSXIdentifier(property.key) != null &&
getJSXAttributeValue(property.value) != null)
)
return {
visitor: {
CallExpression(path) {
const node = getJSXNode(path.node)
if (node == null) {
return null
}
path.replaceWith(node)
}
}
}
}