-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
rules.go
206 lines (174 loc) · 4.8 KB
/
rules.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
package main
import (
"bytes"
"strings"
"github.com/yuin/goldmark/ast"
)
// The first section is a heading with plugin name and paragraph short
// description
func firstSection(t *T, root ast.Node) error {
var n ast.Node
n = root.FirstChild()
t.assertKind(ast.KindHeading, n)
t.assertHeadingLevel(1, n)
t.assertFirstChildRegexp(` Plugin$`, n)
// Make sure there is some text after the heading
n = n.NextSibling()
t.assertKind(ast.KindParagraph, n)
length := len(n.Text(t.markdown))
min := 30
if length < min {
t.assertNodef(n, "short first section. Please add short description of plugin. length %d, minimum %d", length, min)
}
return nil
}
// Somewhere there should be a heading "sample configuration" and a
// toml code block. The toml should match what is in the plugin's go
// code
// Second level headings should include
func requiredSections(t *T, root ast.Node, headings []string) error {
headingsSet := newSet(headings)
expectedLevel := 2
titleCounts := make(map[string]int)
for n := root.FirstChild(); n != nil; n = n.NextSibling() {
var h *ast.Heading
var ok bool
if h, ok = n.(*ast.Heading); !ok {
continue
}
child := h.FirstChild()
if child == nil {
continue
}
title := strings.TrimSpace(string(child.Text(t.markdown)))
if headingsSet.has(title) && h.Level != expectedLevel {
t.assertNodef(n, "has required section %q but wrong heading level. Expected level %d, found %d",
title, expectedLevel, h.Level)
}
titleCounts[title]++
}
headingsSet.forEach(func(title string) {
if _, exists := titleCounts[title]; !exists {
t.assertf("missing required section %q", title)
}
})
return nil
}
// Use this to make a rule that looks for a list of settings. (this is
// a closure of func requiredSection)
func requiredSectionsClose(headings []string) func(*T, ast.Node) error {
return func(t *T, root ast.Node) error {
return requiredSections(t, root, headings)
}
}
func noLongLinesInParagraphs(threshold int) func(*T, ast.Node) error {
return func(t *T, root ast.Node) error {
// We're looking for long lines in paragraphs. Find paragraphs
// first, then which lines are in paragraphs
paraLines := []int{}
for n := root.FirstChild(); n != nil; n = n.NextSibling() {
var p *ast.Paragraph
var ok bool
if p, ok = n.(*ast.Paragraph); !ok {
continue //only looking for paragraphs
}
segs := p.Lines()
for _, seg := range segs.Sliced(0, segs.Len()) {
line := t.line(seg.Start)
paraLines = append(paraLines, line)
}
}
// Find long lines in the whole file
longLines := []int{}
last := 0
for i, cur := range t.newlineOffsets {
length := cur - last - 1 // -1 to exclude the newline
if length > threshold {
longLines = append(longLines, i)
}
last = cur
}
// Merge both lists
p := 0
l := 0
bads := []int{}
for p < len(paraLines) && l < len(longLines) {
long := longLines[l]
para := paraLines[p]
switch {
case long == para:
bads = append(bads, long)
p++
l++
case long < para:
l++
case long > para:
p++
}
}
for _, bad := range bads {
t.assertLinef(bad, "long line in paragraph")
}
return nil
}
}
func configSection(t *T, root ast.Node) error {
var config *ast.Heading
config = nil
expectedTitle := "Configuration"
for n := root.FirstChild(); n != nil; n = n.NextSibling() {
var h *ast.Heading
var ok bool
if h, ok = n.(*ast.Heading); !ok {
continue
}
title := string(h.FirstChild().Text(t.markdown))
if title == expectedTitle {
config = h
continue
}
}
if config == nil {
t.assertf("missing required section %q", expectedTitle)
return nil
}
toml := config.NextSibling()
if toml == nil {
t.assertNodef(toml, "missing config next sibling")
return nil
}
var b *ast.FencedCodeBlock
var ok bool
if b, ok = toml.(*ast.FencedCodeBlock); !ok {
t.assertNodef(toml, "config next sibling isn't a fenced code block")
return nil
}
if !bytes.Equal(b.Language(t.markdown), []byte("toml")) {
t.assertNodef(b, "config fenced code block isn't toml language")
return nil
}
return nil
}
// Links from one markdown file to another in the repo should be relative
func relativeTelegrafLinks(t *T, root ast.Node) error {
for n := root.FirstChild(); n != nil; n = n.NextSibling() {
if _, ok := n.(*ast.Paragraph); !ok {
continue
}
for n2 := n.FirstChild(); n2 != nil; n2 = n2.NextSibling() {
var l *ast.Link
var ok bool
if l, ok = n2.(*ast.Link); !ok {
continue
}
link := string(l.Destination)
if strings.HasPrefix(link, "https://github.com/influxdata/telegraf/blob") {
t.assertNodef(n, "in-repo link must be relative: %s", link)
}
}
}
return nil
}
// To do: Check markdown files that aren't plugin readme files for paragraphs
// with long lines
// To do: Check the toml inside the configuration section for syntax errors