Skip to content

Commit

Permalink
feat: support exceptions in Repetition
Browse files Browse the repository at this point in the history
  • Loading branch information
jdkato committed Jun 10, 2024
1 parent 69c8767 commit ea54364
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 7 deletions.
24 changes: 17 additions & 7 deletions internal/check/repetition.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@ type Repetition struct {
Definition `mapstructure:",squash"`
Tokens []string
Max int
pattern *regexp2.Regexp
Ignorecase bool
Alpha bool
Vocab bool
Exceptions []string

exceptRe *regexp2.Regexp
pattern *regexp2.Regexp
}

// NewRepetition creates a new `repetition`-based rule.
func NewRepetition(_ *core.Config, generic baseCheck, path string) (Repetition, error) {
rule := Repetition{}
func NewRepetition(cfg *core.Config, generic baseCheck, path string) (Repetition, error) {
rule := Repetition{Vocab: true}

err := decodeRule(generic, &rule)
if err != nil {
Expand All @@ -33,18 +37,24 @@ func NewRepetition(_ *core.Config, generic baseCheck, path string) (Repetition,
return rule, err
}

re, err := updateExceptions(rule.Exceptions, cfg.AcceptedTokens, rule.Vocab)
if err != nil {
return rule, core.NewE201FromPosition(err.Error(), path, 1)
}
rule.exceptRe = re

regex := ""
if rule.Ignorecase {
regex += ignoreCase
}

regex += `(` + strings.Join(rule.Tokens, "|") + `)`
re, err := regexp2.CompileStd(regex)

made, err := regexp2.CompileStd(regex)
if err != nil {
return rule, core.NewE201FromPosition(err.Error(), path, 1)
}

rule.pattern = re
rule.pattern = made
return rule, nil
}

Expand Down Expand Up @@ -85,7 +95,7 @@ func (o Repetition) Run(blk nlp.Block, _ *core.File) ([]core.Alert, error) {
return alerts, err
}

if !strings.Contains(converted, "\n") {
if !strings.Contains(converted, "\n") && !isMatch(o.exceptRe, converted) {
floc := []int{ploc[0], loc[1]}

a, erra := makeAlert(o.Definition, floc, txt)
Expand Down
4 changes: 4 additions & 0 deletions testdata/fixtures/checks/Repetition/_vale
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
StylesPath = ../../../styles/

Vocab = Rep

[*]
Vale.Repetition = YES
2 changes: 2 additions & 0 deletions testdata/fixtures/checks/Repetition/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,5 @@ is given here. Bitwise `&`, `|` and `^` applied to boolean arguments are
equivalent to logical `&&`, `||` and `!=` evaluated in non-lazy fashion.

To create an OpenSearch cluster we’ll use the `Aiven command line interface <https://github.com/aiven/aiven-client>`_ . To install and set it up follow the instructions in `its GitHub page <https://github.com/aiven/aiven-client/>`_. However, if you prefer a visual interface, we also have a `web-based console <https://console.aiven.io/>`_ which you can use instead. Read the :doc:`getting started guide <../getting-started>` for more information.

Sometimes did did is okay.
1 change: 1 addition & 0 deletions testdata/styles/config/vocabularies/Rep/accept.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
did
Empty file.

0 comments on commit ea54364

Please sign in to comment.