index.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = _default;
  6. function _esutils() {
  7. const data = _interopRequireDefault(require("esutils"));
  8. _esutils = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function t() {
  14. const data = _interopRequireWildcard(require("@babel/types"));
  15. t = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. 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)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }
  21. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  22. function _default(opts) {
  23. const visitor = {};
  24. visitor.JSXNamespacedName = function (path) {
  25. if (opts.throwIfNamespace) {
  26. throw path.buildCodeFrameError(`Namespace tags are not supported by default. React's JSX doesn't support namespace tags. \
  27. You can turn on the 'throwIfNamespace' flag to bypass this warning.`);
  28. }
  29. };
  30. visitor.JSXSpreadChild = function (path) {
  31. throw path.buildCodeFrameError("Spread children are not supported in React.");
  32. };
  33. visitor.JSXElement = {
  34. exit(path, file) {
  35. const callExpr = buildElementCall(path, file);
  36. if (callExpr) {
  37. path.replaceWith(t().inherits(callExpr, path.node));
  38. }
  39. }
  40. };
  41. visitor.JSXFragment = {
  42. exit(path, file) {
  43. if (opts.compat) {
  44. throw path.buildCodeFrameError("Fragment tags are only supported in React 16 and up.");
  45. }
  46. const callExpr = buildFragmentCall(path, file);
  47. if (callExpr) {
  48. path.replaceWith(t().inherits(callExpr, path.node));
  49. }
  50. }
  51. };
  52. return visitor;
  53. function convertJSXIdentifier(node, parent) {
  54. if (t().isJSXIdentifier(node)) {
  55. if (node.name === "this" && t().isReferenced(node, parent)) {
  56. return t().thisExpression();
  57. } else if (_esutils().default.keyword.isIdentifierNameES6(node.name)) {
  58. node.type = "Identifier";
  59. } else {
  60. return t().stringLiteral(node.name);
  61. }
  62. } else if (t().isJSXMemberExpression(node)) {
  63. return t().memberExpression(convertJSXIdentifier(node.object, node), convertJSXIdentifier(node.property, node));
  64. } else if (t().isJSXNamespacedName(node)) {
  65. return t().stringLiteral(`${node.namespace.name}:${node.name.name}`);
  66. }
  67. return node;
  68. }
  69. function convertAttributeValue(node) {
  70. if (t().isJSXExpressionContainer(node)) {
  71. return node.expression;
  72. } else {
  73. return node;
  74. }
  75. }
  76. function convertAttribute(node) {
  77. const value = convertAttributeValue(node.value || t().booleanLiteral(true));
  78. if (t().isStringLiteral(value) && !t().isJSXExpressionContainer(node.value)) {
  79. value.value = value.value.replace(/\n\s+/g, " ");
  80. if (value.extra && value.extra.raw) {
  81. delete value.extra.raw;
  82. }
  83. }
  84. if (t().isJSXNamespacedName(node.name)) {
  85. node.name = t().stringLiteral(node.name.namespace.name + ":" + node.name.name.name);
  86. } else if (_esutils().default.keyword.isIdentifierNameES6(node.name.name)) {
  87. node.name.type = "Identifier";
  88. } else {
  89. node.name = t().stringLiteral(node.name.name);
  90. }
  91. return t().inherits(t().objectProperty(node.name, value), node);
  92. }
  93. function buildElementCall(path, file) {
  94. if (opts.filter && !opts.filter(path.node, file)) return;
  95. const openingPath = path.get("openingElement");
  96. openingPath.parent.children = t().react.buildChildren(openingPath.parent);
  97. const tagExpr = convertJSXIdentifier(openingPath.node.name, openingPath.node);
  98. const args = [];
  99. let tagName;
  100. if (t().isIdentifier(tagExpr)) {
  101. tagName = tagExpr.name;
  102. } else if (t().isLiteral(tagExpr)) {
  103. tagName = tagExpr.value;
  104. }
  105. const state = {
  106. tagExpr: tagExpr,
  107. tagName: tagName,
  108. args: args
  109. };
  110. if (opts.pre) {
  111. opts.pre(state, file);
  112. }
  113. let attribs = openingPath.node.attributes;
  114. if (attribs.length) {
  115. attribs = buildOpeningElementAttributes(attribs, file);
  116. } else {
  117. attribs = t().nullLiteral();
  118. }
  119. args.push(attribs, ...path.node.children);
  120. if (opts.post) {
  121. opts.post(state, file);
  122. }
  123. return state.call || t().callExpression(state.callee, args);
  124. }
  125. function pushProps(_props, objs) {
  126. if (!_props.length) return _props;
  127. objs.push(t().objectExpression(_props));
  128. return [];
  129. }
  130. function buildOpeningElementAttributes(attribs, file) {
  131. let _props = [];
  132. const objs = [];
  133. const useBuiltIns = file.opts.useBuiltIns || false;
  134. if (typeof useBuiltIns !== "boolean") {
  135. throw new Error("transform-react-jsx currently only accepts a boolean option for " + "useBuiltIns (defaults to false)");
  136. }
  137. while (attribs.length) {
  138. const prop = attribs.shift();
  139. if (t().isJSXSpreadAttribute(prop)) {
  140. _props = pushProps(_props, objs);
  141. objs.push(prop.argument);
  142. } else {
  143. _props.push(convertAttribute(prop));
  144. }
  145. }
  146. pushProps(_props, objs);
  147. if (objs.length === 1) {
  148. attribs = objs[0];
  149. } else {
  150. if (!t().isObjectExpression(objs[0])) {
  151. objs.unshift(t().objectExpression([]));
  152. }
  153. const helper = useBuiltIns ? t().memberExpression(t().identifier("Object"), t().identifier("assign")) : file.addHelper("extends");
  154. attribs = t().callExpression(helper, objs);
  155. }
  156. return attribs;
  157. }
  158. function buildFragmentCall(path, file) {
  159. if (opts.filter && !opts.filter(path.node, file)) return;
  160. const openingPath = path.get("openingElement");
  161. openingPath.parent.children = t().react.buildChildren(openingPath.parent);
  162. const args = [];
  163. const tagName = null;
  164. const tagExpr = file.get("jsxFragIdentifier")();
  165. const state = {
  166. tagExpr: tagExpr,
  167. tagName: tagName,
  168. args: args
  169. };
  170. if (opts.pre) {
  171. opts.pre(state, file);
  172. }
  173. args.push(t().nullLiteral(), ...path.node.children);
  174. if (opts.post) {
  175. opts.post(state, file);
  176. }
  177. file.set("usedFragment", true);
  178. return state.call || t().callExpression(state.callee, args);
  179. }
  180. }