-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrule.go
More file actions
81 lines (70 loc) · 1.76 KB
/
Copy pathrule.go
File metadata and controls
81 lines (70 loc) · 1.76 KB
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
package maxfilelength
import (
"fmt"
"github.com/jeduden/mdsmith/internal/lint"
"github.com/jeduden/mdsmith/internal/rule"
"github.com/jeduden/mdsmith/internal/rules/settings"
)
func init() {
rule.Register(&Rule{Max: 300})
}
// Rule checks that a file does not exceed a configurable number of lines.
type Rule struct {
Max int
}
// ID implements rule.Rule.
func (r *Rule) ID() string { return "MDS022" }
// Name implements rule.Rule.
func (r *Rule) Name() string { return "max-file-length" }
// Category implements rule.Rule.
func (r *Rule) Category() string { return "meta" }
// Check implements rule.Rule.
func (r *Rule) Check(f *lint.File) []lint.Diagnostic {
max := r.Max
if max <= 0 {
max = 300
}
lineCount := len(f.Lines)
// bytes.Split adds an empty trailing element for files ending
// with newline, so subtract one when the last element is empty.
if lineCount > 0 && len(f.Lines[lineCount-1]) == 0 {
lineCount--
}
if lineCount > max {
return []lint.Diagnostic{{
File: f.Path,
Line: 1,
Column: 1,
RuleID: r.ID(),
RuleName: r.Name(),
Severity: lint.Warning,
Message: fmt.Sprintf("file too long (%d > %d)", lineCount, max),
}}
}
return nil
}
// ApplySettings implements rule.Configurable.
func (r *Rule) ApplySettings(s map[string]any) error {
for k, v := range s {
switch k {
case "max":
n, ok := settings.ToInt(v)
if !ok {
return fmt.Errorf(
"max-file-length: max must be an integer, got %T", v,
)
}
r.Max = n
default:
return fmt.Errorf(
"max-file-length: unknown setting %q", k,
)
}
}
return nil
}
// DefaultSettings implements rule.Configurable.
func (r *Rule) DefaultSettings() map[string]any {
return map[string]any{"max": 300}
}
var _ rule.Configurable = (*Rule)(nil)