Add support for React pre v0.12 transform
This uses an option `reactCompat` to emit code that works with React pre v0.12
This commit is contained in:
39
lib/6to5/transformation/transformers/react.js
vendored
39
lib/6to5/transformation/transformers/react.js
vendored
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user