Skip to content

Commit

Permalink
fix: take the best match, when available
Browse files Browse the repository at this point in the history
Fixes #455 and #673.

This isn't the ideal solution; as mentioned in `location.go`, we should
handle to in `ast.go`.
  • Loading branch information
jdkato committed Sep 7, 2023
1 parent c87ac4b commit 61ddbe8
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 3 deletions.
25 changes: 23 additions & 2 deletions internal/core/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func initialPosition(ctx, txt string, a Alert) (int, string) {
sub := strings.ToValidUTF8(a.Match, "")
pat = regexp.MustCompile(`(?:^|\b|_)` + regexp.QuoteMeta(sub) + `(?:_|\b|$)`)

fsi := pat.FindStringIndex(ctx)
fsi := pat.FindAllStringIndex(ctx, -1)
if fsi == nil {
idx = strings.Index(ctx, sub)
if idx < 0 {
Expand All @@ -37,7 +37,28 @@ func initialPosition(ctx, txt string, a Alert) (int, string) {
return guessLocation(ctx, txt, sub)
}
} else {
idx = fsi[0]
idx = fsi[0][0]
// NOTE: This is a workaround for #673.
//
// In cases where we have more than one match, we skip any that look
// like they're inside inline code (e.g., `code`).
//
// This is a bit of a hack: ideally, we'd handle this at the AST level
// by ignoring these inline code spans.
//
// TODO: What about `scope: raw`?
size := len(ctx)
for _, fs := range fsi {
start := fs[0] - 1
end := fs[1] + 1
if start > 0 && (ctx[start] == '`' || ctx[start] == '-') {
continue
} else if end < size && (ctx[end] == '`' || ctx[end] == '-') {
continue
}
idx = fs[0]
break
}
}

if strings.HasPrefix(ctx[idx:], "_") {
Expand Down
5 changes: 5 additions & 0 deletions testdata/features/checks.feature
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ Feature: Checks
Then the output should contain exactly:
"""
test.md:16:1:Vale.Spelling:Did you really mean 'gitlab'?
test.md:28:11:Vale.Spelling:Did you really mean 'typpo'?
test.md:32:17:Vale.Spelling:Did you really mean 'gfm'?
test.md:34:17:Vale.Spelling:Did you really mean 'remmark'?
"""

Scenario: Existence
Expand All @@ -74,6 +77,8 @@ Feature: Checks
When I test "checks/Substitution"
Then the output should contain exactly:
"""
test.md:5:5:Bugs.Newline:Use 'test' rather than 'linuxptp'.
test.md:8:5:Bugs.Newline:Use 'test' rather than 'linuxptp'.
"""

Scenario: Sequence
Expand Down
10 changes: 10 additions & 0 deletions testdata/fixtures/checks/Spelling/test.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,13 @@ Before you get started, you may want to familiarize yourself with [GitHub Apps](
[link title](https://log.gprd.gitlab.net/app/kibana#/discover?_g=(filters:!(),refreshInterval:(pause:!t,value:0),time:(from:now-1d,to:now))&_a=(columns:!(json.type,json.command,json.exec_time_s),filters:!(('$state':(store:appState),meta:(alias:!n,disabled:!f,index:AWSQX_Vf93rHTYrsexmk,key:json.tag,negate:!f,params:(query:redis.slowlog),type:phrase),query:(match:(json.tag:(query:redis.slowlog,type:phrase))))),index:AWSQX_Vf93rHTYrsexmk))

[foo](<bar baz>)

This is a typpo.
A sentence with [remark-gfm](i-am-a-link).
A sentence with remark-gfm.
A sentence with remark-gfm.
A sentence with gfm.
A sentence with remark.
A sentence with remmark.

A sentence with [remark-gfm](i-am-a-link).
2 changes: 2 additions & 0 deletions testdata/fixtures/checks/Substitution/.vale.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ Vocab = Cap

[*]
BasedOnStyles = proselint

Bugs.Newline = YES
7 changes: 6 additions & 1 deletion testdata/fixtures/checks/Substitution/test.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Test

The congressman voted to pass the bill.
The congressman voted to pass the bill.

The linuxptp resource.

The `linuxptp` resource.
The linuxptp resource.
9 changes: 9 additions & 0 deletions testdata/styles/Bugs/Newline.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
extends: substitution
ignorecase: false
level: error
message: Use '%s' rather than '%s'.
action:
name: replace
swap:
linuxptp: test

0 comments on commit 61ddbe8

Please sign in to comment.