Skip to content

Go: add cursor helpers for function-body and init-wrapped-if checks - #8458

Open
bmuschko wants to merge 2 commits into
mainfrom
bmuschko/rewrite-go-in-repo
Open

Go: add cursor helpers for function-body and init-wrapped-if checks#8458
bmuschko wants to merge 2 commits into
mainfrom
bmuschko/rewrite-go-in-repo

Conversation

@bmuschko

@bmuschko bmuschko commented Aug 11, 2026

Copy link
Copy Markdown
Contributor

What

Adds two reusable cursor predicates to rewrite-go pkg/visitor:

  • IsFunctionBodyBlock(c *Cursor) bool — reports whether the block at the cursor is a function's body rather than a nested block such as a loop or if body.
  • IsInitWrappedIf(c *Cursor) bool — reports whether the If at the cursor is the inner statement of a golang.StatementWithInit (an if init; cond form).

Why

This is the follow-up from moderneinc/recipes-go#53 (comment), where these helpers were flagged as generic enough to belong upstream rather than duplicated in a downstream recipe repo. Hosting them in the visitor package lets recipes across languages share them. The package already imports both java and golang, so no new import cycle is introduced.

Not upstreamed

  • BaseIndent — not needed. rewrite-go already exposes the identical logic as Space.Indent() in pkg/tree/java/space.go, so callers should use that instead of a duplicate.
  • IsErrNotNil — stays local to the recipe repo, as it is recipe-specific rather than generic LST navigation.

@greg-at-moderne greg-at-moderne left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what is meant by:

Hosting them in the visitor package lets recipes across languages share them.

in the PR description. But feel free to ignore this comment.

"github.com/openrewrite/rewrite/rewrite-go/pkg/tree/java"
)

func TestIsFunctionBodyBlock(t *testing.T) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am wondering whether we should have more "end-to-end" tests - i.e. a test which would involve parsing a piece of code and assert on count of matches or similar.
As is, the test is rather trivial as it follows the implementation internals.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. Changed.

@bmuschko

Copy link
Copy Markdown
Contributor Author

Hosting them in the visitor package lets recipes across languages share them.

What I was trying to say is that by putting these helpers in the shared pkg/visitor package instead of inside a specific recipe package, any recipe can reuse them rather than each copying its own version (which is what happened in recipes-go).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

2 participants