-
-
Notifications
You must be signed in to change notification settings - Fork 140
/
check.go
758 lines (671 loc) · 21.6 KB
/
check.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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
package check
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/ValeLint/gospell"
"github.com/errata-ai/vale/core"
"github.com/errata-ai/vale/data"
"github.com/errata-ai/vale/rule"
"github.com/jdkato/prose/summarize"
"github.com/jdkato/prose/transform"
"github.com/jdkato/regexp"
"github.com/mitchellh/mapstructure"
"gopkg.in/yaml.v2"
)
const (
ignoreCase = `(?i)`
wordTemplate = `\b(?:%s)\b`
nonwordTemplate = `(?:%s)`
)
var defaultFilters = []*regexp.Regexp{
regexp.MustCompile(`(?:\w+)?\.\w{1,4}\b`),
regexp.MustCompile(`\b(?:[a-zA-Z]\.){2,}`),
regexp.MustCompile(`0[xX][0-9a-fA-F]+`),
regexp.MustCompile(`\w+-\w+`),
regexp.MustCompile(`[A-Z]{1}[a-z]+[A-Z]+\w+`),
regexp.MustCompile(`[0-9]`),
regexp.MustCompile(`[A-Z]+$`),
regexp.MustCompile(`\W`),
regexp.MustCompile(`\w{3,}\.\w{3,}`),
regexp.MustCompile(`@.*\b`),
}
type ruleFn func(string, *core.File) []core.Alert
// Manager controls the loading and validating of the check extension points.
type Manager struct {
AllChecks map[string]Check
Config *core.Config
}
// NewManager creates a new Manager and loads the rule definitions (that is,
// extended checks) specified by config.
func NewManager(config *core.Config) *Manager {
var style, path string
mgr := Manager{AllChecks: make(map[string]Check), Config: config}
// loadedStyles keeps track of the styles we've loaded as we go.
loadedStyles := []string{}
if mgr.Config.StylesPath == "" {
// If we're not given a StylesPath, there's nothing left to look for.
mgr.loadDefaultRules(loadedStyles)
return &mgr
}
baseDir := mgr.Config.StylesPath
for _, style = range mgr.Config.GBaseStyles {
p := filepath.Join(baseDir, style)
if core.StringInSlice(style, loadedStyles) {
// We've already loaded this style.
continue
} else if core.StringInSlice(style, defaultStyles) && !core.IsDir(p) {
continue
}
// Now we load all styles specified at the global ("*") level.
mgr.loadExternalStyle(p)
loadedStyles = append(loadedStyles, style)
}
for _, styles := range mgr.Config.SBaseStyles {
for _, style := range styles {
p := filepath.Join(baseDir, style)
if core.StringInSlice(style, defaultStyles) && !core.IsDir(p) {
continue
} else if !core.StringInSlice(style, loadedStyles) {
// Now we load all styles specified at a syntax level
//(e.g., "*.md"), assuming we didn't already load it at the
// global level.
mgr.loadExternalStyle(p)
loadedStyles = append(loadedStyles, style)
}
}
}
// Finally, after reading the user's `StylesPath`, we load our built-in
// styles:
mgr.loadDefaultRules(loadedStyles)
loadedStyles = append(loadedStyles, defaultStyles...)
for _, chk := range mgr.Config.Checks {
// Load any remaining individual rules.
if !strings.Contains(chk, ".") {
// A rule must be associated with a style (i.e., "Style[.]Rule").
continue
}
parts := strings.Split(chk, ".")
if !core.StringInSlice(parts[0], loadedStyles) {
// If this rule isn't part of an already-loaded style, we load it
// individually.
fName := parts[1] + ".yml"
path = filepath.Join(baseDir, parts[0], fName)
core.CheckError(mgr.loadCheck(fName, path))
}
}
return &mgr
}
func makeRegexp(
template string,
noCase bool,
word func() bool,
callback func() string,
append bool,
) string {
regex := ""
if word() {
if template != "" {
regex += template
} else {
regex += wordTemplate
}
} else {
regex += nonwordTemplate
}
if append {
regex += callback()
} else {
regex = callback() + regex
}
if noCase {
regex = ignoreCase + regex
}
return regex
}
func formatMessages(msg string, desc string, subs ...string) (string, string) {
return core.FormatMessage(msg, subs...), core.FormatMessage(desc, subs...)
}
func makeAlert(chk Definition, loc []int, txt string) core.Alert {
match := txt[loc[0]:loc[1]]
a := core.Alert{
Check: chk.Name, Severity: chk.Level, Span: loc, Link: chk.Link, Match: match}
a.Message, a.Description = formatMessages(chk.Message, chk.Description, match)
return a
}
func checkConditional(txt string, chk Conditional, f *core.File, r []*regexp.Regexp) []core.Alert {
alerts := []core.Alert{}
// We first look for the consequent of the conditional statement.
// For example, if we're ensuring that abbriviations have been defined
// parenthetically, we'd have something like:
// "WHO" [antecedent], "World Health Organization (WHO)" [consequent]
// In other words: if "WHO" exists, it must also have a definition -- which
// we're currently looking for.
matches := r[0].FindAllStringSubmatch(txt, -1)
for _, mat := range matches {
if len(mat) > 1 {
// If we find one, we store it in a slice associated with this
// particular file.
f.Sequences = append(f.Sequences, mat[1])
}
}
// Now we look for the antecedent.
locs := r[1].FindAllStringIndex(txt, -1)
for _, loc := range locs {
s := txt[loc[0]:loc[1]]
if !core.StringInSlice(s, f.Sequences) && !core.StringInSlice(s, chk.Exceptions) {
// If we've found one (e.g., "WHO") and we haven't marked it as
// being defined previously, send an Alert.
alerts = append(alerts, makeAlert(chk.Definition, loc, txt))
}
}
return alerts
}
func checkExistence(txt string, chk Existence, f *core.File, r *regexp.Regexp) []core.Alert {
alerts := []core.Alert{}
locs := r.FindAllStringIndex(txt, -1)
for _, loc := range locs {
alerts = append(alerts, makeAlert(chk.Definition, loc, txt))
}
return alerts
}
func checkOccurrence(txt string, chk Occurrence, f *core.File, r *regexp.Regexp, lim int) []core.Alert {
var loc []int
alerts := []core.Alert{}
locs := r.FindAllStringIndex(txt, -1)
occurrences := len(locs)
if occurrences > lim {
loc = []int{locs[0][0], locs[occurrences-1][1]}
a := core.Alert{Check: chk.Name, Severity: chk.Level, Span: loc,
Link: chk.Link}
a.Message = chk.Message
a.Description = chk.Description
alerts = append(alerts, a)
}
return alerts
}
func checkRepetition(txt string, chk Repetition, f *core.File, r *regexp.Regexp) []core.Alert {
var curr, prev string
var hit bool
var ploc []int
var count int
alerts := []core.Alert{}
for _, loc := range r.FindAllStringIndex(txt, -1) {
curr = strings.TrimSpace(txt[loc[0]:loc[1]])
if chk.Ignorecase {
hit = strings.ToLower(curr) == strings.ToLower(prev) && curr != ""
} else {
hit = curr == prev && curr != ""
}
hit = hit && (!chk.Alpha || core.IsLetter(curr))
if hit {
count++
}
if hit && count > chk.Max {
if !strings.Contains(txt[ploc[0]:loc[1]], "\n") {
floc := []int{ploc[0], loc[1]}
a := core.Alert{Check: chk.Name, Severity: chk.Level, Span: floc,
Link: chk.Link}
a.Message, a.Description = formatMessages(chk.Message,
chk.Description, curr)
alerts = append(alerts, a)
count = 0
}
}
ploc = loc
prev = curr
}
return alerts
}
func checkSubstitution(txt string, chk Substitution, f *core.File, r *regexp.Regexp, repl []string) []core.Alert {
alerts := []core.Alert{}
pos := false
// Leave early if we can to avoid calling `FindAllStringSubmatchIndex`
// unnecessarily.
if !r.MatchString(txt) {
return alerts
}
for _, submat := range r.FindAllStringSubmatchIndex(txt, -1) {
for idx, mat := range submat {
if mat != -1 && idx > 0 && idx%2 == 0 {
loc := []int{mat, submat[idx+1]}
// Based on the current capture group (`idx`), we can determine
// the associated replacement string by using the `repl` slice:
expected := repl[(idx/2)-1]
observed := strings.TrimSpace(txt[loc[0]:loc[1]])
if expected != observed {
if chk.POS != "" {
// If we're given a POS pattern, check that it matches.
//
// If it doesn't match, the alert doesn't get added to
// a File (i.e., `hide` == true).
pos = core.CheckPOS(loc, chk.POS, txt)
}
a := core.Alert{
Check: chk.Name, Severity: chk.Level, Span: loc,
Link: chk.Link, Hide: pos, Match: observed}
a.Message, a.Description = formatMessages(chk.Message,
chk.Description, expected, observed)
alerts = append(alerts, a)
}
}
}
}
return alerts
}
func checkConsistency(txt string, chk Consistency, f *core.File, r *regexp.Regexp, opts []string) []core.Alert {
alerts := []core.Alert{}
loc := []int{}
matches := r.FindAllStringSubmatchIndex(txt, -1)
for _, submat := range matches {
for idx, mat := range submat {
if mat != -1 && idx > 0 && idx%2 == 0 {
loc = []int{mat, submat[idx+1]}
f.Sequences = append(f.Sequences, r.SubexpNames()[idx/2])
}
}
}
if matches != nil && core.AllStringsInSlice(opts, f.Sequences) {
chk.Name = chk.Extends
alerts = append(alerts, makeAlert(chk.Definition, loc, txt))
}
return alerts
}
func checkCapitalization(txt string, chk Capitalization, f *core.File) []core.Alert {
alerts := []core.Alert{}
if !chk.Check(txt, chk.Exceptions) {
alerts = append(alerts, makeAlert(chk.Definition, []int{0, len(txt)}, txt))
}
return alerts
}
func checkReadability(txt string, chk Readability, f *core.File) []core.Alert {
var grade float64
alerts := []core.Alert{}
doc := summarize.NewDocument(txt)
if core.StringInSlice("SMOG", chk.Metrics) {
grade += doc.SMOG()
}
if core.StringInSlice("Gunning Fog", chk.Metrics) {
grade += doc.GunningFog()
}
if core.StringInSlice("Coleman-Liau", chk.Metrics) {
grade += doc.ColemanLiau()
}
if core.StringInSlice("Flesch-Kincaid", chk.Metrics) {
grade += doc.FleschKincaid()
}
if core.StringInSlice("Automated Readability", chk.Metrics) {
grade += doc.AutomatedReadability()
}
grade = grade / float64(len(chk.Metrics))
if grade > chk.Grade {
a := core.Alert{Check: chk.Name, Severity: chk.Level,
Span: []int{0, len(txt)}, Link: chk.Link}
a.Message, a.Description = formatMessages(chk.Message, chk.Description,
fmt.Sprintf("%.2f", grade))
alerts = append(alerts, a)
}
return alerts
}
func checkSpelling(txt string, chk Spelling, gs *gospell.GoSpell, f *core.File) []core.Alert {
alerts := []core.Alert{}
OUTER:
for _, w := range core.WordTokenizer.Tokenize(txt) {
for _, filter := range chk.Filters {
if filter.MatchString(w) {
continue OUTER
}
}
known := gs.Spell(w) || gs.Spell(strings.ToLower(w)) || gs.Spell(strings.Title(w))
if !known {
offset := strings.Index(txt, w)
loc := []int{offset, offset + len(w)}
a := core.Alert{Check: chk.Name, Severity: chk.Level, Span: loc,
Link: chk.Link, Match: strings.ToLower(w)}
a.Message, a.Description = formatMessages(chk.Message,
chk.Description, w)
alerts = append(alerts, a)
}
}
return alerts
}
func (mgr *Manager) addReadabilityCheck(chkName string, chkDef Readability) {
if core.AllStringsInSlice(chkDef.Metrics, readabilityMetrics) {
fn := func(text string, file *core.File) []core.Alert {
return checkReadability(text, chkDef, file)
}
// NOTE: This is the only extension point that doesn't support scoping.
// The reason for this is that we need to split on sentences to
// calculate readability, which means that specifying a scope smaller
// than a paragraph or including non-block level content (i.e.,
// headings, list items or table cells) doesn't make sense.
chkDef.Definition.Scope = "summary"
mgr.updateAllChecks(chkDef.Definition, fn)
}
}
func (mgr *Manager) addCapitalizationCheck(chkName string, chkDef Capitalization) {
if chkDef.Match == "$title" {
var tc *transform.TitleConverter
if chkDef.Style == "Chicago" {
tc = transform.NewTitleConverter(transform.ChicagoStyle)
} else {
tc = transform.NewTitleConverter(transform.APStyle)
}
chkDef.Check = func(s string, ignore []string) bool {
return title(s, ignore, tc)
}
} else if f, ok := varToFunc[chkDef.Match]; ok {
chkDef.Check = f
} else {
re, err := regexp.Compile(chkDef.Match)
if !core.CheckError(err) {
return
}
chkDef.Check = func(s string, ignore []string) bool {
return re.MatchString(s) || core.StringInSlice(s, ignore)
}
}
fn := func(text string, file *core.File) []core.Alert {
return checkCapitalization(text, chkDef, file)
}
mgr.updateAllChecks(chkDef.Definition, fn)
}
func (mgr *Manager) addConsistencyCheck(chkName string, chkDef Consistency) {
var chkRE string
regex := makeRegexp(
mgr.Config.WordTemplate,
chkDef.Ignorecase,
func() bool { return !chkDef.Nonword },
func() string { return "" }, true)
chkKey := strings.Split(chkName, ".")[1]
count := 0
for v1, v2 := range chkDef.Either {
count += 2
subs := []string{
fmt.Sprintf("%s%d", chkKey, count), fmt.Sprintf("%s%d", chkKey, count+1)}
chkRE = fmt.Sprintf("(?P<%s>%s)|(?P<%s>%s)", subs[0], v1, subs[1], v2)
chkRE = fmt.Sprintf(regex, chkRE)
re, err := regexp.Compile(chkRE)
if core.CheckError(err) {
chkDef.Extends = chkName
chkDef.Name = fmt.Sprintf("%s.%s", chkName, v1)
fn := func(text string, file *core.File) []core.Alert {
return checkConsistency(text, chkDef, file, re, subs)
}
mgr.updateAllChecks(chkDef.Definition, fn)
}
}
}
func (mgr *Manager) addExistenceCheck(chkName string, chkDef Existence) {
regex := makeRegexp(
mgr.Config.WordTemplate,
chkDef.Ignorecase,
func() bool { return !chkDef.Nonword && len(chkDef.Tokens) > 0 },
func() string { return strings.Join(chkDef.Raw, "") },
chkDef.Append)
regex = fmt.Sprintf(regex, strings.Join(chkDef.Tokens, "|"))
re, err := regexp.Compile(regex)
if core.CheckError(err) {
fn := func(text string, file *core.File) []core.Alert {
return checkExistence(text, chkDef, file, re)
}
mgr.updateAllChecks(chkDef.Definition, fn)
}
}
func (mgr *Manager) addRepetitionCheck(chkName string, chkDef Repetition) {
regex := ""
if chkDef.Ignorecase {
regex += ignoreCase
}
regex += `(` + strings.Join(chkDef.Tokens, "|") + `)`
re, err := regexp.Compile(regex)
if core.CheckError(err) {
fn := func(text string, file *core.File) []core.Alert {
return checkRepetition(text, chkDef, file, re)
}
mgr.updateAllChecks(chkDef.Definition, fn)
}
}
func (mgr *Manager) addOccurrenceCheck(chkName string, chkDef Occurrence) {
re, err := regexp.Compile(chkDef.Token)
if core.CheckError(err) && chkDef.Max >= 1 {
fn := func(text string, file *core.File) []core.Alert {
return checkOccurrence(text, chkDef, file, re, chkDef.Max)
}
mgr.updateAllChecks(chkDef.Definition, fn)
}
}
func (mgr *Manager) addConditionalCheck(chkName string, chkDef Conditional) {
var re *regexp.Regexp
var expression []*regexp.Regexp
var err error
re, err = regexp.Compile(chkDef.Second)
if !core.CheckError(err) {
return
}
expression = append(expression, re)
re, err = regexp.Compile(chkDef.First)
if !core.CheckError(err) {
return
}
expression = append(expression, re)
fn := func(text string, file *core.File) []core.Alert {
return checkConditional(text, chkDef, file, expression)
}
mgr.updateAllChecks(chkDef.Definition, fn)
}
func (mgr *Manager) addSubstitutionCheck(chkName string, chkDef Substitution) {
tokens := ""
regex := makeRegexp(
mgr.Config.WordTemplate,
chkDef.Ignorecase,
func() bool { return !chkDef.Nonword },
func() string { return "" }, true)
replacements := []string{}
for regexstr, replacement := range chkDef.Swap {
opens := strings.Count(regexstr, "(")
if opens != strings.Count(regexstr, "?:") &&
opens != strings.Count(regexstr, `\(`) {
// We rely on manually-added capture groups to associate a match
// with its replacement -- e.g.,
//
// `(foo)|(bar)`, [replacement1, replacement2]
//
// where the first capture group ("foo") corresponds to the first
// element of the replacements slice ("replacement1"). This means
// that we can only accept non-capture groups from the user (the
// indexing would be mixed up otherwise).
//
// TODO: Should we change this? Perhaps by creating a map of regex
// to replacements?
continue
}
tokens += `(` + regexstr + `)|`
replacements = append(replacements, replacement)
}
regex = fmt.Sprintf(regex, strings.TrimRight(tokens, "|"))
re, err := regexp.Compile(regex)
if core.CheckError(err) {
fn := func(text string, file *core.File) []core.Alert {
return checkSubstitution(text, chkDef, file, re, replacements)
}
mgr.updateAllChecks(chkDef.Definition, fn)
}
}
func (mgr *Manager) addSpellingCheck(chkName string, chkDef Spelling) {
var model *gospell.GoSpell
var err error
affloc := core.DeterminePath(mgr.Config.Path, chkDef.Aff)
dicloc := core.DeterminePath(mgr.Config.Path, chkDef.Dic)
undefined := (chkDef.Aff == "" || chkDef.Dic == "")
if undefined || !(core.FileExists(affloc) && core.FileExists(dicloc)) {
// Fall back to the defaults:
aff, _ := data.Asset("data/en_US-web.aff")
dic, _ := data.Asset("data/en_US-web.dic")
model, err = gospell.NewGoSpellReader(bytes.NewReader(aff), bytes.NewReader(dic))
} else {
model, err = gospell.NewGoSpell(affloc, dicloc)
}
if chkDef.Ignore != "" {
vocab, _ := filepath.Abs(chkDef.Ignore)
_, exists := model.AddWordListFile(vocab)
core.CheckError(exists)
}
if !chkDef.Custom {
chkDef.Filters = append(chkDef.Filters, defaultFilters...)
}
fn := func(text string, file *core.File) []core.Alert {
return checkSpelling(text, chkDef, model, file)
}
if core.CheckError(err) {
mgr.updateAllChecks(chkDef.Definition, fn)
}
}
func (mgr *Manager) updateAllChecks(chkDef Definition, fn ruleFn) {
chk := Check{Rule: fn, Extends: chkDef.Extends, Code: chkDef.Code}
chk.Level = core.LevelToInt[chkDef.Level]
chk.Scope = core.Selector{Value: chkDef.Scope}
mgr.AllChecks[chkDef.Name] = chk
}
func (mgr *Manager) makeCheck(generic map[string]interface{}, extends, chkName string) {
// TODO: make this less ugly ...
if extends == "existence" {
def := Existence{}
if err := mapstructure.Decode(generic, &def); err == nil {
mgr.addExistenceCheck(chkName, def)
}
} else if extends == "substitution" {
def := Substitution{}
if err := mapstructure.Decode(generic, &def); err == nil {
mgr.addSubstitutionCheck(chkName, def)
}
} else if extends == "occurrence" {
def := Occurrence{}
if err := mapstructure.Decode(generic, &def); err == nil {
mgr.addOccurrenceCheck(chkName, def)
}
} else if extends == "repetition" {
def := Repetition{}
if err := mapstructure.Decode(generic, &def); err == nil {
mgr.addRepetitionCheck(chkName, def)
}
} else if extends == "consistency" {
def := Consistency{}
if err := mapstructure.Decode(generic, &def); err == nil {
mgr.addConsistencyCheck(chkName, def)
}
} else if extends == "conditional" {
def := Conditional{}
if err := mapstructure.Decode(generic, &def); err == nil {
mgr.addConditionalCheck(chkName, def)
}
} else if extends == "capitalization" {
def := Capitalization{}
if err := mapstructure.Decode(generic, &def); err == nil {
mgr.addCapitalizationCheck(chkName, def)
}
} else if extends == "readability" {
def := Readability{}
if err := mapstructure.Decode(generic, &def); err == nil {
mgr.addReadabilityCheck(chkName, def)
}
} else if extends == "spelling" {
def := Spelling{}
if generic["filters"] != nil {
// We pre-compile user-provided filters for efficiency.
//
// NOTE: This makes a big difference: ~50s -> ~13s.
for _, filter := range generic["filters"].([]interface{}) {
if pat, e := regexp.Compile(filter.(string)); e == nil {
// TODO: Should we report malformed patterns?
def.Filters = append(def.Filters, pat)
}
}
delete(generic, "filters")
}
if err := mapstructure.Decode(generic, &def); err == nil {
mgr.addSpellingCheck(chkName, def)
}
}
}
func validateDefinition(generic map[string]interface{}, name string) error {
msg := name + ": %s!"
if point, ok := generic["extends"]; !ok {
return fmt.Errorf(msg, "missing extension point")
} else if !core.StringInSlice(point.(string), extensionPoints) {
return fmt.Errorf(msg, "unknown extension point")
} else if _, ok := generic["message"]; !ok {
return fmt.Errorf(msg, "missing message")
}
return nil
}
func (mgr *Manager) addCheck(file []byte, chkName string) error {
// Load the rule definition.
generic := map[string]interface{}{}
err := yaml.Unmarshal(file, &generic)
if err != nil {
return fmt.Errorf("%s: %s", chkName, err.Error())
} else if defErr := validateDefinition(generic, chkName); defErr != nil {
return defErr
}
// Set default values, if necessary.
generic["name"] = chkName
if level, ok := mgr.Config.RuleToLevel[chkName]; ok {
generic["level"] = level
} else if _, ok := generic["level"]; !ok {
generic["level"] = "warning"
}
if _, ok := generic["scope"]; !ok {
generic["scope"] = "text"
}
mgr.makeCheck(generic, generic["extends"].(string), chkName)
return nil
}
func (mgr *Manager) loadExternalStyle(path string) {
err := filepath.Walk(path,
func(fp string, fi os.FileInfo, err error) error {
if err != nil || fi.IsDir() {
return nil
}
core.CheckError(mgr.loadCheck(fi.Name(), fp))
return nil
})
core.CheckError(err)
}
func (mgr *Manager) loadCheck(fName string, fp string) error {
if strings.HasSuffix(fName, ".yml") {
f, err := ioutil.ReadFile(fp)
if !core.CheckError(err) {
return err
}
style := filepath.Base(filepath.Dir(fp))
chkName := style + "." + strings.Split(fName, ".")[0]
if _, ok := mgr.AllChecks[chkName]; ok {
return fmt.Errorf("(%s): duplicate check", chkName)
}
return mgr.addCheck(f, chkName)
}
return nil
}
func (mgr *Manager) loadDefaultRules(loaded []string) {
for _, style := range defaultStyles {
if core.StringInSlice(style, loaded) {
// The user has a style on their `StylesPath` with the same name as
// a built-in style.
continue
}
rules, _ := rule.AssetDir(filepath.Join("rule", style))
for _, name := range rules {
b, err := rule.Asset(filepath.Join("rule", style, name))
if err != nil {
continue
}
identifier := strings.Join([]string{
style, strings.Split(name, ".")[0]}, ".")
core.CheckError(mgr.addCheck(b, identifier))
}
}
}