asttools

package
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 5, 2025 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FindStmtInBody

func FindStmtInBody(stmt ast.Stmt, body []ast.Stmt) (int, error)

Returns the index of the given statement within a function body, or an error if the statement is not found. The contents of the statement (but not necessarily its underlying pointers) must exactly match a statement in the provided body.

func GetEnclosingFile

func GetEnclosingFile(pos token.Pos, packageFiles []*ast.File) *ast.File

Returns the file containing the given position in the provided package files. Returns nil if none of the provided files contain the position.

func GetEnclosingFunction

func GetEnclosingFunction(pos token.Pos, packageFiles []*ast.File) (*ast.FuncDecl, *ast.File)

Returns the function declaration (and corresponding file) enclosing the given position in the provided package files. Returns nil if no function declaration is found, or if none of the provided files contain the position.

func GetParamNameByType

func GetParamNameByType(funcDecl *ast.FuncDecl, paramTypes ...ast.Expr) (string, error)

Returns the name of the first detected parameter in the function declaration that exactly matches any of the provided parameter types. If no matching parameter is found, returns an error.

func GetStmtWithSameIndex

func GetStmtWithSameIndex(stmt ast.Stmt, parentBody, newBody []ast.Stmt) (ast.Stmt, error)

Returns the i-th statement in the new body, where i is the index of the provided statement within its own parent body. For example, if the given statement is at index 2 in its parent body, this returns the statement at index 2 in the new body.

func IsBasicType

func IsBasicType(typ types.Type, info types.BasicInfo) bool

Returns whether a Type is Basic and has the specified info. See `go/types.Basic` for more details.

func IsSelectorFuncCall

func IsSelectorFuncCall(stmt ast.Stmt, owner, name string) (bool, *ast.CallExpr)

Returns a boolean indicating whether a statement is a function call expression of the form `owner.name(...)`, as well as a reference to the `ast.CallExpr` if the statement matches.

func MatchSelectorExpr

func MatchSelectorExpr(expr ast.Expr, owner, name string) bool

Returns a boolean indicating whether a selector expression has the form `owner.name`.

func NewCallExprStmt

func NewCallExprStmt(fun ast.Expr, args []ast.Expr) *ast.ExprStmt

Creates a call expression statement using the provided function and arguments.

func NewSelectorExpr

func NewSelectorExpr(owner, name string) ast.Expr

Creates a selector expression of the form `owner.name`.

func NodeToString

func NodeToString(node ast.Node, fset *token.FileSet) string

Converts an AST node to a string representation using `go/printer`, or return an error string if formatting fails todo CLEANUP should return actual errors

func ReplaceFuncDecl

func ReplaceFuncDecl(old, new *ast.FuncDecl, file *ast.File) error

Replace the reference to the `old` FuncDecl in its parent file with a reference to the `new` FuncDecl, without modifying either of the FuncDecls themselves. The function must be a top-level declaration in the file. Note that the contents of the functions are not compared, only their names. Returns an error if the replacement was not successful.

func SaveFileContents

func SaveFileContents(path string, newFile *ast.File, fset *token.FileSet) error

Saves the contents of the specified AST file to the disk using the specified path, after formatting the AST data with `go/format` using the provided FileSet. Any existing file at the specified path will be overwritten.

func StringToNode

func StringToNode(str string) (ast.Node, error)

Parses a string (usually from JSON) into the corresponding AST expression. This function tries to parse the string as a declaration, statement, or expression in that order.

func Unpointer

func Unpointer(t types.Type) types.Type

Returns T given *T or an alias of *T. For all other types it is the identity function. [copied from `go/typesinternal` package]

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL