4
4
"fmt"
5
5
"go/ast"
6
6
"go/token"
7
+ "os"
7
8
"strings"
8
9
"unicode"
9
10
@@ -12,50 +13,38 @@ import (
12
13
13
14
"github.com/golangci/golangci-lint/pkg/config"
14
15
"github.com/golangci/golangci-lint/pkg/goanalysis"
15
- "github.com/golangci/golangci-lint/pkg/lint/linter "
16
+ "github.com/golangci/golangci-lint/pkg/golinters/internal "
16
17
)
17
18
18
19
const linterName = "misspell"
19
20
20
21
func New (settings * config.MisspellSettings ) * goanalysis.Linter {
21
- analyzer := & analysis.Analyzer {
22
- Name : linterName ,
23
- Doc : goanalysis .TheOnlyanalyzerDoc ,
24
- Run : goanalysis .DummyRun ,
22
+ replacer , err := createMisspellReplacer (settings )
23
+ if err != nil {
24
+ internal .LinterLogger .Fatalf ("%s: %v" , linterName , err )
25
25
}
26
26
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
27
+ a := & analysis.Analyzer {
28
+ Name : linterName ,
29
+ Doc : "Finds commonly misspelled English words" ,
30
+ Run : func (pass * analysis.Pass ) (any , error ) {
31
+ for _ , file := range pass .Files {
32
+ err := runMisspellOnFile (pass , file , replacer , settings .Mode )
33
+ if err != nil {
34
+ return nil , err
35
+ }
43
36
}
44
37
45
38
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
- }
39
+ },
56
40
}
57
41
58
- return nil
42
+ return goanalysis .NewLinter (
43
+ a .URL ,
44
+ a .Doc ,
45
+ []* analysis.Analyzer {a },
46
+ nil ,
47
+ ).WithLoadMode (goanalysis .LoadModeSyntax )
59
48
}
60
49
61
50
func createMisspellReplacer (settings * config.MisspellSettings ) (* misspell.Replacer , error ) {
@@ -90,13 +79,13 @@ func createMisspellReplacer(settings *config.MisspellSettings) (*misspell.Replac
90
79
return replacer , nil
91
80
}
92
81
93
- func runMisspellOnFile (lintCtx * linter. Context , pass * analysis.Pass , file * ast.File , replacer * misspell.Replacer , mode string ) error {
82
+ func runMisspellOnFile (pass * analysis.Pass , file * ast.File , replacer * misspell.Replacer , mode string ) error {
94
83
position , isGoFile := goanalysis .GetGoFilePosition (pass , file )
95
84
if ! isGoFile {
96
85
return nil
97
86
}
98
87
99
- fileContent , err := lintCtx . FileCache . GetFileBytes (position .Filename )
88
+ fileContent , err := os . ReadFile (position .Filename )
100
89
if err != nil {
101
90
return fmt .Errorf ("can't get file %s contents: %w" , position .Filename , err )
102
91
}
0 commit comments