Skip to content

Go: add GoTemplate, GoPattern, MethodMatcher, TypeUtils, and rename module#7235

Merged
jkschneider merged 2 commits intomainfrom
rewrite-go-template-matcher-module-rename
Apr 1, 2026
Merged

Go: add GoTemplate, GoPattern, MethodMatcher, TypeUtils, and rename module#7235
jkschneider merged 2 commits intomainfrom
rewrite-go-template-matcher-module-rename

Conversation

@jkschneider
Copy link
Copy Markdown
Member

Summary

Adds comprehensive Go recipe authoring infrastructure to rewrite-go:

  • GoTemplate/GoPattern (pkg/template/): Refaster-like template matching and replacement for Go. Supports captures, multiple before patterns (anyOf equivalent), scaffold parsing, structural comparison, and a NewRecipe() declarative builder that reduces recipe authoring from 50+ lines to ~10 lines.

  • MethodMatcher (pkg/matcher/): AspectJ-style pattern matching for Go method invocations (e.g., "fmt Sprintf(..)") with glob wildcards and type-aware matching.

  • TypeUtils (pkg/matcher/): Type checking utilities — IsAssignableTo(), IsOfClassType(), IsError(), IsString(), AsClass(), TypeOfExpression(), DeclaringTypeFQN().

  • Markup helpers (pkg/tree/markers.go): MarkupWarn(), MarkupInfo(), MarkupError() convenience functions for severity-level diagnostics on LST nodes.

  • GoVisitor completeness: Fixed 7 visit methods to properly recurse into children (VariableDeclarations, VariableDeclarator, MethodInvocation, Return, If, Assignment, AssignmentOperation).

  • Parser fix: Fixed comma-in-whitespace bug in mapReturnStmt — commas between return expressions now properly consumed.

  • Composite recipe support: test.RewriteRun now handles RecipeList().

  • Module rename: github.com/openrewrite/rewritegithub.com/openrewrite/rewrite/rewrite-go for proper Go submodule publishing within the monorepo.

Test plan

  • All existing rewrite-go tests pass (parser, printer, visitor, recipe, template, matcher)
  • 207 downstream recipes in moderneinc/recipes-go build and pass 526 tests
  • Validation against 5 real OSS Go repos (gorilla/mux, spf13/cobra, sirupsen/logrus, go-chi/chi, labstack/echo) — 0 panics, 0 corrupt output

…odule

Adds comprehensive Go recipe infrastructure to rewrite-go:

- **GoTemplate/GoPattern** (`pkg/template/`): Refaster-like template matching and
  replacement for Go. Supports captures, multiple before patterns (anyOf), scaffold
  parsing, structural comparison, and a `NewRecipe()` builder for declarative recipes.

- **MethodMatcher** (`pkg/matcher/`): AspectJ-style pattern matching for Go method
  invocations (e.g., `"fmt Sprintf(..)"`) with glob wildcards and type-aware matching.

- **TypeUtils** (`pkg/matcher/`): Type checking utilities — `IsAssignableTo()`,
  `IsOfClassType()`, `IsError()`, `IsString()`, `AsClass()`, `TypeOfExpression()`,
  `DeclaringTypeFQN()`.

- **Markup helpers** (`pkg/tree/markers.go`): `MarkupWarn()`, `MarkupInfo()`,
  `MarkupError()` convenience functions for attaching severity-level diagnostics
  to LST nodes.

- **GoVisitor completeness**: Fixed 7 visit methods to properly recurse into
  children (VariableDeclarations, VariableDeclarator, MethodInvocation, Return,
  If, Assignment, AssignmentOperation).

- **Parser fix**: Fixed comma-in-whitespace bug in `mapReturnStmt` — commas between
  return expressions are now properly consumed and stored in `RightPadded.After`.

- **Composite recipe support**: `test.RewriteRun` now handles `RecipeList()` sub-recipes.

- **Module rename**: Changed module path from `github.com/openrewrite/rewrite` to
  `github.com/openrewrite/rewrite/rewrite-go` for proper Go submodule publishing
  within the monorepo.
@jkschneider jkschneider merged commit f136dfb into main Apr 1, 2026
1 check passed
@jkschneider jkschneider deleted the rewrite-go-template-matcher-module-rename branch April 1, 2026 14:29
@github-project-automation github-project-automation bot moved this from In Progress to Done in OpenRewrite Apr 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

1 participant