Merge pull request #410 from amasad/master

Add support for React pre v0.12 transform
This commit is contained in:
Sebastian McKenzie 2015-01-07 13:54:23 +11:00
commit 655196f3ce
7 changed files with 50 additions and 5 deletions

View File

@ -36,8 +36,13 @@ exports.XJSAttribute = {
}
};
var isTag = function(tagName) {
return (/^[a-z]|\-/).test(tagName);
};
exports.XJSOpeningElement = {
exit: function (node) {
exit: function (node, parent, file) {
var reactCompat = file.opts && file.opts.reactCompat;
var tagExpr = node.name;
var args = [];
@ -48,10 +53,12 @@ exports.XJSOpeningElement = {
tagName = tagExpr.value;
}
if (tagName && (/[a-z]/.exec(tagName[0]) || tagName.indexOf("-") >= 0)) {
args.push(t.literal(tagName));
} else {
args.push(tagExpr);
if (!reactCompat) {
if (tagName && isTag(tagName)) {
args.push(t.literal(tagName));
} else {
args.push(tagExpr);
}
}
var props = node.attributes;
@ -96,6 +103,28 @@ exports.XJSOpeningElement = {
args.push(props);
if (reactCompat) {
if (tagName && isTag(tagName)) {
return t.callExpression(
t.memberExpression(
t.memberExpression(
t.identifier('React'),
t.identifier('DOM'),
false
),
tagExpr,
t.isLiteral(tagExpr)
),
args
);
} else {
return t.callExpression(
tagExpr,
args
);
}
}
tagExpr = t.memberExpression(t.identifier("React"), t.identifier("createElement"));
return t.callExpression(tagExpr, args);
}

View File

@ -0,0 +1,3 @@
var x = <Component foo="bar">
<Namespace.Component />
</Component>

View File

@ -0,0 +1,3 @@
var x = Component({
foo: "bar"
}, Namespace.Component(null));

View File

@ -0,0 +1,3 @@
{
"reactCompat": true
}

View File

@ -0,0 +1 @@
var x = <div foo="bar"><font-face></font-face></div>;

View File

@ -0,0 +1,3 @@
var x = React.DOM.div({
foo: "bar"
}, React.DOM["font-face"](null));

View File

@ -0,0 +1,3 @@
{
"reactCompat": true
}