Skip to content

Extract shared walkStatements helper for microflow AST walkers #463

@hjotha

Description

@hjotha

Source

Follow-up from ako's review on PR #337 (merged as part of open PR).

Problem

Several microflow builder passes open-code the same recursive AST walk over MicroflowStatement bodies, each with a nearly identical switch on statement types. Concrete examples:

  • `planMicroflowCallOutputDeclarations` (inner `walk`) in `cmd_microflows_builder.go`
  • `countMicroflowCallOutputs` in the same file
  • `collectListInputVariables` / `collectObjectInputVariables` in `cmd_microflows_builder_graph.go`
  • `statementVarRefs` / `statementsVarRefs` in `cmd_microflows_builder_varutil.go`
  • `bodyReturns` in `validate_microflow.go`

Each copy is ~40 lines of switch boilerplate with an `ErrorHandling` recurse arm. When a new statement type gains an `ErrorHandling` field or nested body, it must be added in every place — this is how the bugs in PR #337 (issue #405) and PR #362 surfaced.

Expected behavior

A single shared helper:

```go
func walkStatements(body []ast.MicroflowStatement, fn func(ast.MicroflowStatement))
```

does the recursion (including `IfStmt`/`LoopStmt`/`WhileStmt` bodies and every `ErrorHandling.Body`). Each caller supplies the per-node callback only. Adding a new statement type with its own body requires exactly one edit in one place.

Proposed fix

  1. Introduce `walkStatements(body, fn)` in `cmd_microflows_builder_varutil.go` (or a new `cmd_microflows_builder_walk.go`).
  2. Rewrite the existing walkers to call it with their per-node logic.
  3. Keep `statementVarRefs` as is — that's the per-node logic, not the walker.

Related

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions