xast: github.com/OneOfOne/xast Index | Examples | Files

package xast

import "github.com/OneOfOne/xast"



Package Files


func Walk Uses

func Walk(root ast.Node, fn WalkFunc) ast.Node

Walk calls WalkNode() with the given node and returns the result.


src := `
package main

// Foo is a foo!
type Foo struct{}

// NotFoo is not a foo!
type NotFoo struct{}

// DeleteMe needs to be deleted with this comment.
func DeleteMe() {}

// DeleteMeToo says hi.
func DeleteMeToo() {}

// GoodBoy is a good boy, yes he is!
func GoodBoy() {
	var nf NotFoo
	_ = nf

fset := token.NewFileSet()
file, err := parser.ParseFile(fset, "foo.go", src, parser.ParseComments)
if err != nil {

rewriteFn := func(n *xast.Node) *xast.Node {
    switch x := n.Node().(type) {
    case *ast.TypeSpec:
        if x.Name.Name == "Foo" {
            x.Name.Name = "Bar"
            // remove Foo's comment.
            n.Parent().Node().(*ast.GenDecl).Doc.List = nil
    case *ast.CommentGroup:
        if strings.Contains(x.Text(), "NotFoo") {
            x.List[0].Text = "// NotFoo got pwned."
        return n.Break() // won't delete the node but Walk won't go down its children list.
    case *ast.FuncDecl:
        switch x.Name.Name {
        case "DeleteMe", "DeleteMeToo":
            return n.Delete()
        case "GoodBoy":
            x.Doc.List = nil // remove the goodboy's comment :-/

    return n

var buf bytes.Buffer
printer.Fprint(&buf, fset, xast.Walk(file, rewriteFn))


package main

type Bar struct{}

// NotFoo got pwned.
type NotFoo struct{}

func GoodBoy() {
	var nf NotFoo
	_ = nf

type Node Uses

type Node struct {
    // contains filtered or unexported fields

Node holds the current ast.Node and a parent *Node.

func NewNode Uses

func NewNode(parent *Node, cur ast.Node) *Node

NewNode returns a new node with the given parent and ast.Node.

func WalkNode Uses

func WalkNode(node *Node, fn WalkFunc) *Node

WalkNode traverses an AST in depth-first order. Panics if you call node.SetNode(new-node) the returned type is not the same type as the original one.

func (*Node) Break Uses

func (n *Node) Break() *Node

Break skips the current node from farther processing.

func (*Node) Canceled Uses

func (n *Node) Canceled() bool

Canceled returns true if n is nil or Break/Delete got called.

func (*Node) Delete Uses

func (n *Node) Delete() *Node

Delete marks the node as nil and returns it, making Walk delete it from its parent.

func (*Node) Node Uses

func (n *Node) Node() ast.Node

Node returns the current ast.Node.

func (*Node) Parent Uses

func (n *Node) Parent() *Node

Parent returns Parent Node.

func (*Node) SetNode Uses

func (n *Node) SetNode(nn ast.Node) *Node

SetNode replaces the current ast.Node.

type WalkFunc Uses

type WalkFunc func(*Node) *Node

WalkFunc describes a function to be called for each node during a Walk. The returned node can be used to rewrite the AST.

Package xast imports 3 packages (graph) and is imported by 1 packages. Updated 2017-08-31. Refresh now. Tools for package owners.