-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
27 lines (24 loc) · 1.05 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
module.exports.convert = function (code) {
var recast = require("recast");
var esprima = require("./esprima-fb-patched/esprima.js");
var ast = recast.parse(code, {esprima: esprima});
var b = recast.types.builders;
recast.visit(ast, {
visitObjectExpression: function (path) {
var obj = path.value;
for (var i = 0; i < obj.properties.length; i++) {
var property = obj.properties[i];
if (property.key.name === "template" && property.value.type === "JSXElement") {
var templateStr = recast.print(property.value).code;
templateStr = templateStr.replace(/(\r\n|\n|\r)/g,'\n');
templateStr = templateStr.replace(/<(.*)className="(.*)"(.*)>/gi, "<$1class=\"$2\"$3>");
property.value = b.literal(templateStr);
}
}
this.traverse(path);
}
}
);
var output = recast.print(ast).code;
return output;
}