Skip to content

Commit

Permalink
fix: ensure Vocab exceptions are sorted by length
Browse files Browse the repository at this point in the history
This ensures that, e.g., "Vale Server" will appear before "Vale" -- effectively
making our regex use greedy alternation.
  • Loading branch information
jdkato committed Nov 19, 2020
1 parent be8fe66 commit 8991e09
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
4 changes: 1 addition & 3 deletions check/capitalization.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ func NewCapitalization(cfg *config.Config, generic baseCheck) (Capitalization, e
func() string { return "" },
true)

for term := range cfg.AcceptedTokens {
rule.Exceptions = append(rule.Exceptions, term)
}
rule.Exceptions = updateExceptions(rule.Exceptions, cfg.AcceptedTokens)

regex = fmt.Sprintf(regex, strings.Join(rule.Exceptions, "|"))
if len(rule.Exceptions) > 0 {
Expand Down
4 changes: 1 addition & 3 deletions check/conditional.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ func NewConditional(cfg *config.Config, generic baseCheck) (Conditional, error)
return rule, readStructureError(err, path)
}

for term := range cfg.AcceptedTokens {
rule.Exceptions = append(rule.Exceptions, term)
}
rule.Exceptions = updateExceptions(rule.Exceptions, cfg.AcceptedTokens)
rule.exceptRe = regexp.MustCompile(strings.Join(rule.Exceptions, "|"))

re, err = regexp.Compile(rule.Second)
Expand Down
14 changes: 14 additions & 0 deletions check/definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package check

import (
"fmt"
"sort"
"strconv"
"strings"

Expand Down Expand Up @@ -247,3 +248,16 @@ func matchToken(expected, observed string, ignorecase bool) bool {
}
return r.MatchString(observed)
}

func updateExceptions(previous []string, current map[string]struct{}) []string {
for term := range current {
previous = append(previous, term)
}

// NOTE: This is required to ensure that we have greedy alternation.
sort.Slice(previous, func(p, q int) bool {
return len(previous[p]) > len(previous[q])
})

return previous
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Vale Server
Vale
Vale Server
JavaScript

0 comments on commit 8991e09

Please sign in to comment.