Skip to content

Commit ff6de4d

Browse files
committed
fix: stop sharing per-file scope state across goroutines
Signed-off-by: Joseph Kato <joseph@jdkato.io>
1 parent 5d413a0 commit ff6de4d

7 files changed

Lines changed: 20 additions & 31 deletions

File tree

cmd/vale/main.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ func doLint(args []string, l *lint.Linter, glob string) ([]*core.File, error) {
5656
fmt.Errorf("argument '%s' does not exist", file),
5757
)
5858
}
59-
l.HasDir = status == 0
6059
input = append(input, file)
6160
}
6261
linted, err = l.Lint(input, glob)

internal/core/file.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ type File struct {
5050
lineIdxCtx string // the context lineIdx was built from
5151
simple bool // -
5252
Lookup bool // -
53+
MetaScope string // extra scope context, e.g. a YAML key or comment
5354
}
5455

5556
// lineStarts returns the byte offset at which each line of ctx begins.
@@ -493,3 +494,14 @@ func (f *File) ResetComments() {
493494
}
494495
}
495496
}
497+
498+
// SetMetaScope sets an optional scope appended to each check's scope, giving
499+
// it extra context. It lives on the file because files are linted
500+
// concurrently.
501+
func (f *File) SetMetaScope(scope string) {
502+
if scope != "" {
503+
f.MetaScope = "." + scope
504+
} else {
505+
f.MetaScope = ""
506+
}
507+
}

internal/lint/ast.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ func (l *Linter) lintScope(f *core.File, state *walker, txt string) error {
257257
txt = strings.TrimLeft(txt, " ")
258258
shift -= len(txt)
259259

260-
b := state.block(txt, withClasses(scope, state)+l.metaScope+f.RealExt)
260+
b := state.block(txt, withClasses(scope, state)+f.MetaScope+f.RealExt)
261261

262262
// Prose, not just a block: a list item or a heading is made of
263263
// sentences the same way a paragraph is, and only this path segments
@@ -283,7 +283,7 @@ func (l *Linter) lintScope(f *core.File, state *walker, txt string) error {
283283

284284
f.Summary.WriteString(txt + "\n\n")
285285

286-
b := state.block(txt, withClasses("text", state)+l.metaScope+f.RealExt)
286+
b := state.block(txt, withClasses("text", state)+f.MetaScope+f.RealExt)
287287
if err := l.lintProse(f, b, state.lines); err != nil {
288288
return err
289289
}
@@ -318,7 +318,7 @@ func (l *Linter) lintInline(f *core.File, state *walker, blk nlp.Block, lines, s
318318
}
319319

320320
b := nlp.NewLinedBlock(
321-
blk.Context, cap.text, cap.scope+l.metaScope+f.RealExt, blk.Line)
321+
blk.Context, cap.text, cap.scope+f.MetaScope+f.RealExt, blk.Line)
322322
b.Offset = at
323323

324324
// Without an offset the match has to be located by searching, which is

internal/lint/code.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func (l *Linter) lintCode(f *core.File) error {
5151

5252
last := 0
5353
for _, comment := range comments {
54-
l.SetMetaScope(comment.Scope)
54+
f.SetMetaScope(comment.Scope)
5555
if core.StringInSlice("comment", ignored) {
5656
continue
5757
} else if core.StringInSlice(comment.Scope, ignored) {

internal/lint/data.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,12 @@ func (l *Linter) lintData(f *core.File) error {
3030

3131
func (l *Linter) lintScopedValues(f *core.File, values []core.ScopedValues) error {
3232
var err error
33-
// We want to set up our processing servers as if we were dealing with
34-
// a directory since we likely have many fragments to convert.
35-
l.HasDir = true
36-
3733
wholeFile := f.Content
3834
srcLines := strings.Split(wholeFile, "\n")
3935
last := 0
4036

4137
for _, match := range values {
42-
l.SetMetaScope(match.Scope)
38+
f.SetMetaScope(match.Scope)
4339

4440
seen := make(map[string]int)
4541
for _, sv := range match.Values {

internal/lint/fragment.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,6 @@ func commentPadding(comment code.Comment, line int, source string, lang *code.La
7676
}
7777

7878
func (l *Linter) lintFragments(f *core.File) error {
79-
// We want to set up our processing servers as if we were dealing with
80-
// a directory since we likely have many fragments to convert.
81-
l.HasDir = true
82-
8379
lang, err := code.GetLanguageFromExt(f.RealExt)
8480
if err != nil {
8581
return err
@@ -99,7 +95,7 @@ func (l *Linter) lintFragments(f *core.File) error {
9995

10096
last := 0
10197
for _, comment := range comments {
102-
l.SetMetaScope(comment.Scope)
98+
f.SetMetaScope(comment.Scope)
10399
f.SetText(comment.Text)
104100

105101
switch f.NormedExt {

internal/lint/lint.go

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ type Linter struct {
2323
Manager *check.Manager
2424
glob *glob.Glob
2525
client *http.Client
26-
HasDir bool
2726
nonGlobal bool
28-
metaScope string
2927

3028
// adoc holds the Asciidoctor processes this run is using, and adocOnce
3129
// starts them the first time an AsciiDoc file is seen.
@@ -104,18 +102,6 @@ func (l *Linter) LintString(src string) ([]*core.File, error) {
104102
return []*core.File{linted.file}, linted.err
105103
}
106104

107-
// SetMetaScope sets an optional meta scope.
108-
//
109-
// A meta scope is a string that is appended to the end of each check's scope
110-
// providing extra context for the check.
111-
func (l *Linter) SetMetaScope(scope string) {
112-
if scope != "" {
113-
l.metaScope = "." + scope
114-
} else {
115-
l.metaScope = ""
116-
}
117-
}
118-
119105
// Lint src according to its format.
120106
func (l *Linter) Lint(input []string, pat string) ([]*core.File, error) {
121107
var linted []*core.File
@@ -306,12 +292,12 @@ func (l *Linter) lintProse(f *core.File, blk nlp.Block, lines int) error {
306292
}
307293

308294
func (l *Linter) lintTxt(f *core.File) error {
309-
block := nlp.NewBlock("", f.Content, "text"+l.metaScope+f.RealExt)
295+
block := nlp.NewBlock("", f.Content, "text"+f.MetaScope+f.RealExt)
310296
return l.lintProse(f, block, len(f.Lines))
311297
}
312298

313299
func (l *Linter) lintLines(f *core.File) error {
314-
block := nlp.NewBlock("", f.Content, "text"+l.metaScope+f.RealExt)
300+
block := nlp.NewBlock("", f.Content, "text"+f.MetaScope+f.RealExt)
315301
return l.lintBlock(f, block, len(f.Lines), 0, true)
316302
}
317303

0 commit comments

Comments
 (0)