Documentation
¶
Overview ¶
Package astutil 提供通用 Go AST 操作库,基于 dave/dst 封装。
Package astutil 提供通用 Go AST 操作库,基于 dave/dst 封装。
Index ¶
- type Field
- type File
- func (f *File) Apply(ops ...Op)
- func (f *File) FindDecls(declType any) []dst.Decl
- func (f *File) FindFunction(name string) *dst.FuncDecl
- func (f *File) FindFunctions() []*dst.FuncDecl
- func (f *File) FindGenDecls(tok interface{ ... }) []*dst.GenDecl
- func (f *File) FindImports() []*dst.ImportSpec
- func (f *File) Format() ([]byte, error)
- func (f *File) WriteTo(path string) error
- type Op
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type File ¶
type File struct {
// contains filtered or unexported fields
}
File 表示一个可操作的 Go 源文件。
func (*File) FindDecls ¶
FindDecls 返回所有匹配指定类型的顶层声明。 declType 应为声明类型的零值,如 *dst.GenDecl 或 *dst.FuncDecl。
func (*File) FindFunction ¶
FindFunction 按名称查找函数。
func (*File) FindGenDecls ¶
FindGenDecls 返回所有匹配指定 token 类型的 GenDecl。 例如传入 token.IMPORT、token.TYPE、token.CONST、token.VAR。
type Op ¶
Op 表示一个 AST 修改操作。
func EnsureFunction ¶
EnsureFunction 确保指定函数存在,不存在则创建。 函数存在时不做任何操作,不存在时创建新的函数声明并添加到文件末尾。
func InsertAfter ¶
InsertAfter 在指定标记后插入代码。 marker 可以是以下类型:
- string:查找包含该字符串的注释标记(如 "//inject:here"),在该注释所在声明后插入
- dst.Decl:在该节点后插入(通过指针比较定位)
如果找不到标记,操作不执行(no-op)。
func ReplaceNode ¶
ReplaceNode 替换指定的 AST 节点。 通过指针比较(oldNode == decl)在 f.Decls 中定位目标节点, 找到后用 newNode 替换。如果找不到 oldNode,不执行替换(no-op)。
Click to show internal directories.
Click to hide internal directories.