-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
142 lines (125 loc) · 4 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
module.exports = function(babel) {
var t = babel.types
const jsxHandler = {
exit(path, state) {
// turn tag into createElement call
var callExpr = buildElementCall(path.get('openingElement'), state)
if (path.node.children.length) {
// add children as 3rd+ arg
path.node.children.forEach(c => callExpr.arguments.push(c))
// if you want to create an array instead, do it here
}
path.replaceWith(t.inherits(callExpr, path.node))
},
}
return {
//inherits: require('babel-plugin-syntax-jsx'),
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.',
)
},
JSXFragment: jsxHandler,
JSXElement: jsxHandler,
},
}
function buildElementCall(path, state) {
if (state.opts.addArrow) {
// extra option to add arrow around every child that is js expression
replaceWithArrow(path.parent.children)
}
path.parent.children = t.react.buildChildren(path.parent)
var tagExpr = convertJSXIdentifier(path.node ? path.node.name : t.NullLiteral(), 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)) {
// starts with uppercase
args.push(t.stringLiteral(tagName))
} else {
args.push(tagExpr)
}
var attribs = t.NullLiteral()
if (path.node) {
attribs = path.node.attributes
if (attribs.length) {
attribs = buildOpeningElementAttributes(attribs, state)
} else {
attribs = t.nullLiteral()
}
}
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)) {
node = t.thisExpression()
} else {
// Vue uses esutils here to confirm valid Identifier name
// we do no such thing for our simple JSX transform
node.type = 'Identifier'
}
} else if (t.isJSXMemberExpression(node)) {
node = t.memberExpression(
convertJSXIdentifier(node.object, node),
convertJSXIdentifier(node.property, node),
)
}
return node
}
/**
* Convert to object declaration by adding all
* props and spreads as they are found.
*/
function buildOpeningElementAttributes(attribs, state) {
var _props = []
while (attribs.length) {
var prop = attribs.shift()
if (t.isJSXSpreadAttribute(prop)) {
prop.argument._isSpread = true
_props.push(t.spreadElement(prop.argument))
} else {
_props.push(convertAttribute(prop, state.opts.addArrow))
}
}
attribs = t.objectExpression(_props)
return attribs
}
function convertAttribute(node, addArrow) {
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)
}
// extra option to add arrow around every attribute value that is js expression
if (addArrow && t.isJSXExpressionContainer(node.value)) {
value = t.arrowFunctionExpression([], value)
}
return t.inherits(t.objectProperty(node.name, value), node)
}
function convertAttributeValue(node) {
if (t.isJSXExpressionContainer(node)) {
return node.expression
} else {
return node
}
}
function replaceWithArrow(ch) {
for (var i = 0; i < ch.length; i++) {
if (t.isJSXExpressionContainer(ch[i])) {
ch[i].expression = t.arrowFunctionExpression([], ch[i].expression)
}
}
}
}