@@ -12,50 +12,38 @@ import (
12
12
13
13
"github.com/golangci/golangci-lint/pkg/config"
14
14
"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 "
16
16
)
17
17
18
18
const linterName = "misspell"
19
19
20
20
func 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 )
25
24
}
26
25
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
+ }
43
35
}
44
36
45
37
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
+ },
56
39
}
57
40
58
- return nil
41
+ return goanalysis .NewLinter (
42
+ a .Name ,
43
+ a .Doc ,
44
+ []* analysis.Analyzer {a },
45
+ nil ,
46
+ ).WithLoadMode (goanalysis .LoadModeSyntax )
59
47
}
60
48
61
49
func createMisspellReplacer (settings * config.MisspellSettings ) (* misspell.Replacer , error ) {
@@ -90,13 +78,15 @@ func createMisspellReplacer(settings *config.MisspellSettings) (*misspell.Replac
90
78
return replacer , nil
91
79
}
92
80
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 {
94
82
position , isGoFile := goanalysis .GetGoFilePosition (pass , file )
95
83
if ! isGoFile {
96
84
return nil
97
85
}
98
86
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 )
100
90
if err != nil {
101
91
return fmt .Errorf ("can't get file %s contents: %w" , position .Filename , err )
102
92
}
0 commit comments