astutil

package
v0.3.0-beta.1 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2021 License: Apache-2.0 Imports: 12 Imported by: 24

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Apply

func Apply(node ast.Node, before, after func(Cursor) bool) ast.Node

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

func ApplyRecursively(n ast.Node) ast.Node

ApplyRecursively indicates that a node inserted with InsertBefore, or InsertAfter should be processed recursively.

func CopyComments

func CopyComments(to, from ast.Node)

CopyComments associates comments of one node with another. It may change the relative position of comments.

func CopyMeta

func CopyMeta(to, from ast.Node) ast.Node

CopyMeta copies comments and position information from one node to another. It returns the destination node.

func CopyPosition

func CopyPosition(to, from ast.Node)

CopyPosition sets the position of one node to another.

func Resolve

func Resolve(f *ast.File, errFn ErrFunc)

Resolve resolves all identifiers in a file. Unresolved identifiers are recorded in Unresolved. It will not overwrite already resolved values.

func ResolveExpr

func ResolveExpr(e ast.Expr, errFn ErrFunc)

Resolve resolves all identifiers in an expression. It will not overwrite already resolved values.

func Sanitize added in v0.2.0

func Sanitize(f *ast.File) error

Sanitize rewrites File f in place to be well formed after automated construction of an AST.

Rewrites:

  • auto inserts imports associated with Idents
  • unshadows imports associated with idents
  • unshadows references for identifiers that were already resolved.

func ToFile added in v0.2.1

func ToFile(x ast.Expr) (*ast.File, error)

ToFile converts an expression to a File. It will create an import section for any of the identifiers in x that refer to an import and will unshadow references as appropriate.

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 ErrFunc

type ErrFunc func(pos token.Pos, msg string, args ...interface{})

An ErrFunc processes errors.

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.

Jump to

Keyboard shortcuts

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