errata-ai / vale

:pencil: A markup-aware linter for prose built with speed and extensibility in mind.
https://vale.sh
MIT License
4.3k stars 144 forks source link

Mixing escaped parentheses and non-capturing groups in substitution rule leads to an error #804

Closed teners closed 2 months ago

teners commented 2 months ago

Check for existing issues

Environment

3.3.0 in Vale Studio

Describe the bug / provide steps to reproduce it

Using regexes with non-capturing groups together with escaped parenthesis in substitution rules leads to an error.

Expected behaviour: regular expression is considered valid.

Current behaviour: error capture group not supported; use '(?:' instead of '('.

Package example:

extends: substitution
message: "Use '%s' instead of '%s'."
link: "https://example.com/styleguide/style/wordlist/"
level: error
ignorecase: false
action:
  name: replace
swap:
 '(?:\()(?:api|Api)(?:\))?': API

(I realize that escaped parentheses in this particular regex are redundant — I've actually got more difficult case, but I've prepared this simpler one for the issue demonstration)

In Vale Studio: https://studio.vale.sh/s/e49686ef79df096fefe7886dce4fb9e1

In regex101: https://regex101.com/r/6G21Pd/1

Seems that the issue is in these lines: ./internal/check/substitution.go#L69-L70.

I've made an attempt at fixing it: https://github.com/teners/vale/commit/54203033a56581489828ddc92079be7e51da7366, if that's the right way I can submit a PR.

jdkato commented 2 months ago

Thanks for looking into this -- your solution looks good.