Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions internal/check/substitution.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ func NewSubstitution(cfg *core.Config, generic baseCheck, path string) (Substitu
replacement := rule.Swap[regexstr]

opens := strings.Count(regexstr, "(")
if opens != strings.Count(regexstr, "(?") &&
opens != strings.Count(regexstr, `\(`) {
if opens != strings.Count(regexstr, "(?")+strings.Count(regexstr, `\(`) {
// We rely on manually-added capture groups to associate a match
// with its replacement -- e.g.,
//
Expand Down
60 changes: 60 additions & 0 deletions internal/check/substitution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,63 @@ func TestIsDeterministic(t *testing.T) {
}
}
}

func TestRegex(t *testing.T) {
swap := map[string]interface{}{
"extends": "substitution",
"name": "Vale.Terms",
"level": "error",
"message": "Use '%s' instead of '%s'.",
"scope": "text",
"ignorecase": true,
"swap": map[string]string{
`(?:foo|bar)`: "sub",
},
}
text := "foo"
rule, err := makeSubstitution(swap)
if err != nil {
t.Fatal(err)
}

actual, err := rule.Run(nlp.NewBlock(text, text, "text"), &core.File{})
if err != nil {
t.Fatal(err)
}

expected := "Use 'sub' instead of 'foo'."
message := actual[0].Message
if message != expected {
t.Fatalf("Expected message `%s`, got `%s`", expected, message)
}
}

func TestRegexEscapedParens(t *testing.T) {
swap := map[string]interface{}{
"extends": "substitution",
"name": "Vale.Terms",
"level": "error",
"message": "Use '%s' instead of '%s'.",
"scope": "text",
"ignorecase": true,
"swap": map[string]string{
`(?!\()(?:foo|bar)(?!\))?`: "sub",
},
}
text := "(foo)"
rule, err := makeSubstitution(swap)
if err != nil {
t.Fatal(err)
}

actual, err := rule.Run(nlp.NewBlock(text, text, "text"), &core.File{})
if err != nil {
t.Fatal(err)
}

expected := "Use 'sub' instead of 'foo'."
message := actual[0].Message
if message != expected {
t.Fatalf("Expected message `%s`, got `%s`", expected, message)
}
}