blob: 6df974e91a35ad7c32055a9cce5d036ce97effc8 (
plain)
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
|
package rule
import (
"regexp"
"github.com/mgechev/revive/lint"
)
// FileHeaderRule lints given else constructs.
type FileHeaderRule struct{}
var (
multiRegexp = regexp.MustCompile("^/\\*")
singleRegexp = regexp.MustCompile("^//")
)
// Apply applies the rule to given file.
func (r *FileHeaderRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
if len(arguments) != 1 {
panic(`invalid configuration for "file-header" rule`)
}
header, ok := arguments[0].(string)
if !ok {
panic(`invalid argument for "file-header" rule: first argument should be a string`)
}
failure := []lint.Failure{
{
Node: file.AST,
Confidence: 1,
Failure: "the file doesn't have an appropriate header",
},
}
if len(file.AST.Comments) == 0 {
return failure
}
g := file.AST.Comments[0]
if g == nil {
return failure
}
comment := ""
for _, c := range g.List {
text := c.Text
if multiRegexp.Match([]byte(text)) {
text = text[2 : len(text)-2]
} else if singleRegexp.Match([]byte(text)) {
text = text[2:]
}
comment += text
}
regex, err := regexp.Compile(header)
if err != nil {
panic(err.Error())
}
if !regex.Match([]byte(comment)) {
return failure
}
return nil
}
// Name returns the rule name.
func (r *FileHeaderRule) Name() string {
return "file-header"
}
|