diff --git a/lib/babel/transformation/helpers/build-react-transformer.js b/lib/babel/transformation/helpers/build-react-transformer.js index 85b865bf44..3076873888 100644 --- a/lib/babel/transformation/helpers/build-react-transformer.js +++ b/lib/babel/transformation/helpers/build-react-transformer.js @@ -73,7 +73,7 @@ module.exports = function (exports, opts) { }; if (opts.pre) { - opts.pre(state); + opts.pre(state, file); } var attribs = node.attributes; @@ -86,7 +86,7 @@ module.exports = function (exports, opts) { args.push(attribs); if (opts.post) { - opts.post(state); + opts.post(state, file); } return state.call || t.callExpression(state.callee, args); diff --git a/lib/babel/transformation/transformers/other/react.js b/lib/babel/transformation/transformers/other/react.js index fcfc7168b7..b8d7a727fb 100644 --- a/lib/babel/transformation/transformers/other/react.js +++ b/lib/babel/transformation/transformers/other/react.js @@ -3,6 +3,22 @@ var react = require("../../helpers/react"); var t = require("../../../types"); +var JSX_ANNOTATION_REGEX = /^\*\s*@jsx\s+([^\s]+)/; + +exports.Program = function (node, parent, scope, file) { + var id = "React.createElement"; + + var comment = file.ast.comments[0]; + if (comment) { + var matches = JSX_ANNOTATION_REGEX.exec(comment.value); + if (matches) id = matches[1]; + } + + file.set("jsxIdentifier", id.split(".").map(t.identifier).reduce(function (object, property) { + return t.memberExpression(object, property); + })); +}; + require("../../helpers/build-react-transformer")(exports, { pre: function (state) { var tagName = state.tagName; @@ -14,7 +30,7 @@ require("../../helpers/build-react-transformer")(exports, { } }, - post: function (state) { - state.callee = t.memberExpression(t.identifier("React"), t.identifier("createElement")); + post: function (state, file) { + state.callee = file.get("jsxIdentifier"); } }); diff --git a/test/fixtures/transformation/react/honor-custom-jsx-comment/actual.js b/test/fixtures/transformation/react/honor-custom-jsx-comment/actual.js new file mode 100644 index 0000000000..acb0d0ca1b --- /dev/null +++ b/test/fixtures/transformation/react/honor-custom-jsx-comment/actual.js @@ -0,0 +1,8 @@ +/** @jsx dom */ + +; + +var profile =
+ +

{[user.firstName, user.lastName].join(" ")}

+
; diff --git a/test/fixtures/transformation/react/honor-custom-jsx-comment/expected.js b/test/fixtures/transformation/react/honor-custom-jsx-comment/expected.js new file mode 100644 index 0000000000..1febfa3188 --- /dev/null +++ b/test/fixtures/transformation/react/honor-custom-jsx-comment/expected.js @@ -0,0 +1,14 @@ +/** @jsx dom */ + +dom(Foo, null); + +var profile = dom( + "div", + null, + dom("img", { src: "avatar.png", className: "profile" }), + dom( + "h3", + null, + [user.firstName, user.lastName].join(" ") + ) +);