| 1 | import { types as t, type PluginPass } from "@babel/core"; |
| 2 | import type { NodePath, Visitor } from "@babel/traverse"; |
| 3 | import buildOptimizedSequenceExpression from "./buildOptimizedSequenceExpression"; |
| 4 | |
| 5 | const minimalVisitor: Visitor<PluginPass> = { |
| 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 = t.callExpression(right, [t.cloneNode(placeholder)]); |
| 14 | path.replaceWith( |
| 15 | buildOptimizedSequenceExpression({ |
| 16 | placeholder, |
| 17 | call, |
| 18 | path: path as NodePath<t.BinaryExpression & { operator: "|>" }>, |
| 19 | }), |
| 20 | ); |
| 21 | }, |
| 22 | }; |
| 23 | |
| 24 | export default minimalVisitor; |