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