-
Notifications
You must be signed in to change notification settings - Fork 1
/
checks.go
66 lines (55 loc) · 1.67 KB
/
checks.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
package pslint
import (
"regexp"
)
// ListCheck creates and returns a list of checks to be run.
func (l *Linter) ListChecks() []CheckFunc {
return []CheckFunc{
l.checkSpaces,
l.checkRuleLowercase,
l.checkRuleEmptyLabels,
}
}
// Spaces: checks the Line does not have irrelevant spaces.
//
// - The line should not have a leading space
// - The line should not have a trailing space
func (l *Linter) checkSpaces(line *line) (*Problem, error) {
if match := regexp.MustCompile(`^\s`).MatchString(line.source); match {
problem := l.newProblem(line, "leading space", LEVEL_WARN)
return problem, nil
}
if match := regexp.MustCompile(`\s$`).MatchString(line.source); match {
problem := l.newProblem(line, "trailing space", LEVEL_WARN)
return problem, nil
}
return nil, nil
}
// Lowercase: checks the Rule is entirely lower-case.
func (l *Linter) checkRuleLowercase(line *line) (*Problem, error) {
if !line.isRule() {
return nil, nil
}
match := regexp.MustCompile(`[A-Z]`).MatchString(line.source)
if match {
problem := l.newProblem(line, "non-lowercase suffix", LEVEL_ERROR)
return problem, nil
}
return nil, nil
}
// EmptyLabels: checks the Rule contains empty labels.
//
// An empty label is caused by two `..` (dots) with no content.
// The token `. .` will not be detected by this check, as there is already a more general check
// that checks the presence of spaces in a Rule.
func (l *Linter) checkRuleEmptyLabels(line *line) (*Problem, error) {
if !line.isRule() {
return nil, nil
}
match := regexp.MustCompile(`\.{2,}`).MatchString(line.source)
if match {
problem := l.newProblem(line, "empty label", LEVEL_ERROR)
return problem, nil
}
return nil, nil
}