/
anycheck.go
53 lines (47 loc) · 1.06 KB
/
anycheck.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
package anycheck
import (
"go/ast"
)
type Linter struct {
cfg config
}
func NewLinter(options ...Option) (*Linter, error) {
cfg := config{}
for _, option := range options {
if err := option.apply(&cfg); err != nil {
return nil, err
}
}
return &Linter{cfg}, nil
}
func (l *Linter) Run(nodes []ast.Node) []Issue {
issues := []Issue{}
for _, node := range nodes {
if newIssue := l.checkForAny(node); newIssue != nil {
issues = append(issues, newIssue)
}
if newIssue := l.checkForInterface(node); newIssue != nil {
issues = append(issues, newIssue)
}
}
return issues
}
func (l *Linter) checkForAny(node ast.Node) Issue {
switch n := node.(type) {
case *ast.Ident:
if n.Name == "any" && !l.cfg.allowAny {
return anyNotAllowed{pos: node.Pos()}
}
}
return nil
}
func (l *Linter) checkForInterface(node ast.Node) Issue {
switch n := node.(type) {
case *ast.InterfaceType:
// This is an empty interface with no methods.
if len(n.Methods.List) == 0 && !l.cfg.allowInterface {
return interfaceNotAllowed{pos: node.Pos()}
}
}
return nil
}