GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - OneOfOne/xast: xast: ast rewriter with built-in clean up.

BranchesTags
Open more actions menu

Latest commit

 

History

9 Commits
9 Commits

Folders and files

NameName
Last commit message
Last commit date
.travis.yml
.travis.yml
 
 
README.md
README.md
 
 
xast.go
xast.go
 
 
xast_example_test.go
xast_example_test.go
 
 

Repository files navigation

xast GoDoc Build Status

xast provides Walk()/WalkNode() functions, similar to astrewrite.Walk from the astrewrite package. The main difference is that the passed walk function can also check a node's parent.

Also deleting a node will automatically remove any assocociated comments with it.

Note that this is similar to https://go-review.googlesource.com/c/go/+/55790 but the API itself is a lot simpler.

Example

package main

import (
	"bytes"
	"fmt"
	"go/ast"
	"go/parser"
	"go/printer"
	"go/token"

	"github.com/OneOfOne/xast"
)

func main() {
	src := `package main

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

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

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

	rewriteFn := func(n *xast.Node) *xast.Node {
		switch x := n.Node().(type) {
		case *ast.TypeSpec:
			if x.Name.Name == "Foo" {
				x.Name.Name = "Bar"
			}
		case *ast.CommentGroup:
			// check n.Text()?
			x.List = nil
			return n.Break() // don't delete the node but don't go down it's children list.

		// or if you want to remove a single comment out of a group
		case *ast.Comment: // won't ever get here since we return n.Break() from case *ast.CommentGroup.
			panic("can't get here now")
			return n.Nil() // delete this node

		}

		return n
	}

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

About

xast: ast rewriter with built-in clean up.

Resources

Readme
Activity

Stars

27 stars

Watchers

3 watching

Forks

0 forks
Report repository

Releases

Packages

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.