forked from vuejs/babel-plugin-transform-vue-jsx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
224 lines (203 loc) · 6.69 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
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
220
221
222
223
224
var esutils = require('esutils')
var groupProps = require('./lib/group-props')
var mustUseProp = require('./lib/must-use-prop')
var addDefault = require('@babel/helper-module-imports').addDefault
var isInsideJsxExpression = function (t, path) {
if (!path.parentPath) {
return false
}
if (t.isJSXExpressionContainer(path.parentPath)) {
return true
}
return isInsideJsxExpression(t, path.parentPath)
}
module.exports = function (babel) {
var t = babel.types
return {
inherits: require('@babel/plugin-syntax-jsx').default,
visitor: {
JSXNamespacedName (path) {
throw path.buildCodeFrameError(
'Namespaced tags/attributes are not supported. JSX is not XML.\n' +
'For attributes like xlink:href, use xlinkHref instead.'
)
},
JSXElement: {
exit (path, file) {
// turn tag into createElement call
var callExpr = buildElementCall(path.get('openingElement'), file)
if (path.node.children.length) {
// add children array as 3rd arg
callExpr.arguments.push(t.arrayExpression(path.node.children))
if (callExpr.arguments.length >= 3) {
callExpr._prettyCall = true
}
}
path.replaceWith(t.inherits(callExpr, path.node))
}
},
'Program' (path) {
path.traverse({
'ObjectMethod|ClassMethod' (path) {
const params = path.get('params')
// do nothing if there is (h) param
if (params.length && params[0].node.name === 'h') {
return
}
// do nothing if there is no JSX inside
const jsxChecker = {
hasJsx: false
}
path.traverse({
JSXElement () {
this.hasJsx = true
}
}, jsxChecker)
if (!jsxChecker.hasJsx) {
return
}
// do nothing if this method is a part of JSX expression
if (isInsideJsxExpression(t, path)) {
return
}
const isRender = path.node.key.name === 'render'
// inject h otherwise
path.get('body').unshiftContainer('body', t.variableDeclaration('const', [
t.variableDeclarator(
t.identifier('h'),
(
isRender
? t.memberExpression(
t.identifier('arguments'),
t.numericLiteral(0),
true
)
: t.memberExpression(
t.thisExpression(),
t.identifier('$createElement')
)
)
)
]))
},
JSXOpeningElement (path) {
const tag = path.get('name').node.name
const attributes = path.get('attributes')
const typeAttribute = attributes.find(attributePath => attributePath.node.name && attributePath.node.name.name === 'type')
const type = typeAttribute && t.isStringLiteral(typeAttribute.node.value) ? typeAttribute.node.value.value : null
attributes.forEach(attributePath => {
const attribute = attributePath.get('name')
if (!attribute.node) {
return
}
const attr = attribute.node.name
if (mustUseProp(tag, type, attr) && t.isJSXExpressionContainer(attributePath.node.value)) {
attribute.replaceWith(t.JSXIdentifier(`domProps-${attr}`))
}
})
}
})
}
}
}
function buildElementCall (path, file) {
path.parent.children = t.react.buildChildren(path.parent)
var tagExpr = convertJSXIdentifier(path.node.name, path.node)
var args = []
var tagName
if (t.isIdentifier(tagExpr)) {
tagName = tagExpr.name
} else if (t.isLiteral(tagExpr)) {
tagName = tagExpr.value
}
if (t.react.isCompatTag(tagName)) {
args.push(t.stringLiteral(tagName))
} else {
args.push(tagExpr)
}
var attribs = path.node.attributes
if (attribs.length) {
attribs = buildOpeningElementAttributes(attribs, path)
args.push(attribs)
}
return t.callExpression(t.identifier('h'), args)
}
function convertJSXIdentifier (node, parent) {
if (t.isJSXIdentifier(node)) {
if (node.name === 'this' && t.isReferenced(node, parent)) {
return t.thisExpression()
} else if (esutils.keyword.isIdentifierNameES6(node.name)) {
node.type = 'Identifier'
} else {
return t.stringLiteral(node.name)
}
} else if (t.isJSXMemberExpression(node)) {
return t.memberExpression(
convertJSXIdentifier(node.object, node),
convertJSXIdentifier(node.property, node)
)
}
return node
}
/**
* The logic for this is quite terse. It's because we need to
* support spread elements. We loop over all attributes,
* breaking on spreads, we then push a new object containing
* all prior attributes to an array for later processing.
*/
function buildOpeningElementAttributes (attribs, path) {
var _props = []
var objs = []
function pushProps () {
if (!_props.length) return
objs.push(t.objectExpression(_props))
_props = []
}
while (attribs.length) {
var prop = attribs.shift()
if (t.isJSXSpreadAttribute(prop)) {
pushProps()
prop.argument._isSpread = true
objs.push(prop.argument)
} else {
_props.push(convertAttribute(prop))
}
}
pushProps()
objs = objs.map(function (o) {
return o._isSpread ? o : groupProps(o.properties, t)
})
if (objs.length === 1) {
// only one object
attribs = objs[0]
} else if (objs.length) {
// add prop merging helper
var helper = addDefault(path, 'babel-helper-vue-jsx-merge-props', { nameHint: '_mergeJSXProps' })
// spread it
attribs = t.callExpression(
helper,
[t.arrayExpression(objs)]
)
}
return attribs
}
function convertAttribute (node) {
var value = convertAttributeValue(node.value || t.booleanLiteral(true))
if (t.isStringLiteral(value) && !t.isJSXExpressionContainer(node.value)) {
value.value = value.value.replace(/\n\s+/g, ' ')
}
if (t.isValidIdentifier(node.name.name)) {
node.name.type = 'Identifier'
} else {
node.name = t.stringLiteral(node.name.name)
}
return t.inherits(t.objectProperty(node.name, value), node)
}
function convertAttributeValue (node) {
if (t.isJSXExpressionContainer(node)) {
return node.expression
} else {
return node
}
}
}