Debian Codegraph @

website/cmd/generate_checks/generate_checks.go

raw
1 package main
2
3 import (
4 "bytes"
5 "encoding/json"
6 "fmt"
7 "log"
8 "regexp"
9 "sort"
10 "strings"
11
12 "honnef.co/go/tools/analysis/lint"
13 "honnef.co/go/tools/quickfix"
14 "honnef.co/go/tools/simple"
15 "honnef.co/go/tools/staticcheck"
16 "honnef.co/go/tools/stylecheck"
17 )
18
19 type Output struct {
20 Checks map[string]*lint.Documentation
21 ByCategory map[string][]string
22 }
23
24 func category(check string) string {
25 idx := strings.IndexAny(check, "0123456789")
26 return check[:idx+1]
27 }
28
29 func main() {
30 output := Output{
31 Checks: map[string]*lint.Documentation{},
32 ByCategory: map[string][]string{},
33 }
34
35 groups := [][]*lint.Analyzer{
36 staticcheck.Analyzers,
37 simple.Analyzers,
38 stylecheck.Analyzers,
39 quickfix.Analyzers,
40 }
41 for _, group := range groups {
42 for _, a := range group {
43 doc := a.Doc.Compile()
44 doc.Text = convertText(doc.Text)
45 doc.TextMarkdown = convertText(doc.TextMarkdown)
46 output.Checks[a.Analyzer.Name] = doc
47 g := output.ByCategory[category(a.Analyzer.Name)]
48 output.ByCategory[category(a.Analyzer.Name)] = append(g, a.Analyzer.Name)
49 }
50 }
51
52 for _, v := range output.ByCategory {
53 sort.Strings(v)
54 }
55
56 out, err := json.MarshalIndent(output, "", "\t")
57 if err != nil {
58 log.Fatal(err)
59 }
60 fmt.Println(string(out))
61 }
62
63 func moreCodeFollows(lines []string) bool {
64 for _, line := range lines {
65 if line == "" {
66 continue
67 }
68 if strings.HasPrefix(line, " ") {
69 return true
70 } else {
71 return false
72 }
73 }
74 return false
75 }
76
77 var alpha = regexp.MustCompile(`^[a-zA-Z ]+$`)
78
79 func convertText(text string) string {
80 var buf bytes.Buffer
81 lines := strings.Split(text, "\n")
82
83 inCode := false
84 empties := 0
85 for i, line := range lines {
86 if inCode {
87 if !moreCodeFollows(lines[i:]) {
88 if inCode {
89 fmt.Fprintln(&buf, "```")
90 inCode = false
91 }
92 }
93 }
94
95 prevEmpties := empties
96 if line == "" && !inCode {
97 empties++
98 } else {
99 empties = 0
100 }
101
102 if line == "" {
103 fmt.Fprintln(&buf)
104 continue
105 }
106
107 if strings.HasPrefix(line, " ") {
108 line = line[4:]
109 if !inCode {
110 fmt.Fprintln(&buf, "```go")
111 inCode = true
112 }
113 }
114
115 onlyAlpha := alpha.MatchString(line)
116 out := line
117 if !inCode && prevEmpties >= 2 && onlyAlpha {
118 fmt.Fprintf(&buf, "#### %s\n", out)
119 } else {
120 fmt.Fprint(&buf, out)
121 fmt.Fprintln(&buf)
122 }
123 }
124 if inCode {
125 fmt.Fprintln(&buf, "```")
126 }
127
128 return strings.TrimSpace(buf.String())
129 }