-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrule.go
More file actions
235 lines (210 loc) · 4.91 KB
/
Copy pathrule.go
File metadata and controls
235 lines (210 loc) · 4.91 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
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package concisenessscoring
import (
"bytes"
"fmt"
"strings"
"sync"
"github.com/jeduden/mdsmith/internal/lint"
"github.com/jeduden/mdsmith/internal/mdtext"
"github.com/jeduden/mdsmith/internal/rule"
"github.com/jeduden/mdsmith/internal/rules/settings"
"github.com/yuin/goldmark/ast"
)
const (
defaultMinScore = 0.20
defaultMinWords = 20
)
var (
scorerOnce sync.Once
globalScorer *Scorer
scorerErr error
errReportedOnce sync.Once
)
func loadScorer() (*Scorer, error) {
scorerOnce.Do(func() {
globalScorer, scorerErr = NewScorer()
})
return globalScorer, scorerErr
}
func init() {
rule.Register(&Rule{
MinScore: defaultMinScore,
MinWords: defaultMinWords,
})
}
// Rule checks paragraph conciseness using the embedded classifier.
type Rule struct {
MinScore float64
MinWords int
}
// ID implements rule.Rule.
func (r *Rule) ID() string { return "MDS029" }
// Name implements rule.Rule.
func (r *Rule) Name() string { return "conciseness-scoring" }
// Category implements rule.Rule.
func (r *Rule) Category() string { return "meta" }
// EnabledByDefault implements rule.Defaultable.
func (r *Rule) EnabledByDefault() bool { return false }
// Check implements rule.Rule.
func (r *Rule) Check(f *lint.File) []lint.Diagnostic {
scorer, err := loadScorer()
if err != nil {
return r.loadErrorDiag(f, err)
}
var diags []lint.Diagnostic
_ = ast.Walk(f.AST, func(n ast.Node, entering bool) (ast.WalkStatus, error) {
if !entering {
return ast.WalkContinue, nil
}
para, ok := n.(*ast.Paragraph)
if !ok {
return ast.WalkContinue, nil
}
if isTable(para, f) {
return ast.WalkContinue, nil
}
text := mdtext.ExtractPlainText(para, f.Source)
result := scorer.Score(text)
if result.WordCount < r.MinWords || result.Conciseness >= r.MinScore {
return ast.WalkContinue, nil
}
line := paragraphLine(para, f)
message := fmt.Sprintf(
"conciseness score too low (%.2f < %.2f); target >= %.2f",
result.Conciseness, r.MinScore, r.MinScore,
)
examples := formatExamples(result.Cues)
if examples != "" {
message += fmt.Sprintf(
"; reduce verbose cues (e.g., %s)",
examples,
)
}
diags = append(diags, lint.Diagnostic{
File: f.Path,
Line: line,
Column: 1,
RuleID: r.ID(),
RuleName: r.Name(),
Severity: lint.Warning,
Message: message,
})
return ast.WalkContinue, nil
})
return diags
}
func (r *Rule) loadErrorDiag(
f *lint.File, err error,
) []lint.Diagnostic {
var diag []lint.Diagnostic
errReportedOnce.Do(func() {
diag = []lint.Diagnostic{{
File: f.Path,
Line: 1,
Column: 1,
RuleID: r.ID(),
RuleName: r.Name(),
Severity: lint.Error,
Message: fmt.Sprintf(
"classifier load failed: %v", err,
),
}}
})
return diag
}
func formatExamples(examples []string) string {
if len(examples) == 0 {
return ""
}
limit := 2
if len(examples) < limit {
limit = len(examples)
}
values := make([]string, 0, limit)
for i := 0; i < limit; i++ {
values = append(values, fmt.Sprintf("%q", examples[i]))
}
return strings.Join(values, ", ")
}
func paragraphLine(para *ast.Paragraph, f *lint.File) int {
lines := para.Lines()
if lines.Len() > 0 {
return f.LineOfOffset(lines.At(0).Start)
}
return 1
}
// isTable returns true if the paragraph's first line starts with a pipe,
// indicating it is a markdown table (goldmark without the table extension
// parses tables as paragraphs).
func isTable(para *ast.Paragraph, f *lint.File) bool {
lines := para.Lines()
if lines.Len() == 0 {
return false
}
seg := lines.At(0)
return bytes.HasPrefix(
bytes.TrimSpace(f.Source[seg.Start:seg.Stop]),
[]byte("|"),
)
}
// ApplySettings implements rule.Configurable.
func (r *Rule) ApplySettings(s map[string]any) error {
for k, v := range s {
switch k {
case "min-score":
if err := r.setMinScore(v); err != nil {
return err
}
case "min-words":
if err := r.setMinWords(v); err != nil {
return err
}
default:
return fmt.Errorf("conciseness-scoring: unknown setting %q", k)
}
}
return nil
}
func (r *Rule) setMinScore(v any) error {
n, ok := settings.ToFloat(v)
if !ok {
return fmt.Errorf(
"conciseness-scoring: min-score must be a number, got %T",
v,
)
}
if n <= 0 || n > 1 {
return fmt.Errorf(
"conciseness-scoring: min-score must be > 0 and <= 1, got %.2f",
n,
)
}
r.MinScore = n
return nil
}
func (r *Rule) setMinWords(v any) error {
n, ok := settings.ToInt(v)
if !ok {
return fmt.Errorf(
"conciseness-scoring: min-words must be an integer, got %T",
v,
)
}
if n <= 0 {
return fmt.Errorf(
"conciseness-scoring: min-words must be > 0, got %d",
n,
)
}
r.MinWords = n
return nil
}
// DefaultSettings implements rule.Configurable.
func (r *Rule) DefaultSettings() map[string]any {
return map[string]any{
"min-score": defaultMinScore,
"min-words": defaultMinWords,
}
}
var _ rule.Configurable = (*Rule)(nil)
var _ rule.Defaultable = (*Rule)(nil)