https://github.com/lestrrat-go/astv
An Alternate Go AST Visitor
https://github.com/lestrrat-go/astv
Last synced: 4 months ago
JSON representation
An Alternate Go AST Visitor
- Host: GitHub
- URL: https://github.com/lestrrat-go/astv
- Owner: lestrrat-go
- License: mit
- Created: 2021-01-29T02:03:57.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2021-01-29T05:07:16.000Z (over 4 years ago)
- Last Synced: 2025-01-09T03:41:41.289Z (6 months ago)
- Language: Go
- Size: 9.77 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# astv - An Alternate Go AST Visitor
Handling Go AST usually involves a huge type switch:
```
func Visit(n ast.Node) ast.Visitor {
switch n := n.(type) {
case *ast.BinaryExpr:
...
case *ast.GenDecl:
...
}
}
```The API in this module compartmentalizes this into Handlers.
The logic will stil be pretty much the same as a huge type switch, but you can organize them into small(er) method calls for better maintainability.```
func (h *myHandler) BinaryExpr(n *ast.BinaryExpr) bool {
...
return true // return true to keep recursing
}func (h *myHandler) GenDecl(n *ast.GenDecl) bool {
...
return false // return false to stop recursing
}v := astv.NewVisitor()
v.Handler(h)ast.Walk(v, node)
```# TODO
Way to remove handlers.
Way to get/set individual handlers.