walker

package
v1.16.0 Latest Latest
Warning

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

Go to latest
Published: Apr 20, 2023 License: MPL-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetAncestor

func GetAncestor[T ast.Node](cursor *Cursor) T

GetAncestor returns the closest ancestor of the given type

func GetFurthestAncestor added in v1.9.3

func GetFurthestAncestor[T ast.Node](cursor *Cursor) T

GetFurthestAncestor returns the furthest ancestor of the given type

func Walk

func Walk(root ast.Node, visitor Visitor)

Walk walks the AST starting with root.

This function combines the behaviour of ast.Walk, astutil.Inspect, and astutil.Apply.

- From ast.Walk we have the ability to swap out the visitor for each subtree of nodes - From astutil.Inspect we track all the parent nodes walked through to get to the current node - From astutil.Apply we have the ability to insert, delete or replace nodes via the Cursor

Types

type Cursor

type Cursor struct {
	*astutil.Cursor
	// contains filtered or unexported fields
}

func (*Cursor) DocComment

func (c *Cursor) DocComment() string

DocComment returns the doc comment associated with the given node. It walks through the parent nodes until it finds a node with a Comment field or fields, and stops when it comes across a node which represents a block where a previous comment would no longer be valid (such as a FuncType, StructType, InterfaceType or BlockStmt).

func (*Cursor) Location

func (c *Cursor) Location() (loc locations.Location)

Location returns the location of the current node.

func (*Cursor) Parents

func (c *Cursor) Parents() []ast.Node

Parents returns the parent nodes of the current node, with the first element being the immediate parent and the the last element being the root node given to the Walk function.

type Visitor

type Visitor interface {
	Visit(cursor *Cursor) Visitor
}

Jump to

Keyboard shortcuts

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