-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecl.go
138 lines (121 loc) · 2.6 KB
/
decl.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package mingo
import (
"go/ast"
"github.com/bobg/errors"
)
func (p *pkgScanner) decl(decl ast.Decl) error {
switch decl := decl.(type) {
case *ast.FuncDecl:
return p.funcDecl(decl)
case *ast.GenDecl:
return p.genDecl(decl)
}
return nil
}
func (p *pkgScanner) funcDecl(decl *ast.FuncDecl) error {
if err := p.fieldList(decl.Recv); err != nil {
return errors.Wrapf(err, "scanning receiver for func %s", decl.Name.Name)
}
if p.isMax() {
return nil
}
// Generics are supported in Go 1.18 and later.
if decl.Type.TypeParams != nil && len(decl.Type.TypeParams.List) > 0 {
declResult := posResult{
version: 18,
pos: p.fset.Position(decl.Pos()),
desc: "generic func decl",
}
if p.greater(declResult) {
return nil
}
}
if err := p.fieldList(decl.Type.Params); err != nil {
return errors.Wrapf(err, "scanning params for func %s", decl.Name.Name)
}
if p.isMax() {
return nil
}
if err := p.fieldList(decl.Type.Results); err != nil {
return errors.Wrapf(err, "scanning results for func %s", decl.Name.Name)
}
if p.isMax() {
return nil
}
return p.funcBody(decl.Body)
}
func (p *pkgScanner) fieldList(list *ast.FieldList) error {
if list == nil {
return nil
}
for _, field := range list.List {
if err := p.field(field); err != nil {
return err
}
if p.isMax() {
return nil
}
}
return nil
}
func (p *pkgScanner) field(field *ast.Field) error {
return p.expr(field.Type)
}
func (p *pkgScanner) genDecl(decl *ast.GenDecl) error {
for _, spec := range decl.Specs {
if err := p.spec(spec); err != nil {
return err
}
if p.isMax() {
return nil
}
}
return nil
}
func (p *pkgScanner) spec(spec ast.Spec) error {
switch spec := spec.(type) {
case *ast.ValueSpec:
return p.valueSpec(spec)
case *ast.TypeSpec:
return p.typeSpec(spec)
}
return nil
}
func (p *pkgScanner) valueSpec(spec *ast.ValueSpec) error {
if err := p.expr(spec.Type); err != nil {
return err
}
if p.isMax() {
return nil
}
for _, value := range spec.Values {
if err := p.expr(value); err != nil {
return err
}
if p.isMax() {
return nil
}
}
return nil
}
func (p *pkgScanner) typeSpec(spec *ast.TypeSpec) error {
if spec.Assign.IsValid() {
p.greater(posResult{
version: 9,
pos: p.fset.Position(spec.Pos()),
desc: "type alias",
})
}
// Generics are supported in Go 1.18 and later.
if spec.TypeParams != nil && len(spec.TypeParams.List) > 0 {
declResult := posResult{
version: 18,
pos: p.fset.Position(spec.Pos()),
desc: "generic type decl",
}
if p.greater(declResult) {
return nil
}
}
return p.expr(spec.Type)
}