index.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. "use strict";
  2. exports.__esModule = true;
  3. exports.default = function (opts) {
  4. var visitor = {};
  5. visitor.JSXNamespacedName = function (path) {
  6. throw path.buildCodeFrameError("Namespace tags are not supported. ReactJSX is not XML.");
  7. };
  8. visitor.JSXElement = {
  9. exit: function exit(path, file) {
  10. var callExpr = buildElementCall(path.get("openingElement"), file);
  11. callExpr.arguments = callExpr.arguments.concat(path.node.children);
  12. if (callExpr.arguments.length >= 3) {
  13. callExpr._prettyCall = true;
  14. }
  15. path.replaceWith(t.inherits(callExpr, path.node));
  16. }
  17. };
  18. return visitor;
  19. function convertJSXIdentifier(node, parent) {
  20. if (t.isJSXIdentifier(node)) {
  21. if (node.name === "this" && t.isReferenced(node, parent)) {
  22. return t.thisExpression();
  23. } else if (_esutils2.default.keyword.isIdentifierNameES6(node.name)) {
  24. node.type = "Identifier";
  25. } else {
  26. return t.stringLiteral(node.name);
  27. }
  28. } else if (t.isJSXMemberExpression(node)) {
  29. return t.memberExpression(convertJSXIdentifier(node.object, node), convertJSXIdentifier(node.property, node));
  30. }
  31. return node;
  32. }
  33. function convertAttributeValue(node) {
  34. if (t.isJSXExpressionContainer(node)) {
  35. return node.expression;
  36. } else {
  37. return node;
  38. }
  39. }
  40. function convertAttribute(node) {
  41. var value = convertAttributeValue(node.value || t.booleanLiteral(true));
  42. if (t.isStringLiteral(value) && !t.isJSXExpressionContainer(node.value)) {
  43. value.value = value.value.replace(/\n\s+/g, " ");
  44. }
  45. if (t.isValidIdentifier(node.name.name)) {
  46. node.name.type = "Identifier";
  47. } else {
  48. node.name = t.stringLiteral(node.name.name);
  49. }
  50. return t.inherits(t.objectProperty(node.name, value), node);
  51. }
  52. function buildElementCall(path, file) {
  53. path.parent.children = t.react.buildChildren(path.parent);
  54. var tagExpr = convertJSXIdentifier(path.node.name, path.node);
  55. var args = [];
  56. var tagName = void 0;
  57. if (t.isIdentifier(tagExpr)) {
  58. tagName = tagExpr.name;
  59. } else if (t.isLiteral(tagExpr)) {
  60. tagName = tagExpr.value;
  61. }
  62. var state = {
  63. tagExpr: tagExpr,
  64. tagName: tagName,
  65. args: args
  66. };
  67. if (opts.pre) {
  68. opts.pre(state, file);
  69. }
  70. var attribs = path.node.attributes;
  71. if (attribs.length) {
  72. attribs = buildOpeningElementAttributes(attribs, file);
  73. } else {
  74. attribs = t.nullLiteral();
  75. }
  76. args.push(attribs);
  77. if (opts.post) {
  78. opts.post(state, file);
  79. }
  80. return state.call || t.callExpression(state.callee, args);
  81. }
  82. function buildOpeningElementAttributes(attribs, file) {
  83. var _props = [];
  84. var objs = [];
  85. var useBuiltIns = file.opts.useBuiltIns || false;
  86. if (typeof useBuiltIns !== "boolean") {
  87. throw new Error("transform-react-jsx currently only accepts a boolean option for " + "useBuiltIns (defaults to false)");
  88. }
  89. function pushProps() {
  90. if (!_props.length) return;
  91. objs.push(t.objectExpression(_props));
  92. _props = [];
  93. }
  94. while (attribs.length) {
  95. var prop = attribs.shift();
  96. if (t.isJSXSpreadAttribute(prop)) {
  97. pushProps();
  98. objs.push(prop.argument);
  99. } else {
  100. _props.push(convertAttribute(prop));
  101. }
  102. }
  103. pushProps();
  104. if (objs.length === 1) {
  105. attribs = objs[0];
  106. } else {
  107. if (!t.isObjectExpression(objs[0])) {
  108. objs.unshift(t.objectExpression([]));
  109. }
  110. var helper = useBuiltIns ? t.memberExpression(t.identifier("Object"), t.identifier("assign")) : file.addHelper("extends");
  111. attribs = t.callExpression(helper, objs);
  112. }
  113. return attribs;
  114. }
  115. };
  116. var _esutils = require("esutils");
  117. var _esutils2 = _interopRequireDefault(_esutils);
  118. var _babelTypes = require("babel-types");
  119. var t = _interopRequireWildcard(_babelTypes);
  120. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
  121. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  122. module.exports = exports["default"];