Go: add cursor helpers for function-body and init-wrapped-if checks - #8458
Go: add cursor helpers for function-body and init-wrapped-if checks#8458bmuschko wants to merge 2 commits into
Conversation
greg-at-moderne
left a comment
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Makes sense. Changed.
What I was trying to say is that by putting these helpers in the shared |
What
Adds two reusable cursor predicates to
rewrite-gopkg/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 orifbody.IsInitWrappedIf(c *Cursor) bool— reports whether theIfat the cursor is the inner statement of agolang.StatementWithInit(anif init; condform).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
visitorpackage lets recipes across languages share them. The package already imports bothjavaandgolang, so no new import cycle is introduced.Not upstreamed
BaseIndent— not needed.rewrite-goalready exposes the identical logic asSpace.Indent()inpkg/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.