mukan-ignite/ignite/pkg/xast/xast.go
Mukan Erkin Törük 26b204bd04
Some checks are pending
Docs Deploy / build_and_deploy (push) Waiting to run
Generate Docs / cli (push) Waiting to run
Generate Config Doc / cli (push) Waiting to run
Go formatting / go-formatting (push) Waiting to run
Check links / markdown-link-check (push) Waiting to run
Integration / pre-test (push) Waiting to run
Integration / test on (push) Blocked by required conditions
Integration / status (push) Blocked by required conditions
Lint / Lint Go code (push) Waiting to run
Test / test (ubuntu-latest) (push) Waiting to run
feat: fork Ignite CLI v29 as Mukan Ignite — remove cosmos-sdk restrictions
2026-05-11 03:31:37 +03:00

55 lines
1.5 KiB
Go

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 = 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
}