-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathS024.go
67 lines (52 loc) · 1.91 KB
/
S024.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
package S024
import (
"go/ast"
"github.com/bflad/tfproviderlint/helper/terraformtype/helper/schema"
"github.com/bflad/tfproviderlint/passes/commentignore"
"github.com/bflad/tfproviderlint/passes/helper/schema/resourceinfodatasourceonly"
"golang.org/x/tools/go/analysis"
)
const Doc = `check for Schema that should omit ForceNew in data source schema attributes
The S024 analyzer reports usage of ForceNew in data source schema attributes,
which is unnecessary.`
const analyzerName = "S024"
var Analyzer = &analysis.Analyzer{
Name: analyzerName,
Doc: Doc,
Requires: []*analysis.Analyzer{
commentignore.Analyzer,
resourceinfodatasourceonly.Analyzer,
},
Run: run,
}
func run(pass *analysis.Pass) (interface{}, error) {
ignorer := pass.ResultOf[commentignore.Analyzer].(*commentignore.Ignorer)
resourceInfos := pass.ResultOf[resourceinfodatasourceonly.Analyzer].([]*schema.ResourceInfo)
for _, resourceInfo := range resourceInfos {
if ignorer.ShouldIgnore(analyzerName, resourceInfo.AstCompositeLit) {
continue
}
var schemaInfos []*schema.SchemaInfo
ast.Inspect(resourceInfo.AstCompositeLit, func(n ast.Node) bool {
compositeLit, ok := n.(*ast.CompositeLit)
if !ok {
return true
}
if schema.IsMapStringSchema(compositeLit, pass.TypesInfo) {
for _, mapSchema := range schema.GetSchemaMapSchemas(compositeLit) {
schemaInfos = append(schemaInfos, schema.NewSchemaInfo(mapSchema, pass.TypesInfo))
}
} else if schema.IsTypeSchema(pass.TypesInfo.TypeOf(compositeLit.Type)) {
schemaInfos = append(schemaInfos, schema.NewSchemaInfo(compositeLit, pass.TypesInfo))
}
return true
})
for _, schemaInfo := range schemaInfos {
if !schemaInfo.DeclaresField(schema.SchemaFieldForceNew) {
continue
}
pass.Reportf(schemaInfo.Fields[schema.SchemaFieldForceNew].Pos(), "%s: ForceNew is extraneous in data source schema attributes", analyzerName)
}
}
return nil, nil
}