-
Notifications
You must be signed in to change notification settings - Fork 25
/
R014.go
64 lines (52 loc) · 2.51 KB
/
R014.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
package R014
import (
"github.com/bflad/tfproviderlint/helper/astutils"
"github.com/bflad/tfproviderlint/helper/terraformtype/helper/schema"
"github.com/bflad/tfproviderlint/passes/commentignore"
"github.com/bflad/tfproviderlint/passes/helper/schema/crudfuncinfo"
"golang.org/x/tools/go/analysis"
)
const Doc = `check for CreateFunc, CreateContextFunc, DeleteFunc, DeleteContextFunc, ReadFunc, ReadContextFunc, UpdateFunc, and UpdateContextFunc parameter naming
The R014 analyzer reports when CreateFunc, CreateContextFunc, DeleteFunc,
DeleteContextFunc, ReadFunc, ReadContextFunc, UpdateFunc, and UpdateContextFunc
declarations do not use d as the name for the *schema.ResourceData parameter
or meta as the name for the interface{} parameter. This parameter naming is the
standard convention for resources.`
const analyzerName = "R014"
var Analyzer = &analysis.Analyzer{
Name: analyzerName,
Doc: Doc,
Requires: []*analysis.Analyzer{
commentignore.Analyzer,
crudfuncinfo.Analyzer,
},
Run: run,
}
func run(pass *analysis.Pass) (interface{}, error) {
ignorer := pass.ResultOf[commentignore.Analyzer].(*commentignore.Ignorer)
crudFuncs := pass.ResultOf[crudfuncinfo.Analyzer].([]*schema.CRUDFuncInfo)
for _, crudFunc := range crudFuncs {
if ignorer.ShouldIgnore(analyzerName, crudFunc.Node) {
continue
}
params := crudFunc.Type.Params
paramCount := len(params.List)
switch paramCount {
case 2:
if name := astutils.FieldListName(params, 0, 0); name != nil && *name != "_" && *name != "d" {
pass.Reportf(params.List[0].Pos(), "%s: *schema.ResourceData parameter of CreateFunc, ReadFunc, UpdateFunc, or DeleteFunc should be named d", analyzerName)
}
if name := astutils.FieldListName(params, 1, 0); name != nil && *name != "_" && *name != "meta" {
pass.Reportf(params.List[1].Pos(), "%s: interface{} parameter of CreateFunc, ReadFunc, UpdateFunc, or DeleteFunc should be named meta", analyzerName)
}
case 3:
if name := astutils.FieldListName(params, 1, 0); name != nil && *name != "_" && *name != "d" {
pass.Reportf(params.List[1].Pos(), "%s: *schema.ResourceData parameter of CreateContextFunc, ReadContextFunc, UpdateContextFunc, or DeleteContextFunc should be named d", analyzerName)
}
if name := astutils.FieldListName(params, 2, 0); name != nil && *name != "_" && *name != "meta" {
pass.Reportf(params.List[2].Pos(), "%s: interface{} parameter of CreateContextFunc, ReadContextFunc, UpdateContextFunc, or DeleteContextFunc should be named meta", analyzerName)
}
}
}
return nil, nil
}