-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathast_gen.go
72 lines (64 loc) · 1.64 KB
/
ast_gen.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"os"
"path"
"path/filepath"
"golang.org/x/tools/go/packages"
)
func MustGenExpr(expr string) (*token.FileSet, ast.Expr) {
fset, stmt := MustGenStatements(expr)
return fset, stmt[0].(*ast.ExprStmt).X
}
func MustGenStatements(statements string) (*token.FileSet, []ast.Stmt) {
fset, funcAst := MustGenFunc(fmt.Sprintf("func main() {\n%v\n}", statements))
return fset, funcAst.Body.List
}
func MustGenFunc(src string) (*token.FileSet, *ast.FuncDecl) {
fset, fileAst := MustGenSrc(fmt.Sprintf("package main\n%v", src))
return fset, fileAst.Decls[0].(*ast.FuncDecl)
}
func MustGenSrc(src string) (*token.FileSet, *ast.File) {
fset := token.NewFileSet()
fileAst, err := parser.ParseFile(fset, "", src, parser.AllErrors)
if err != nil {
panic(fmt.Errorf("src parsing failed: %w", err))
}
return fset, fileAst
}
func MustGenMod(src string) (string, func(), error) {
dir, err := os.MkdirTemp(".", "test-*")
if err != nil {
return "", nil, err
}
dir, err = filepath.Abs(dir)
if err != nil {
return "", nil, err
}
f, err := os.Create(path.Join(dir, "main.go"))
if err != nil {
return "", nil, err
}
_, _ = f.WriteString(src)
return dir, func() { _ = os.RemoveAll(dir) }, nil
}
func MustExtractFunc(pkgs []*packages.Package) *ast.FuncDecl {
var result *ast.FuncDecl
for _, pkg := range pkgs {
for _, f := range pkg.Syntax {
ast.Inspect(f, func(node ast.Node) bool {
if funcDecl, ok := node.(*ast.FuncDecl); ok && result == nil {
result = funcDecl
}
return true
})
}
}
if result == nil {
panic(fmt.Errorf("unable to find function"))
}
return result
}