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.

79 lines
2.7 KiB

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