smartVisitor.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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 updateTopicReferenceVisitor = {
  8. PipelinePrimaryTopicReference(path) {
  9. path.replaceWith(_core.types.cloneNode(this.topicId));
  10. },
  11. PipelineTopicExpression(path) {
  12. path.skip();
  13. }
  14. };
  15. const smartVisitor = {
  16. BinaryExpression(path) {
  17. const {
  18. scope
  19. } = path;
  20. const {
  21. node
  22. } = path;
  23. const {
  24. operator,
  25. left,
  26. right
  27. } = node;
  28. if (operator !== "|>") return;
  29. const placeholder = scope.generateUidIdentifierBasedOnNode(left);
  30. scope.push({
  31. id: placeholder
  32. });
  33. let call;
  34. if (_core.types.isPipelineTopicExpression(right)) {
  35. path.get("right").traverse(updateTopicReferenceVisitor, {
  36. topicId: placeholder
  37. });
  38. call = right.expression;
  39. } else {
  40. let callee = right.callee;
  41. if (_core.types.isIdentifier(callee, {
  42. name: "eval"
  43. })) {
  44. callee = _core.types.sequenceExpression([_core.types.numericLiteral(0), callee]);
  45. }
  46. call = _core.types.callExpression(callee, [_core.types.cloneNode(placeholder)]);
  47. }
  48. path.replaceWith(_core.types.sequenceExpression([_core.types.assignmentExpression("=", _core.types.cloneNode(placeholder), left), call]));
  49. }
  50. };
  51. var _default = smartVisitor;
  52. exports.default = _default;