ReconstructPipingFlow reconstructs a piping flow into a string expression by applying transformations sequentially. For example: subject with transformations [f, g] becomes "g(f(subject))". Note: Effect.fn and Effect.fnUntraced transformations cannot be reconstructed as a chain since they are part
(sf *ast.SourceFile, subject *PipingFlowSubject, transformations []*PipingFlowTransformation)
| 29 | // as a chain since they are part of the Effect.fn call itself. In this case, |
| 30 | // the original subject node text is returned. |
| 31 | func ReconstructPipingFlow(sf *ast.SourceFile, subject *PipingFlowSubject, transformations []*PipingFlowTransformation) string { |
| 32 | if sf == nil || subject == nil { |
| 33 | return "" |
| 34 | } |
| 35 | |
| 36 | // Check if all transformations are effectFn or effectFnUntraced. |
| 37 | // In this case, reconstruction is not possible - return the original node text. |
| 38 | if len(transformations) > 0 { |
| 39 | allEffectFn := true |
| 40 | for _, t := range transformations { |
| 41 | if t.Kind != TransformationKindEffectFn && t.Kind != TransformationKindEffectFnUntraced { |
| 42 | allEffectFn = false |
| 43 | break |
| 44 | } |
| 45 | } |
| 46 | if allEffectFn { |
| 47 | return nodeText(sf, subject.Node) |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | result := nodeText(sf, subject.Node) |
| 52 | |
| 53 | for _, t := range transformations { |
| 54 | if t.Kind == TransformationKindEffectFn || t.Kind == TransformationKindEffectFnUntraced { |
| 55 | // Effect.fn transformations cannot be reconstructed as part of a chain |
| 56 | continue |
| 57 | } |
| 58 | |
| 59 | calleeText := nodeText(sf, t.Callee) |
| 60 | |
| 61 | if t.Kind == TransformationKindCall { |
| 62 | // Single-arg call: callee(result) |
| 63 | result = calleeText + "(" + result + ")" |
| 64 | } else { |
| 65 | // Pipe or pipeable: apply the transformation |
| 66 | if len(t.Args) > 0 { |
| 67 | // Curried form: callee(args...)(result) |
| 68 | var argsText strings.Builder |
| 69 | for i, arg := range t.Args { |
| 70 | if i > 0 { |
| 71 | argsText.WriteString(", ") |
| 72 | } |
| 73 | argsText.WriteString(nodeText(sf, arg)) |
| 74 | } |
| 75 | result = calleeText + "(" + argsText.String() + ")(" + result + ")" |
| 76 | } else { |
| 77 | // Constant: callee(result) |
| 78 | result = calleeText + "(" + result + ")" |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | return result |
| 84 | } |