/
index.js
246 lines (200 loc) · 6.63 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
module.exports = function (babel) {
"use strict";
var t = babel.types;
var visitor = parser({
t: t,
pre: function pre(state) {
var tagName = state.tagName;
var args = state.args;
if (t.react.isCompatTag(tagName)) {
args.push(t.stringLiteral(tagName));
} else {
args.push(state.tagExpr);
}
},
post: function post(state, pass) {
state.callee = pass.get("jsxIdentifier");
}
});
visitor.Program = function (path, state) {
var id = state.opts.pragma || "h";
state.set("jsxIdentifier", id.split(".").map(function (name) {
return t.identifier(name);
}).reduce(function (object, property) {
return t.memberExpression(object, property);
}));
};
return {
inherits: require("babel-plugin-syntax-jsx"),
visitor: visitor
};
};
function parser(opts) {
var t = opts.t;
var visitor = {};
/*
visitor.JSXNamespacedName = function (path) {
throw path.buildCodeFrameError("Namespace tags are not supported yet.");
};
*/
visitor.JSXElement = {
exit: function exit(path, file) {
var callExpr = buildElementCall(path.get("openingElement"), file);
var concatExpression = [];
if (path.node.children.length) {
var emptyArray = t.arrayExpression([]);
var emptyArrayDotConcat = t.memberExpression(emptyArray, t.identifier("concat"));
concatExpression = t.callExpression(emptyArrayDotConcat, path.node.children);
}
callExpr.arguments = callExpr.arguments.concat(concatExpression); // path.node.children
if (callExpr.arguments.length >= 3) {
callExpr._prettyCall = true;
}
path.replaceWith(t.inherits(callExpr, path.node));
}
};
return visitor;
function convertJSXIdentifier(node, parent) {
if (t.isJSXIdentifier(node)) {
if (node.name === "this" && t.isReferenced(node, parent)) {
return t.thisExpression();
} else if (/^[A-Z]/.test(node.name)) {
// node is assumed to be a JS identifier (var, function, class, etc) if the first letter is UPPERCASE
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;
}
function convertAttributeValue(node) {
if (t.isJSXExpressionContainer(node)) {
return node.expression;
} else {
return node;
}
}
function convertAttribute(node) {
var value = convertAttributeValue(node.value || t.booleanLiteral(true));
if (t.isStringLiteral(value)) {
value.value = value.value.replace(/\n\s+/g, " ");
}
if (t.isValidIdentifier(node.name.name)) {
node.name.type = "Identifier";
} else {
var nodeName = node.name.namespace ? node.name.namespace.name + ":" + node.name.name.name : node.name.name;
node.name = t.stringLiteral(nodeName);
}
return t.inherits(t.objectProperty(node.name, value), node);
}
function buildElementCall(path, file) {
path.parent.children = t.react.buildChildren(path.parent);
var tagExpr = convertJSXIdentifier(path.node.name, path.node);
var args = [];
var tagName = undefined;
if (t.isIdentifier(tagExpr)) {
tagName = tagExpr.name;
} else if (t.isLiteral(tagExpr)) {
tagName = tagExpr.value;
}
var state = {
tagExpr: tagExpr,
tagName: tagName,
args: args
};
if (opts.pre) {
opts.pre(state, file);
}
var attribs = path.node.attributes;
if (attribs.length) {
attribs = buildOpeningElementAttributes(attribs, file);
} else {
attribs = t.objectExpression([]);
// attribs = t.nullLiteral();
}
args.push(attribs);
if (opts.post) {
opts.post(state, file);
}
return state.call || t.callExpression(state.callee, args);
}
function groupAttributes(props) {
var attributes = [];
// group props based on prefix (prefix-name)
props.forEach(prop => {
var propName = prop.key.name || prop.key.value;
var propNameSplitOnDash = propName.split("-");
if (propNameSplitOnDash.length > 1) { // contains -
var prefix = propNameSplitOnDash[0];
var suffix = propNameSplitOnDash[1];
// check if node already contains property with name prefix
var propertyWithPrefix = attributes.find(p => p.key.name === prefix);
var suffixProperty = t.objectProperty(t.identifier(suffix), prop.value);
if (!propertyWithPrefix) {
propertyWithPrefix = t.objectProperty(t.identifier(prefix), t.objectExpression([suffixProperty]));
attributes.push(propertyWithPrefix);
} else {
propertyWithPrefix.value.properties.push(
suffixProperty
);
}
} else if (propName.endsWith("_")) {
attributes.push(t.objectProperty(t.identifier(propName.replace(/_$/, "")), prop.value));
} else {
// push to attrs property
var attrsProperty = attributes.find(p => p.key.name === "attrs");
if (!attrsProperty) {
attrsProperty = t.objectProperty(t.identifier("attrs"), t.objectExpression([prop]));
attributes.push(attrsProperty);
} else {
attrsProperty.value.properties.push(prop);
}
// attributes.push(prop);
}
});
return attributes;
//obj.properties = obj.properties.concat(attributes);
//return obj;
}
/**
* 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 containg
* all prior attributes to an array for later processing.
*/
function buildOpeningElementAttributes(attribs, file) {
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();
objs.push(prop.argument);
} else {
_props.push(convertAttribute(prop));
}
}
pushProps();
if (objs.length === 1) {
// only one object
attribs = objs[0];
attribs = t.objectExpression( groupAttributes(attribs.properties));
// group attribs based on
} else {
// looks like we have multiple objects
if (!t.isObjectExpression(objs[0])) {
objs.unshift(t.objectExpression([]));
}
// spread it
attribs = t.callExpression(file.addHelper("extends"), objs);
}
return attribs;
}
}