Skip to content

Commit 954250c

Browse files
committed
feat(generator): Sort rules by name to avoid unnecessary diffs
1 parent 70f78de commit 954250c

File tree

2 files changed

+264
-251
lines changed

2 files changed

+264
-251
lines changed

generator/genrules.go

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"go/format"
77
"os"
8+
"sort"
89
"strings"
910

1011
"github.com/PuerkitoBio/goquery"
@@ -28,7 +29,7 @@ func main() {
2829
buildFile(rs)
2930
}
3031

31-
func getRules() (map[string]map[string]bool, error) {
32+
func getRules() (map[string][]string, error) {
3233
resp, err := grequests.Get(rulesURl, nil)
3334
if err != nil {
3435
return nil, err
@@ -39,7 +40,7 @@ func getRules() (map[string]map[string]bool, error) {
3940
return nil, err
4041
}
4142

42-
rules := map[string]map[string]bool{
43+
rules := map[string][]string{
4344
problems: {},
4445
suggestions: {},
4546
lf: {},
@@ -62,33 +63,45 @@ func getRules() (map[string]map[string]bool, error) {
6263
}
6364
c, ok := s.Attr("class")
6465
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)
6668
}
6769
}
6870
})
6971
return rules, nil
7072
}
7173

72-
func buildFile(ruleMap map[string]map[string]bool) {
74+
func buildFile(ruleMap map[string][]string) {
7375
fileName := "generated_rules.go"
7476

77+
sortedRuleTypes := []string{lf, problems, suggestions}
78+
7579
b := strings.Builder{}
7680
b.WriteString("// Code generated by generator/genrules. DO NOT EDIT.\n")
7781
b.WriteString("package plugin\n")
7882
b.WriteString("import \"github.com/cocov-ci/go-plugin-kit/cocov\"\n")
7983
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
90103
}
91-
b.WriteString(fmt.Sprintf("\"%s\": %s,\n", ruleName, issueKind))
104+
b.WriteString(fmt.Sprintf("\"%s\": %s,\n", rule, issueKind))
92105
}
93106
}
94107

0 commit comments

Comments
 (0)