Skip to content

Commit

Permalink
fix: handle repeated content
Browse files Browse the repository at this point in the history
  • Loading branch information
jdkato committed Jan 8, 2025
1 parent 7fc4ef2 commit 4a2b957
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
16 changes: 12 additions & 4 deletions internal/lint/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,18 @@ func (l *Linter) lintScopedValues(f *core.File, values []core.ScopedValues) erro
l.HasDir = true

wholeFile := f.Content

last := 0
index := 0

for _, matches := range values {
l.SetMetaScope(matches.Scope)
for _, v := range matches.Values {
i, line := findLineBySubstring(wholeFile, v)
i, line := findLineBySubstring(wholeFile, v, index)
if i == 0 {
return core.NewE100(f.Path, fmt.Errorf("'%s' not found", v))
}

index = i
f.SetText(v)

switch f.NormedExt {
Expand Down Expand Up @@ -73,12 +76,17 @@ func (l *Linter) lintScopedValues(f *core.File, values []core.ScopedValues) erro
return err
}

func findLineBySubstring(s, sub string) (int, string) {
func findLineBySubstring(s, sub string, last int) (int, string) {
if strings.Count(sub, "\n") > 0 {
sub = strings.Split(sub, "\n")[0]
}

for i, line := range strings.Split(s, "\n") {
if strings.Contains(line, sub) {
if i >= last && strings.Contains(line, sub) {
return i + 1, line
}
}

return 0, ""
}

Expand Down
2 changes: 2 additions & 0 deletions testdata/features/blueprints.feature
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ Feature: Blueprints
API.yml:3:10:OpenAPI.Titles:'sample API' should be capitalized
API.yml:4:25:Vale.Spelling:Did you really mean 'multiline'?
API.yml:9:70:Vale.Spelling:Did you really mean 'serrver'?
API.yml:13:17:Vale.Spelling:Did you really mean 'serrver'?
API.yml:15:70:Vale.Spelling:Did you really mean 'serrver'?
Rule.yml:3:39:Vale.Repetition:'can' is repeated!
test.py:1:3:vale.Annotations:'FIXME' left in text
test.py:11:3:vale.Annotations:'XXX' left in text
Expand Down
6 changes: 5 additions & 1 deletion testdata/fixtures/blueprints/API.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ servers:
- url: http://api.example.com/v1
description: Optional server description, e.g. Main (production) serrver
- url: http://staging-api.example.com
description: Optional server description, e.g. Internal staging server for testing
description: |
Optional server description, e.g.
Internal staging serrver for testing
- url: http://api.example.com/v2
description: Optional server description, e.g. Main (production) serrver

paths:
/users:
Expand Down

0 comments on commit 4a2b957

Please sign in to comment.