-
Notifications
You must be signed in to change notification settings - Fork 25
/
XS002.go
53 lines (42 loc) · 1.46 KB
/
XS002.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 XS002
import (
"go/ast"
"sort"
"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/schemamapcompositelit"
"golang.org/x/tools/go/analysis"
)
const Doc = `check for Schema that attribute names are in alphabetical order
The XS002 analyzer reports cases of schemas where attribute names
are not in alphabetical order.`
const analyzerName = "XS002"
var Analyzer = &analysis.Analyzer{
Name: analyzerName,
Doc: Doc,
Requires: []*analysis.Analyzer{
schemamapcompositelit.Analyzer,
commentignore.Analyzer,
},
Run: run,
}
func run(pass *analysis.Pass) (interface{}, error) {
ignorer := pass.ResultOf[commentignore.Analyzer].(*commentignore.Ignorer)
schemamapcompositelits := pass.ResultOf[schemamapcompositelit.Analyzer].([]*ast.CompositeLit)
for _, smap := range schemamapcompositelits {
if ignorer.ShouldIgnore(analyzerName, smap) {
continue
}
schemaKeys := make([]string, 0 , len(schema.GetSchemaMapAttributeNames(smap)))
for _, attributeName := range schema.GetSchemaMapAttributeNames(smap) {
if v := astutils.ExprStringValue(attributeName); v != nil {
schemaKeys = append(schemaKeys, *v)
}
}
if !sort.StringsAreSorted(schemaKeys) {
pass.Reportf(smap.Pos(), "%s: schema attributes should be in alphabetical order", analyzerName)
}
}
return nil, nil
}