-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinitcheck.go
121 lines (93 loc) · 2.25 KB
/
initcheck.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package initcheck
import (
"bytes"
"fmt"
"go/ast"
"go/printer"
"go/token"
"golang.org/x/tools/go/packages"
)
//go:generate go run gen.go
func isInitFuncDeclaration(funcDecl *ast.FuncDecl) bool {
return funcDecl.Name.Name == "init" && funcDecl.Recv.NumFields() == 0
}
func checkInitInPackage(pkg *packages.Package) {
for _, fileAst := range pkg.Syntax {
ast.Inspect(fileAst, func(n ast.Node) bool {
funcDecl, ok := n.(*ast.FuncDecl)
if !ok {
return true
}
if isInitFuncDeclaration(funcDecl) {
render(pkg.Fset, funcDecl)
}
return true
})
}
}
// render returns the pretty-print of the given node
func render(fset *token.FileSet, funcDecl *ast.FuncDecl) {
name := fset.File(funcDecl.Pos()).Name()
line := fset.Position(funcDecl.Pos()).Line
var buf bytes.Buffer
if err := printer.Fprint(&buf, fset, funcDecl); err != nil {
panic(err)
}
fmt.Printf("%s:%d\n%s\n\n", name, line, buf.String())
}
// allImports returns all imports without duplication
func allImports(pkgs []*packages.Package) []*packages.Package {
var all []*packages.Package
var visit func(pkg *packages.Package)
seen := make(map[*packages.Package]bool)
visit = func(pkg *packages.Package) {
if seen[pkg] {
return
}
seen[pkg] = true
var importsPaths []string
for path := range pkg.Imports {
importsPaths = append(importsPaths, path)
}
for _, path := range importsPaths {
visit(pkg.Imports[path])
}
all = append(all, pkg)
}
for _, pkg := range pkgs {
visit(pkg)
}
return all
}
func loadPackages(patterns []string) ([]*packages.Package, error) {
config := &packages.Config{
Mode: packages.NeedName | packages.NeedImports | packages.NeedDeps |
packages.NeedModule | packages.NeedFiles | packages.NeedSyntax |
packages.NeedTypes,
}
pkgs, err := packages.Load(config, patterns...)
if err != nil {
return nil, err
}
for _, p := range pkgs {
for _, err := range p.Errors {
return nil, err
}
}
return pkgs, nil
}
// Run loads and check if imported packages contain an init function
func Run(patterns []string) error {
pkgs, err := loadPackages(patterns)
if err != nil {
return err
}
all := allImports(pkgs)
for _, pkg := range all {
if isStdlib(pkg) {
continue
}
checkInitInPackage(pkg)
}
return nil
}