Skip to content

Commit

Permalink
feat: add comment syntax for ignoring individual matches (#844)
Browse files Browse the repository at this point in the history
Co-authored-by: Joseph Kato <[email protected]>
  • Loading branch information
ItsVeryWindy and jdkato authored Nov 5, 2024
1 parent 13f7f75 commit 57180ee
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
13 changes: 13 additions & 0 deletions internal/core/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package core

import (
"bytes"
"encoding/json"
"os"
"path/filepath"
"regexp"
Expand All @@ -19,6 +20,8 @@ var commentControlRE = regexp.MustCompile(`^vale (.+\..+|[^.]+) = (YES|NO|on|off

var commentStyleRE = regexp.MustCompile(`^vale styles? = (.*)$`)

var commentControlMatchesRE = regexp.MustCompile(`^vale (.+\..+)(\[.+\]) = (YES|NO)$`)

// A File represents a linted text file.
type File struct {
NLP nlp.Info // -
Expand Down Expand Up @@ -316,6 +319,16 @@ func (f *File) UpdateComments(comment string) {
f.Comments["off"] = true
} else if comment == "vale on" {
f.Comments["off"] = false
} else if commentControlMatchesRE.MatchString(comment) {
check := commentControlMatchesRE.FindStringSubmatch(comment)
if len(check) == 4 {
var parts []string
if err := json.Unmarshal([]byte(check[2]), &parts); err == nil {
for i := range parts {
f.Comments[check[1]+"["+parts[i]+"]"] = check[3] == "NO"
}
}
}
} else if commentControlRE.MatchString(comment) {
check := commentControlRE.FindStringSubmatch(comment)
if len(check) == 3 {
Expand Down
3 changes: 3 additions & 0 deletions internal/lint/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,9 @@ func (l *Linter) lintBlock(f *core.File, blk nlp.Block, lines, pad int, lookup b
return err
}
for i := range alerts {
if f.QueryComments(name + "[" + alerts[i].Match + "]") {
continue
}
core.FormatAlert(&alerts[i], info.Limit, info.Level, name)
f.AddAlert(alerts[i], blk, lines, pad, lookup)
}
Expand Down
18 changes: 18 additions & 0 deletions testdata/fixtures/comments/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,24 @@ This is some text ACT test

<!-- vale vale.Redundancy = YES -->

<!-- vale vale.Redundancy["ACT test"] = NO -->

This is some text ACT test

<!-- vale vale.Redundancy["ACT test"] = YES -->

<!-- vale vale.Redundancy["OTHER"] = NO -->

This is some text ACT test

<!-- vale vale.Redundancy["OTHER"] = YES -->

<!-- vale vale.Redundancy["ACT test","OTHER"] = NO -->

This is some text ACT test

<!-- vale vale.Redundancy["ACT test"] = YES -->

This is some text ACT test

<!-- vale demo.Ending-Preposition = NO -->
Expand Down

0 comments on commit 57180ee

Please sign in to comment.