hackVisitor.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _core = require("@babel/core");
  7. const topicReferenceVisitor = {
  8. exit(path, state) {
  9. if (path.isTopicReference()) {
  10. state.topicReferences.push(path);
  11. } else {
  12. if (state.topicReferences.length === 0 && !state.sideEffectsBeforeFirstTopicReference && !path.isPure()) {
  13. state.sideEffectsBeforeFirstTopicReference = true;
  14. }
  15. }
  16. },
  17. "ClassBody|Function"(_, state) {
  18. if (state.topicReferences.length === 0) {
  19. state.sideEffectsBeforeFirstTopicReference = true;
  20. }
  21. }
  22. };
  23. const visitor = {
  24. BinaryExpression: {
  25. exit(path) {
  26. const {
  27. scope,
  28. node
  29. } = path;
  30. if (node.operator !== "|>") {
  31. return;
  32. }
  33. const pipeBodyPath = path.get("right");
  34. if (pipeBodyPath.node.type === "TopicReference") {
  35. path.replaceWith(node.left);
  36. return;
  37. }
  38. const visitorState = {
  39. topicReferences: [],
  40. sideEffectsBeforeFirstTopicReference: pipeBodyPath.isFunction()
  41. };
  42. pipeBodyPath.traverse(topicReferenceVisitor, visitorState);
  43. if (visitorState.topicReferences.length === 1 && (!visitorState.sideEffectsBeforeFirstTopicReference || path.scope.isPure(node.left, true))) {
  44. visitorState.topicReferences[0].replaceWith(node.left);
  45. path.replaceWith(node.right);
  46. return;
  47. }
  48. const topicVariable = scope.generateUidIdentifierBasedOnNode(node);
  49. scope.push({
  50. id: topicVariable
  51. });
  52. visitorState.topicReferences.forEach(path => path.replaceWith(_core.types.cloneNode(topicVariable)));
  53. path.replaceWith(_core.types.sequenceExpression([_core.types.assignmentExpression("=", _core.types.cloneNode(topicVariable), node.left), node.right]));
  54. }
  55. }
  56. };
  57. var _default = visitor;
  58. exports.default = _default;