-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
misspell.go
69 lines (58 loc) · 1.49 KB
/
misspell.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package golinters
import (
"context"
"fmt"
"go/token"
"io/ioutil"
"strings"
"github.com/client9/misspell"
"github.com/golangci/golangci-lint/pkg/lint/linter"
"github.com/golangci/golangci-lint/pkg/result"
)
type Misspell struct{}
func (Misspell) Name() string {
return "misspell"
}
func (Misspell) Desc() string {
return "Finds commonly misspelled English words in comments"
}
func (lint Misspell) Run(ctx context.Context, lintCtx *linter.Context) ([]result.Issue, error) {
r := misspell.Replacer{
Replacements: misspell.DictMain,
}
// Figure out regional variations
locale := lintCtx.Settings().Misspell.Locale
switch strings.ToUpper(locale) {
case "":
// nothing
case "US":
r.AddRuleList(misspell.DictAmerican)
case "UK", "GB":
r.AddRuleList(misspell.DictBritish)
case "NZ", "AU", "CA":
return nil, fmt.Errorf("unknown locale: %q", locale)
}
r.Compile()
var res []result.Issue
for _, f := range lintCtx.PkgProgram.Files(lintCtx.Cfg.Run.AnalyzeTests) {
fileContent, err := ioutil.ReadFile(f)
if err != nil {
return nil, fmt.Errorf("can't read file %s: %s", f, err)
}
_, diffs := r.ReplaceGo(string(fileContent))
for _, diff := range diffs {
text := fmt.Sprintf("`%s` is a misspelling of `%s`", diff.Original, diff.Corrected)
pos := token.Position{
Filename: f,
Line: diff.Line,
Column: diff.Column + 1,
}
res = append(res, result.Issue{
Pos: pos,
Text: text,
FromLinter: lint.Name(),
})
}
}
return res, nil
}