This repository has been archived by the owner on May 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
206 lines (181 loc) · 4.28 KB
/
main.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// staticcheck-codegen generates a set of packages from 'staticcheck' that can be
// consumed by rules_go's nogo static analysis framework.
//
// Check README.md for usage instructions.
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"sort"
"strings"
"text/template"
"honnef.co/go/tools/analysis/lint"
"honnef.co/go/tools/quickfix"
"honnef.co/go/tools/simple"
"honnef.co/go/tools/staticcheck"
"honnef.co/go/tools/stylecheck"
)
// Templates
const (
goTpl = `package {{ .Key }}
import (
"honnef.co/go/tools/{{ .CheckType }}"
)
var Analyzer = {{ .CheckType }}.Analyzers[{{ .Index }}].Analyzer`
)
// Some constants
const (
PrefixStyleCheck = "ST"
PrefixStaticCheck = "SA"
PrefixQuickFix = "QF"
CodeGenDir = "_gen"
)
type config struct {
// onlyFiles is a list of regular expressions that match files an analyzer
// will emit diagnostics for. When empty, the analyzer will emit diagnostics
// for all files.
OnlyFiles map[string]string `json:"only_files,omitempty"`
// excludeFiles is a list of regular expressions that match files that an
// analyzer will not emit diagnostics for.
ExcludeFiles map[string]string `json:"exclude_files,omitempty"`
}
func main() {
tpl := template.Must(template.New("source").Parse(goTpl))
err := os.RemoveAll(CodeGenDir)
if err != nil {
log.Fatalf("os.RemoveAll: %v", err)
}
err = os.Mkdir(CodeGenDir, os.ModePerm)
if err != nil {
log.Fatalf("os.Mkdir: %v", err)
}
keys := []string{}
for _, analyzers := range [][]*lint.Analyzer{
staticcheck.Analyzers,
stylecheck.Analyzers,
simple.Analyzers,
quickfix.Analyzers,
} {
sort.Slice(analyzers, func(i, j int) bool {
return analyzers[i].Analyzer.Name > analyzers[j].Analyzer.Name
})
for i, v := range analyzers {
k := v.Analyzer.Name
keys = append(keys, k)
kUpper := k
k = strings.ToLower(k)
err = os.Chdir(CodeGenDir)
if err != nil {
log.Fatalf("os.Chdir: %v", err)
}
err = os.Mkdir(k, os.ModePerm)
if err != nil {
log.Fatalf("os.Mkdir: %v", err)
}
err = os.Chdir(k)
if err != nil {
log.Fatalf("os.Chdir: %v", err)
}
// Use stylecheck template instead of
checkType := "simple"
if strings.HasPrefix(k, strings.ToLower(PrefixStaticCheck)) {
checkType = "staticcheck"
}
if strings.HasPrefix(k, strings.ToLower(PrefixStyleCheck)) {
checkType = "stylecheck"
}
if strings.HasPrefix(k, strings.ToLower(PrefixQuickFix)) {
checkType = "quickfix"
}
tplFiles := []struct {
tmplate *template.Template
fileName string
}{
{tpl, "analyzer.go"},
}
data := struct {
Key string
KeyUpper string
CheckType string
Index int
}{
Index: i,
Key: k,
KeyUpper: kUpper,
CheckType: checkType,
}
// Render the files
for _, tplFile := range tplFiles {
outFile, err := os.Create(tplFile.fileName)
if err != nil {
log.Fatalf("os.Create: %v", err)
}
if err = tplFile.tmplate.Execute(outFile, data); err != nil {
log.Fatalf("template.Execute failed: %v", err)
}
}
// Return to repo's root
os.Chdir("../../")
}
}
sort.Strings(keys)
// Analyzers from 'go tool vet help'
govetChecks := []string{
"asmdecl",
"assign",
"atomic",
"bools",
"buildtag",
// "cgocall",
"composite",
"copylock",
// "errorsas",
// "framepointer",
"httpresponse",
// "ifaceassert",
"loopclosure",
"lostcancel",
"nilfunc",
"printf",
"shift",
// "sigchanyzer",
"stdmethods",
// "stringintconv",
"structtag",
// "testinggoroutine",
"tests",
// "unmarshal",
"unreachable",
"unsafeptr",
"unusedresult",
}
temp := make(map[string]*config)
for _, v := range append(govetChecks, keys...) {
c := &config{ExcludeFiles: map[string]string{"external/": "third_party"}}
if v == "ST1000" {
c.ExcludeFiles["rules_go_work"] = "rules_go generated code"
}
if v == "composite" {
v = "composites"
}
if v == "copylock" {
v = "copylocks"
}
temp[v] = c
}
b, err := json.MarshalIndent(temp, "", " ")
if err != nil {
log.Fatalf("json marshal: %v", err)
}
err = os.Remove("nogo_config.json")
if err != nil {
log.Fatalf("rm nogo config: %v", err)
}
cf, err := os.Create("nogo_config.json")
if err != nil {
log.Fatalf("create nogo config: %v", err)
}
fmt.Fprint(cf, string(b))
}