| 1 | import { types as t } from "@babel/core"; |
| 2 | import type { NodePath, Visitor } from "@babel/traverse"; |
| 3 | import type { PluginPass } from "@babel/core"; |
| 4 | |
| 5 | type State = { |
| 6 | topicReferences: NodePath<t.TopicReference>[]; |
| 7 | sideEffectsBeforeFirstTopicReference: boolean; |
| 8 | }; |
| 9 | |
| 10 | const topicReferenceVisitor: Visitor<State> = { |
| 11 | exit(path, state) { |
| 12 | if (path.isTopicReference()) { |
| 13 | state.topicReferences.push(path); |
| 14 | } else { |
| 15 | if ( |
| 16 | state.topicReferences.length === 0 && |
| 17 | !state.sideEffectsBeforeFirstTopicReference && |
| 18 | !path.isPure() |
| 19 | ) { |
| 20 | state.sideEffectsBeforeFirstTopicReference = true; |
| 21 | } |
| 22 | } |
| 23 | }, |
| 24 | "ClassBody|Function"(_, state) { |
| 25 | if (state.topicReferences.length === 0) { |
| 26 | state.sideEffectsBeforeFirstTopicReference = true; |
| 27 | } |
| 28 | }, |
| 29 | }; |
| 30 | |
| 31 | // This visitor traverses `BinaryExpression` |
| 32 | // and replaces any that use `|>` |
| 33 | // with sequence expressions containing assignment expressions |
| 34 | // with automatically generated variables, |
| 35 | // from inside to outside, from left to right. |
| 36 | const visitor: Visitor<PluginPass> = { |
| 37 | BinaryExpression: { |
| 38 | exit(path) { |
| 39 | const { scope, node } = path; |
| 40 | |
| 41 | if (node.operator !== "|>") { |
| 42 | // The path node is a binary expression, |
| 43 | // but it is not a pipe expression. |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | const pipeBodyPath = path.get("right"); |
| 48 | if (pipeBodyPath.node.type === "TopicReference") { |
| 49 | // If the pipe body is itself a lone topic reference, |
| 50 | // then replace the whole expression with its left operand. |
| 51 | path.replaceWith(node.left); |
| 52 | return; |
| 53 | } |
| 54 | |
| 55 | const visitorState: State = { |
| 56 | topicReferences: [], |
| 57 | // pipeBodyPath might be a function, and it won't be visited by |
| 58 | // topicReferenceVisitor because traverse() skips the top-level |
| 59 | // node. We must handle that case here manually. |
| 60 | sideEffectsBeforeFirstTopicReference: pipeBodyPath.isFunction(), |
| 61 | }; |
| 62 | pipeBodyPath.traverse(topicReferenceVisitor, visitorState); |
| 63 | |
| 64 | if ( |
| 65 | visitorState.topicReferences.length === 1 && |
| 66 | (!visitorState.sideEffectsBeforeFirstTopicReference || |
| 67 | path.scope.isPure(node.left, true)) |
| 68 | ) { |
| 69 | visitorState.topicReferences[0].replaceWith(node.left); |
| 70 | path.replaceWith(node.right); |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | const topicVariable = scope.generateUidIdentifierBasedOnNode(node); |
| 75 | scope.push({ id: topicVariable }); |
| 76 | |
| 77 | // Replace topic references with the topic variable. |
| 78 | visitorState.topicReferences.forEach(path => |
| 79 | path.replaceWith(t.cloneNode(topicVariable)), |
| 80 | ); |
| 81 | |
| 82 | // Replace the pipe expression itself with an assignment expression. |
| 83 | path.replaceWith( |
| 84 | t.sequenceExpression([ |
| 85 | t.assignmentExpression( |
| 86 | "=", |
| 87 | t.cloneNode(topicVariable), |
| 88 | // @ts-expect-error node.left must not be a PrivateName when operator is |> |
| 89 | node.left, |
| 90 | ), |
| 91 | node.right, |
| 92 | ]), |
| 93 | ); |
| 94 | }, |
| 95 | }, |
| 96 | }; |
| 97 | |
| 98 | export default visitor; |