Package rewrite can rewrite a Go AST node given a rewriter function
Go
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
testdata
README.md
doc.go
example_test.go
rewrite.go
rewrite_test.go

README.md

Package rewrite

GoDoc

Package rewrite can recursively rewrite a Go AST given a rewriter function.

The following example splits an asignment x, y := 0, 1 to two assignments x := 0 and y := 1

src := "func() { x, y := 0, 1 }"
node, err := parser.ParseExpr(src)
if err != nil {
    panic(err)
}

node = rewrite.Rewrite(node, func(node ast.Node) (ast.Node, bool) {
    if assign, ok := node.(*AssignStmt); ok {
        if len(assign.Lhs) > 1 && assign.Tok == token.DEFINE {
            var block rewrite.ExpandedBlockStmt
            for i := range assign.Lhs {
                block.List = append(block.List, &AssignStmt{
                    Lhs: []Expr{assign.Lhs[i]},
                    Tok: token.DEFINE,
                    Rhs: []Expr{assign.Rhs[i]},
                })
            }
            node = &block
        }
    }
    return node, true
}).(Expr)

format.Node(os.Stdout, token.NewFileSet(), node)

Output:

func() {
    x := 0
    y := 1
}