index.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = _default;
  6. function t() {
  7. const data = _interopRequireWildcard(require("@babel/types"));
  8. t = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. 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; } }
  14. function getObjRef(node, nodes, file, scope) {
  15. let ref;
  16. if (t().isSuper(node)) {
  17. return node;
  18. } else if (t().isIdentifier(node)) {
  19. if (scope.hasBinding(node.name)) {
  20. return node;
  21. } else {
  22. ref = node;
  23. }
  24. } else if (t().isMemberExpression(node)) {
  25. ref = node.object;
  26. if (t().isSuper(ref) || t().isIdentifier(ref) && scope.hasBinding(ref.name)) {
  27. return ref;
  28. }
  29. } else {
  30. throw new Error(`We can't explode this node type ${node.type}`);
  31. }
  32. const temp = scope.generateUidIdentifierBasedOnNode(ref);
  33. scope.push({
  34. id: temp
  35. });
  36. nodes.push(t().assignmentExpression("=", t().cloneNode(temp), t().cloneNode(ref)));
  37. return temp;
  38. }
  39. function getPropRef(node, nodes, file, scope) {
  40. const prop = node.property;
  41. const key = t().toComputedKey(node, prop);
  42. if (t().isLiteral(key) && t().isPureish(key)) return key;
  43. const temp = scope.generateUidIdentifierBasedOnNode(prop);
  44. scope.push({
  45. id: temp
  46. });
  47. nodes.push(t().assignmentExpression("=", t().cloneNode(temp), t().cloneNode(prop)));
  48. return temp;
  49. }
  50. function _default(node, nodes, file, scope, allowedSingleIdent) {
  51. let obj;
  52. if (t().isIdentifier(node) && allowedSingleIdent) {
  53. obj = node;
  54. } else {
  55. obj = getObjRef(node, nodes, file, scope);
  56. }
  57. let ref, uid;
  58. if (t().isIdentifier(node)) {
  59. ref = t().cloneNode(node);
  60. uid = obj;
  61. } else {
  62. const prop = getPropRef(node, nodes, file, scope);
  63. const computed = node.computed || t().isLiteral(prop);
  64. uid = t().memberExpression(t().cloneNode(obj), t().cloneNode(prop), computed);
  65. ref = t().memberExpression(t().cloneNode(obj), t().cloneNode(prop), computed);
  66. }
  67. return {
  68. uid: uid,
  69. ref: ref
  70. };
  71. }