index.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _helperPluginUtils = require("@babel/helper-plugin-utils");
  7. var _helperReplaceSupers = require("@babel/helper-replace-supers");
  8. var _core = require("@babel/core");
  9. function replacePropertySuper(path, getObjectRef, file) {
  10. const replaceSupers = new _helperReplaceSupers.default({
  11. getObjectRef: getObjectRef,
  12. methodPath: path,
  13. file: file
  14. });
  15. replaceSupers.replace();
  16. }
  17. var _default = exports.default = (0, _helperPluginUtils.declare)(api => {
  18. api.assertVersion(7);
  19. const newLets = new Set();
  20. return {
  21. name: "transform-object-super",
  22. visitor: {
  23. Loop: {
  24. exit(path) {
  25. newLets.forEach(v => {
  26. if (v.scopePath === path) {
  27. path.scope.push({
  28. id: v.id,
  29. kind: "let"
  30. });
  31. path.scope.crawl();
  32. path.requeue();
  33. newLets.delete(v);
  34. }
  35. });
  36. }
  37. },
  38. ObjectExpression(path, state) {
  39. let objectRef;
  40. const getObjectRef = () => objectRef = objectRef || path.scope.generateUidIdentifier("obj");
  41. path.get("properties").forEach(propPath => {
  42. if (!propPath.isMethod()) return;
  43. replacePropertySuper(propPath, getObjectRef, state.file);
  44. });
  45. if (objectRef) {
  46. const scopePath = path.findParent(p => p.isFunction() || p.isProgram() || p.isLoop());
  47. const useLet = scopePath.isLoop();
  48. if (useLet) {
  49. newLets.add({
  50. scopePath,
  51. id: _core.types.cloneNode(objectRef)
  52. });
  53. } else {
  54. path.scope.push({
  55. id: _core.types.cloneNode(objectRef),
  56. kind: "var"
  57. });
  58. }
  59. path.replaceWith(_core.types.assignmentExpression("=", _core.types.cloneNode(objectRef), path.node));
  60. }
  61. }
  62. }
  63. };
  64. });
  65. //# sourceMappingURL=index.js.map