Dashboard sipadu mbip
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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