Merge pull request #410 from amasad/master
Add support for React pre v0.12 transform
This commit is contained in:
commit
655196f3ce
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);
|
||||
}
|
||||
|
||||
3
test/fixtures/transformation/react/compat-convert-component/actual.js
vendored
Normal file
3
test/fixtures/transformation/react/compat-convert-component/actual.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
var x = <Component foo="bar">
|
||||
<Namespace.Component />
|
||||
</Component>
|
||||
3
test/fixtures/transformation/react/compat-convert-component/expected.js
vendored
Normal file
3
test/fixtures/transformation/react/compat-convert-component/expected.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
var x = Component({
|
||||
foo: "bar"
|
||||
}, Namespace.Component(null));
|
||||
3
test/fixtures/transformation/react/compat-convert-component/options.json
vendored
Normal file
3
test/fixtures/transformation/react/compat-convert-component/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"reactCompat": true
|
||||
}
|
||||
1
test/fixtures/transformation/react/compat-convert-tags/actual.js
vendored
Normal file
1
test/fixtures/transformation/react/compat-convert-tags/actual.js
vendored
Normal file
@ -0,0 +1 @@
|
||||
var x = <div foo="bar"><font-face></font-face></div>;
|
||||
3
test/fixtures/transformation/react/compat-convert-tags/expected.js
vendored
Normal file
3
test/fixtures/transformation/react/compat-convert-tags/expected.js
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
var x = React.DOM.div({
|
||||
foo: "bar"
|
||||
}, React.DOM["font-face"](null));
|
||||
3
test/fixtures/transformation/react/compat-convert-tags/options.json
vendored
Normal file
3
test/fixtures/transformation/react/compat-convert-tags/options.json
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"reactCompat": true
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user