-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinnertypealias.go
87 lines (75 loc) · 2.04 KB
/
innertypealias.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
package innertypealias
import (
"fmt"
"go/ast"
"go/token"
"go/types"
"golang.org/x/tools/go/analysis"
"golang.org/x/tools/go/analysis/passes/inspect"
"golang.org/x/tools/go/ast/inspector"
)
const doc = "innertypealias find a type which is an alias for exported same package's type"
var Analyzer = &analysis.Analyzer{
Name: "innertypealias",
Doc: doc,
Run: run,
Requires: []*analysis.Analyzer{
inspect.Analyzer,
},
}
func run(pass *analysis.Pass) (interface{}, error) {
inspect := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
nodes := []ast.Node{(*ast.StructType)(nil)}
embeddeds := make(map[string]bool)
inspect.Preorder(nodes, func(n ast.Node) {
st, _ := n.(*ast.StructType)
if st == nil {
return
}
for _, f := range st.Fields.List {
id, _ := f.Type.(*ast.Ident)
if id == nil || f.Names != nil {
continue
}
obj := pass.TypesInfo.ObjectOf(id)
if obj.Pkg() == pass.Pkg {
embeddeds[id.Name] = true
}
}
})
for _, f := range pass.Files {
for _, decl := range f.Decls {
decl, _ := decl.(*ast.GenDecl)
if decl == nil || decl.Tok != token.TYPE {
continue
}
for _, spec := range decl.Specs {
spec, _ := spec.(*ast.TypeSpec)
if spec == nil || spec.Assign == token.NoPos || !spec.Name.IsExported() {
continue
}
typ, _ := pass.TypesInfo.TypeOf(spec.Type).(*types.Named)
if typ == nil || typ.Obj().Pkg() != pass.Pkg || !typ.Obj().Exported() || embeddeds[spec.Name.Name] {
continue
}
// type X = Y => type X Y
x, y := spec.Name.Name, typ.Obj().Name()
fix := analysis.SuggestedFix{
Message: "fix type alias to defined type",
TextEdits: []analysis.TextEdit{{
Pos: spec.Pos(),
End: spec.End(),
NewText: []byte(x + " " + y),
}},
}
pass.Report(analysis.Diagnostic{
Pos: spec.Pos(),
End: spec.End(),
Message: fmt.Sprintf("%s is a alias for %s but it is exported type", x, y),
SuggestedFixes: []analysis.SuggestedFix{fix},
})
}
}
}
return nil, nil
}