astrewrite

Go tool to walk & rewrite AST

  • 所有者: fatih/astrewrite
  • 平台:
  • 许可证: MIT License
  • 分类:
  • 主题:
  • 喜欢:
    0
      比较:

Github星跟踪图

This project is archived and not maintained. There is a better implementation
and solution in the
golang.org/x/tools/go/ast/astutil
package that you can use.

astrewrite GoDoc Build Status

astrewrite provides a Walk() function, similar to ast.Inspect() from the
ast package. The only difference is that the passed walk function can also
return a node, which is used to rewrite the parent node. This provides an easy
way to rewrite a given ast.Node while walking the AST.

Example

package main

import (
	"bytes"
	"fmt"
	"go/ast"
	"go/parser"
	"go/printer"
	"go/token"

	"github.com/fatih/astrewrite"
)

func main() {
	src := `package main

type Foo struct{}`

	fset := token.NewFileSet()
	file, err := parser.ParseFile(fset, "foo.go", src, parser.ParseComments)
	if err != nil {
		panic(err)
	}

	rewriteFunc := func(n ast.Node) (ast.Node, bool) {
		x, ok := n.(*ast.TypeSpec)
		if !ok {
			return n, true
		}

		// change struct type name to "Bar"
		x.Name.Name = "Bar"
		return x, true
	}

	rewritten := astrewrite.Walk(file, rewriteFunc)

	var buf bytes.Buffer
	printer.Fprint(&buf, fset, rewritten)
	fmt.Println(buf.String())
	// Output:
	// package main
	//
	// type Bar struct{}
}

主要指标

概览
名称与所有者fatih/astrewrite
主编程语言Go
编程语言Go (语言数: 1)
平台
许可证MIT License
所有者活动
创建于2017-01-10 08:36:47
推送于2019-12-07 15:40:17
最后一次提交2019-12-07 07:40:02
发布数0
用户参与
星数170
关注者数6
派生数13
提交数7
已启用问题?
问题数2
打开的问题数0
拉请求数0
打开的拉请求数0
关闭的拉请求数1
项目设置
已启用Wiki?
已存档?
是复刻?
已锁定?
是镜像?
是私有?