-
Notifications
You must be signed in to change notification settings - Fork 532
/
xast.go
55 lines (49 loc) · 1.45 KB
/
xast.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package xast
import (
"go/ast"
"go/parser"
"go/token"
"strings"
"github.com/ignite/cli/v29/ignite/pkg/errors"
)
var ErrStop = errors.New("ast stop")
// Inspect is like ast.Inspect but with error handling.
// Unlike ast.Inspect the function parameter f returns an error and not a bool.
// The returned error is propagated to the caller, unless it is equal to
// ErrStop, which in that case indicates the child nodes shouldn't not be
// inspected (like returning false in the function of ast.Inspect).
func Inspect(n ast.Node, f func(n ast.Node) error) (err error) {
ast.Inspect(n, func(n ast.Node) bool {
err = f(n)
if err == nil {
return true
}
if errors.Is(err, ErrStop) {
err = nil
}
return false
})
return
}
// ParseDir invokes ast.ParseDir and returns the first package found that is
// doesn't has the "_test" suffix.
func ParseDir(dir string) (*ast.Package, *token.FileSet, error) {
fileSet := token.NewFileSet()
pkgs, err := parser.ParseDir(fileSet, dir, nil, 0)
if err != nil {
return nil, nil, err
}
for name, pkg := range pkgs {
if strings.HasSuffix(name, "_test") {
continue
}
return pkg, fileSet, nil
}
return nil, nil, errors.Errorf("no valid package found in %s", dir)
}
// ParseFile invokes ast.ParseFile and returns the *ast.File.
func ParseFile(filepath string) (*ast.File, *token.FileSet, error) {
fileSet := token.NewFileSet()
file, err := parser.ParseFile(fileSet, filepath, nil, 0)
return file, fileSet, err
}