@@ -12,50 +12,38 @@ import (
1212
1313 "github.com/golangci/golangci-lint/pkg/config"
1414 "github.com/golangci/golangci-lint/pkg/goanalysis"
15- "github.com/golangci/golangci-lint/pkg/lint/linter "
15+ "github.com/golangci/golangci-lint/pkg/golinters/internal "
1616)
1717
1818const linterName = "misspell"
1919
2020func New (settings * config.MisspellSettings ) * goanalysis.Linter {
21- analyzer := & analysis.Analyzer {
22- Name : linterName ,
23- Doc : goanalysis .TheOnlyanalyzerDoc ,
24- Run : goanalysis .DummyRun ,
21+ replacer , err := createMisspellReplacer (settings )
22+ if err != nil {
23+ internal .LinterLogger .Fatalf ("%s: %v" , linterName , err )
2524 }
2625
27- return goanalysis .NewLinter (
28- linterName ,
29- "Finds commonly misspelled English words" ,
30- []* analysis.Analyzer {analyzer },
31- nil ,
32- ).WithContextSetter (func (lintCtx * linter.Context ) {
33- replacer , ruleErr := createMisspellReplacer (settings )
34-
35- analyzer .Run = func (pass * analysis.Pass ) (any , error ) {
36- if ruleErr != nil {
37- return nil , ruleErr
38- }
39-
40- err := runMisspell (lintCtx , pass , replacer , settings .Mode )
41- if err != nil {
42- return nil , err
26+ a := & analysis.Analyzer {
27+ Name : linterName ,
28+ Doc : "Finds commonly misspelled English words" ,
29+ Run : func (pass * analysis.Pass ) (any , error ) {
30+ for _ , file := range pass .Files {
31+ err := runMisspellOnFile (pass , file , replacer , settings .Mode )
32+ if err != nil {
33+ return nil , err
34+ }
4335 }
4436
4537 return nil , nil
46- }
47- }).WithLoadMode (goanalysis .LoadModeSyntax )
48- }
49-
50- func runMisspell (lintCtx * linter.Context , pass * analysis.Pass , replacer * misspell.Replacer , mode string ) error {
51- for _ , file := range pass .Files {
52- err := runMisspellOnFile (lintCtx , pass , file , replacer , mode )
53- if err != nil {
54- return err
55- }
38+ },
5639 }
5740
58- return nil
41+ return goanalysis .NewLinter (
42+ a .Name ,
43+ a .Doc ,
44+ []* analysis.Analyzer {a },
45+ nil ,
46+ ).WithLoadMode (goanalysis .LoadModeSyntax )
5947}
6048
6149func createMisspellReplacer (settings * config.MisspellSettings ) (* misspell.Replacer , error ) {
@@ -90,13 +78,15 @@ func createMisspellReplacer(settings *config.MisspellSettings) (*misspell.Replac
9078 return replacer , nil
9179}
9280
93- func runMisspellOnFile (lintCtx * linter. Context , pass * analysis.Pass , file * ast.File , replacer * misspell.Replacer , mode string ) error {
81+ func runMisspellOnFile (pass * analysis.Pass , file * ast.File , replacer * misspell.Replacer , mode string ) error {
9482 position , isGoFile := goanalysis .GetGoFilePosition (pass , file )
9583 if ! isGoFile {
9684 return nil
9785 }
9886
99- fileContent , err := lintCtx .FileCache .GetFileBytes (position .Filename )
87+ // Uses the non-adjusted file to work with cgo:
88+ // if we read the real file, the positions are wrong in some cases.
89+ fileContent , err := pass .ReadFile (pass .Fset .PositionFor (file .Pos (), false ).Filename )
10090 if err != nil {
10191 return fmt .Errorf ("can't get file %s contents: %w" , position .Filename , err )
10292 }
0 commit comments