asttools

package
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FindStmtInBody

func FindStmtInBody(stmt dst.Stmt, body []dst.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 GenerateUniqueName added in v1.2.2

func GenerateUniqueName(scope *types.Scope, base string) string

Generates a unique name for a variable in the given scope by appending a number to the base name until it is unique. For example, if the base name is "x" and a variable named "x" is already defined in the scope, the next attempt will be "x1", then "x2", and so on. If the base name is not already defined, it will be returned without modification.

func GetEnclosingFile

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

Returns the AST 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 AST 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 *dst.FuncDecl, paramTypes ...dst.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 dst.Stmt, parentBody, newBody []dst.Stmt) (dst.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 IsNameUsed added in v1.2.2

func IsNameUsed(scope *types.Scope, name string) bool

Returns whether a variable with the given name is used in the given scope, any of the scope's parents (upward), or any nested scopes (downward). This indicates whether the name is at risk of being redeclared or shadowed.

func IsPointer added in v1.2.2

func IsPointer(t types.Type) bool

Returns whether a Type's underlying Type is a pointer.

func IsSelectorFuncCall

func IsSelectorFuncCall(stmt dst.Stmt, owner, name string) (bool, *dst.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 `dst.CallExpr` if the statement matches.

func MatchSelectorExpr

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

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

func NewCallExprStmt

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

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

func NewSelectorExpr

func NewSelectorExpr(owner, name string) dst.Expr

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

func NodeToString

func NodeToString(node dst.Node) string

Converts a DST node to a string representation, or returns an empty string if formatting fails todo CLEANUP should return actual errors

func ReplaceFuncDecl

func ReplaceFuncDecl(old, new *dst.FuncDecl, file *dst.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 *dst.File) error

Saves the contents of the specified DST file to the disk using the specified path, overwriting any existing data at the specified path.

func StringToNode

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

Parses a string (usually from JSON) into the corresponding DST expression. This function tries to parse the string as a declaration, statement, or expression in that order. TODO DST - might need to use an existing decorator here to avoid discarding the fset

func UnderlyingType added in v1.2.2

func UnderlyingType(t types.Type) types.Type

Returns the underlying type of a type, unwrapping any pointer types.

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
⌂ 换网址