| 1 | import { types as t, type PluginObject } from "@babel/core"; |
| 2 | import type { NodePath } from "@babel/traverse"; |
| 3 | import buildOptimizedSequenceExpression from "./buildOptimizedSequenceExpression"; |
| 4 | |
| 5 | const fsharpVisitor: PluginObject["visitor"] = { |
| 6 | BinaryExpression(path) { |
| 7 | const { scope, node } = path; |
| 8 | const { operator, left, right } = node; |
| 9 | if (operator !== "|>") return; |
| 10 | |
| 11 | const placeholder = scope.generateUidIdentifierBasedOnNode(left); |
| 12 | |
| 13 | const call = |
| 14 | right.type === "AwaitExpression" |
| 15 | ? t.awaitExpression(t.cloneNode(placeholder)) |
| 16 | : t.callExpression(right, [t.cloneNode(placeholder)]); |
| 17 | const sequence = buildOptimizedSequenceExpression({ |
| 18 | placeholder, |
| 19 | call, |
| 20 | path: path as NodePath<t.BinaryExpression & { operator: "|>" }>, |
| 21 | }); |
| 22 | path.replaceWith(sequence); |
| 23 | }, |
| 24 | }; |
| 25 | |
| 26 | export default fsharpVisitor; |