-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparam.go
63 lines (49 loc) · 1.33 KB
/
param.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
package varnamelen
import (
"go/ast"
"golang.org/x/tools/go/analysis"
)
// parameter represents a declared function or method parameter.
type parameter struct {
// name is the name of the parameter.
name string
// typ is the type of the parameter.
typ string
// field is the declaration of the parameter.
field *ast.Field
}
// checkParams applies the analysis to parameters in paramToDist, according to cfg.
func checkParams(pass *analysis.Pass, paramToDist map[parameter]int, cfg configuration) {
for param, dist := range paramToDist {
if param.isConventional() {
continue
}
if cfg.ignoreNames.contains(param.name) {
continue
}
if cfg.ignoreDecls.matchParameter(param) {
continue
}
if checkNameAndDistance(param.name, dist, cfg) {
continue
}
pass.Reportf(param.field.Pos(), "parameter name '%s' is too short for the scope of its usage", param.name)
}
}
// match returns whether p matches decl.
func (p parameter) match(decl identDeclaration) bool {
if p.name != decl.name {
return false
}
return decl.matchType(p.typ)
}
// isConventional returns true if p matches a conventional Go parameter name and type,
// such as "ctx context.Context" or "t *testing.T".
func (p parameter) isConventional() bool {
for _, decl := range conventionalDecls {
if p.match(decl) {
return true
}
}
return false
}