5
5
"fmt"
6
6
"go/format"
7
7
"os"
8
+ "sort"
8
9
"strings"
9
10
10
11
"github.com/PuerkitoBio/goquery"
@@ -28,7 +29,7 @@ func main() {
28
29
buildFile (rs )
29
30
}
30
31
31
- func getRules () (map [string ]map [ string ] bool , error ) {
32
+ func getRules () (map [string ][] string , error ) {
32
33
resp , err := grequests .Get (rulesURl , nil )
33
34
if err != nil {
34
35
return nil , err
@@ -39,7 +40,7 @@ func getRules() (map[string]map[string]bool, error) {
39
40
return nil , err
40
41
}
41
42
42
- rules := map [string ]map [ string ] bool {
43
+ rules := map [string ][] string {
43
44
problems : {},
44
45
suggestions : {},
45
46
lf : {},
@@ -62,33 +63,45 @@ func getRules() (map[string]map[string]bool, error) {
62
63
}
63
64
c , ok := s .Attr ("class" )
64
65
if ok && c == "rule__name" {
65
- rules [currentRule ][s .Nodes [0 ].LastChild .Data ] = true
66
+ r := s .Nodes [0 ].LastChild .Data
67
+ rules [currentRule ] = append (rules [currentRule ], r )
66
68
}
67
69
}
68
70
})
69
71
return rules , nil
70
72
}
71
73
72
- func buildFile (ruleMap map [string ]map [ string ] bool ) {
74
+ func buildFile (ruleMap map [string ][] string ) {
73
75
fileName := "generated_rules.go"
74
76
77
+ sortedRuleTypes := []string {lf , problems , suggestions }
78
+
75
79
b := strings.Builder {}
76
80
b .WriteString ("// Code generated by generator/genrules. DO NOT EDIT.\n " )
77
81
b .WriteString ("package plugin\n " )
78
82
b .WriteString ("import \" github.com/cocov-ci/go-plugin-kit/cocov\" \n " )
79
83
b .WriteString ("var rules = map[string]cocov.IssueKind{\n " )
80
- for ruleType , rules := range ruleMap {
81
- for ruleName := range rules {
82
- issueKind := ""
83
- switch ruleType {
84
- case suggestions :
85
- issueKind = "cocov.IssueKindConvention"
86
- case problems :
87
- issueKind = "cocov.IssueKindBug"
88
- case lf :
89
- issueKind = "cocov.IssueKindStyle"
84
+
85
+ ruleExists := map [string ]bool {}
86
+
87
+ for _ , rType := range sortedRuleTypes {
88
+ issueKind := ""
89
+ switch rType {
90
+ case suggestions :
91
+ issueKind = "cocov.IssueKindConvention"
92
+ case problems :
93
+ issueKind = "cocov.IssueKindBug"
94
+ case lf :
95
+ issueKind = "cocov.IssueKindStyle"
96
+ }
97
+
98
+ rules := ruleMap [rType ]
99
+ sort .Strings (rules )
100
+ for _ , rule := range rules {
101
+ if ok := ruleExists [rule ]; ok {
102
+ continue
90
103
}
91
- b .WriteString (fmt .Sprintf ("\" %s\" : %s,\n " , ruleName , issueKind ))
104
+ b .WriteString (fmt .Sprintf ("\" %s\" : %s,\n " , rule , issueKind ))
92
105
}
93
106
}
94
107
0 commit comments