mukan-ignite/ignite/pkg/xast/xast.go
Mukan Erkin Törük c32551b6f7
Some checks failed
Docs Deploy / build_and_deploy (push) Has been cancelled
Generate Docs / cli (push) Has been cancelled
Generate Config Doc / cli (push) Has been cancelled
Go formatting / go-formatting (push) Has been cancelled
Check links / markdown-link-check (push) Has been cancelled
Integration / pre-test (push) Has been cancelled
Integration / test on (push) Has been cancelled
Integration / status (push) Has been cancelled
Lint / Lint Go code (push) Has been cancelled
Test / test (ubuntu-latest) (push) Has been cancelled
refactor: replace all github.com upstream refs with git.cw.tr/mukan-network
2026-05-11 03:36:24 +03:00

55 lines
1.5 KiB
Go

package xast
import (
"go/ast"
"go/parser"
"go/token"
"strings"
"git.cw.tr/mukan-network/mukan-ignite/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 = errors.Join(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) { //nolint:staticcheck,nolintlint
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
}