Skip to content

Commit

Permalink
fix: ensure capitalization exceptions are word-bounded
Browse files Browse the repository at this point in the history
See #268.
  • Loading branch information
jdkato committed Nov 12, 2020
1 parent 864264a commit 8ab68c8
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
11 changes: 10 additions & 1 deletion check/capitalization.go
@@ -1,6 +1,7 @@
package check

import (
"fmt"
"strings"

"github.com/errata-ai/vale/v2/config"
Expand Down Expand Up @@ -38,11 +39,19 @@ func NewCapitalization(cfg *config.Config, generic baseCheck) (Capitalization, e
return rule, readStructureError(err, path)
}

regex := makeRegexp(
"",
false,
func() bool { return true },
func() string { return "" },
true)

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

rule.exceptRe = regexp.MustCompile(regex)
if rule.Match == "$title" {
var tc *transform.TitleConverter
if rule.Style == "Chicago" {
Expand Down
7 changes: 5 additions & 2 deletions check/variables.go
Expand Up @@ -16,8 +16,11 @@ func isMatch(r *regexp.Regexp, s string) bool {
}

func makeExceptions(ignore []string) *regexp.Regexp {
ignore = append(ignore, `[\p{N}\p{L}*]+[^\s]*`)
return regexp.MustCompile(`(?:` + strings.Join(ignore, "|") + `)`)
s := ""
if len(ignore) > 0 {
s = `\b(?:` + strings.Join(ignore, "|") + `)\b|`
}
return regexp.MustCompile(s + `[\p{N}\p{L}*]+[^\s]*`)
}

func lower(s string, ignore []string, re *regexp.Regexp) bool {
Expand Down
12 changes: 10 additions & 2 deletions check/variables_test.go
@@ -1,6 +1,7 @@
package check

import (
"fmt"
"strings"
"testing"

Expand Down Expand Up @@ -45,12 +46,19 @@ func TestSentence(t *testing.T) {
for _, h := range headings {
var r *regexp.Regexp
if len(h.exceptions) > 0 {
r = regexp.MustCompile(strings.Join(h.exceptions, "|"))
regex := makeRegexp(
"",
false,
func() bool { return true },
func() string { return "" },
true)
regex = fmt.Sprintf(regex, strings.Join(h.exceptions, "|"))
r = regexp.MustCompile(regex)
}

s := sentence(h.heading, h.exceptions, h.indicators, r)
if s != h.match {
t.Errorf("expected = %v, got = %v", s, h.match)
t.Errorf("expected = %v, got = %v (%s)", h.match, s, h.heading)
}
}
}
@@ -1,3 +1,3 @@
Vale Server
Vale
JavaScript
Server

0 comments on commit 8ab68c8

Please sign in to comment.