forked from bflad/tfproviderlint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathS028.go
50 lines (40 loc) · 1.43 KB
/
S028.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
package S028
import (
"go/ast"
"github.com/bflad/tfproviderlint/helper/terraformtype/helper/schema"
"github.com/bflad/tfproviderlint/passes/commentignore"
"github.com/bflad/tfproviderlint/passes/helper/schema/schemainfocomputedonly"
"golang.org/x/tools/go/analysis"
)
const Doc = `check for Schema with only Computed enabled and DefaultFunc configured
The S028 analyzer reports cases of schemas which only enables Computed
and configures DefaultFunc, which is not valid.`
const analyzerName = "S028"
var Analyzer = &analysis.Analyzer{
Name: analyzerName,
Doc: Doc,
Requires: []*analysis.Analyzer{
commentignore.Analyzer,
schemainfocomputedonly.Analyzer,
},
Run: run,
}
func run(pass *analysis.Pass) (interface{}, error) {
ignorer := pass.ResultOf[commentignore.Analyzer].(*commentignore.Ignorer)
schemaInfos := pass.ResultOf[schemainfocomputedonly.Analyzer].([]*schema.SchemaInfo)
for _, schemaInfo := range schemaInfos {
if ignorer.ShouldIgnore(analyzerName, schemaInfo.AstCompositeLit) {
continue
}
if schemaInfo.Schema.DefaultFunc == nil {
continue
}
switch t := schemaInfo.AstCompositeLit.Type.(type) {
default:
pass.Reportf(schemaInfo.AstCompositeLit.Lbrace, "%s: schema should not only enable Computed and configure DefaultFunc", analyzerName)
case *ast.SelectorExpr:
pass.Reportf(t.Sel.Pos(), "%s: schema should not only enable Computed and configure DefaultFunc", analyzerName)
}
}
return nil, nil
}