Documentation ¶
Index ¶
- func Apply(node ast.Node, before, after func(Cursor) bool) ast.Node
- func ApplyRecursively(n ast.Node) ast.Node
- func CopyComments(to, from ast.Node)
- func CopyMeta(to, from ast.Node) ast.Node
- func CopyPosition(to, from ast.Node)
- func Resolve(f *ast.File, errFn ErrFunc)
- func ResolveExpr(e ast.Expr, errFn ErrFunc)
- func Sanitize(f *ast.File) error
- func ToFile(x ast.Expr) (*ast.File, error)
- type Cursor
- type ErrFunc
- type ImportInfo
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Apply ¶
Apply traverses a syntax tree recursively, starting with root, and calling pre and post for each node as described below. Apply returns the syntax tree, possibly modified.
If pre is not nil, it is called for each node before the node's children are traversed (pre-order). If pre returns false, no children are traversed, and post is not called for that node.
If post is not nil, and a prior call of pre didn't return false, post is called for each node after its children are traversed (post-order). If post returns false, traversal is terminated and Apply returns immediately.
Only fields that refer to AST nodes are considered children; i.e., token.Pos, Scopes, Objects, and fields of basic types (strings, etc.) are ignored.
Children are traversed in the order in which they appear in the respective node's struct definition.
func ApplyRecursively ¶ added in v0.0.12
ApplyRecursively indicates that a node inserted with InsertBefore, or InsertAfter should be processed recursively.
func CopyComments ¶
CopyComments associates comments of one node with another. It may change the relative position of comments.
func CopyMeta ¶
CopyMeta copies comments and position information from one node to another. It returns the destination node.
func CopyPosition ¶
CopyPosition sets the position of one node to another.
func Resolve ¶
Resolve resolves all identifiers in a file. Unresolved identifiers are recorded in Unresolved. It will not overwrite already resolved values.
func ResolveExpr ¶
Resolve resolves all identifiers in an expression. It will not overwrite already resolved values.
Types ¶
type Cursor ¶
type Cursor interface { // Node returns the current Node. Node() ast.Node // Parent returns the parent of the current Node. Parent() Cursor // Index reports the index >= 0 of the current Node in the slice of Nodes // that contains it, or a value < 0 if the current Node is not part of a // list. Index() int // Import reports an opaque identifier that refers to the given package. It // may only be called if the input to apply was an ast.File. If the import // does not exist, it will be added. Import(path string) *ast.Ident // Replace replaces the current Node with n. // The replacement node is not walked by Apply. Comments of the old node // are copied to the new node if it has not yet an comments associated // with it. Replace(n ast.Node) // Delete deletes the current Node from its containing struct. // If the current Node is not part of a struct, Delete panics. Delete() // InsertAfter inserts n after the current Node in its containing struct. // If the current Node is not part of a struct, InsertAfter panics. // Unless n is wrapped by ApplyRecursively, Apply does not walk n. InsertAfter(n ast.Node) // InsertBefore inserts n before the current Node in its containing struct. // If the current Node is not part of a struct, InsertBefore panics. // Unless n is wrapped by ApplyRecursively, Apply does not walk n. InsertBefore(n ast.Node) // contains filtered or unexported methods }
A Cursor describes a node encountered during Apply. Information about the node and its parent is available from the Node, Parent, and Index methods.
The methods Replace, Delete, InsertBefore, and InsertAfter can be used to change the AST without disrupting Apply. Delete, InsertBefore, and InsertAfter are only defined for modifying a StructLit and will panic in any other context.
type ImportInfo ¶ added in v0.2.0
type ImportInfo struct { Ident string // identifier used to refer to the import PkgName string // name of the package ID string // full import path, including the name Dir string // import path, excluding the name }
ImportInfo describes the information contained in an ImportSpec.
func ParseImportSpec ¶ added in v0.2.0
func ParseImportSpec(spec *ast.ImportSpec) (info ImportInfo, err error)
ParseImportSpec returns the name and full path of an ImportSpec.