From 8ab68c807e4c2eed1ae2f01a3dbd1ae6ca540ace Mon Sep 17 00:00:00 2001 From: Joseph Kato Date: Thu, 12 Nov 2020 14:31:17 -0800 Subject: [PATCH] fix: ensure capitalization exceptions are word-bounded See #268. --- check/capitalization.go | 11 ++++++++++- check/variables.go | 7 +++++-- check/variables_test.go | 12 ++++++++++-- .../projects/ini/styles/Vocab/Basic/accept.txt | 2 +- 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/check/capitalization.go b/check/capitalization.go index a31c2c3a..53bf285d 100644 --- a/check/capitalization.go +++ b/check/capitalization.go @@ -1,6 +1,7 @@ package check import ( + "fmt" "strings" "github.com/errata-ai/vale/v2/config" @@ -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" { diff --git a/check/variables.go b/check/variables.go index be0f074a..82dc1964 100755 --- a/check/variables.go +++ b/check/variables.go @@ -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 { diff --git a/check/variables_test.go b/check/variables_test.go index c1502d17..5a8c1118 100755 --- a/check/variables_test.go +++ b/check/variables_test.go @@ -1,6 +1,7 @@ package check import ( + "fmt" "strings" "testing" @@ -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) } } } diff --git a/fixtures/misc/filesystem/projects/ini/styles/Vocab/Basic/accept.txt b/fixtures/misc/filesystem/projects/ini/styles/Vocab/Basic/accept.txt index 62c4ca65..98acc6df 100755 --- a/fixtures/misc/filesystem/projects/ini/styles/Vocab/Basic/accept.txt +++ b/fixtures/misc/filesystem/projects/ini/styles/Vocab/Basic/accept.txt @@ -1,3 +1,3 @@ +Vale Server Vale JavaScript -Server