@@ -29,7 +29,7 @@ func NewMisspell(settings *config.MisspellSettings) *goanalysis.Linter {
29
29
30
30
return goanalysis .NewLinter (
31
31
misspellName ,
32
- "Finds commonly misspelled English words in comments " ,
32
+ "Finds commonly misspelled English words" ,
33
33
[]* analysis.Analyzer {analyzer },
34
34
nil ,
35
35
).WithContextSetter (func (lintCtx * linter.Context ) {
@@ -40,7 +40,7 @@ func NewMisspell(settings *config.MisspellSettings) *goanalysis.Linter {
40
40
return nil , ruleErr
41
41
}
42
42
43
- issues , err := runMisspell (lintCtx , pass , replacer )
43
+ issues , err := runMisspell (lintCtx , pass , replacer , settings . Mode )
44
44
if err != nil {
45
45
return nil , err
46
46
}
@@ -60,15 +60,16 @@ func NewMisspell(settings *config.MisspellSettings) *goanalysis.Linter {
60
60
}).WithLoadMode (goanalysis .LoadModeSyntax )
61
61
}
62
62
63
- func runMisspell (lintCtx * linter.Context , pass * analysis.Pass , replacer * misspell.Replacer ) ([]goanalysis.Issue , error ) {
63
+ func runMisspell (lintCtx * linter.Context , pass * analysis.Pass , replacer * misspell.Replacer , mode string ) ([]goanalysis.Issue , error ) {
64
64
fileNames := getFileNames (pass )
65
65
66
66
var issues []goanalysis.Issue
67
67
for _ , filename := range fileNames {
68
- lintIssues , err := runMisspellOnFile (lintCtx , filename , replacer )
68
+ lintIssues , err := runMisspellOnFile (lintCtx , filename , replacer , mode )
69
69
if err != nil {
70
70
return nil , err
71
71
}
72
+
72
73
for i := range lintIssues {
73
74
issues = append (issues , goanalysis .NewIssue (& lintIssues [i ], pass ))
74
75
}
@@ -104,25 +105,36 @@ func createMisspellReplacer(settings *config.MisspellSettings) (*misspell.Replac
104
105
return replacer , nil
105
106
}
106
107
107
- func runMisspellOnFile (lintCtx * linter.Context , filename string , replacer * misspell.Replacer ) ([]result.Issue , error ) {
108
- var res []result.Issue
108
+ func runMisspellOnFile (lintCtx * linter.Context , filename string , replacer * misspell.Replacer , mode string ) ([]result.Issue , error ) {
109
109
fileContent , err := lintCtx .FileCache .GetFileBytes (filename )
110
110
if err != nil {
111
111
return nil , fmt .Errorf ("can't get file %s contents: %s" , filename , err )
112
112
}
113
113
114
- // use r.Replace, not r.ReplaceGo because r.ReplaceGo doesn't find
115
- // issues inside strings: it searches only inside comments. r.Replace
116
- // searches all words: it treats input as a plain text. A standalone misspell
117
- // tool uses r.Replace by default.
118
- _ , diffs := replacer .Replace (string (fileContent ))
114
+ // `r.ReplaceGo` doesn't find issues inside strings: it searches only inside comments.
115
+ // `r.Replace` searches all words: it treats input as a plain text.
116
+ // The standalone misspell tool uses `r.Replace` by default.
117
+ var replace func (input string ) (string , []misspell.Diff )
118
+ switch strings .ToLower (mode ) {
119
+ case "restricted" :
120
+ replace = replacer .ReplaceGo
121
+ default :
122
+ replace = replacer .Replace
123
+ }
124
+
125
+ _ , diffs := replace (string (fileContent ))
126
+
127
+ var res []result.Issue
128
+
119
129
for _ , diff := range diffs {
120
130
text := fmt .Sprintf ("`%s` is a misspelling of `%s`" , diff .Original , diff .Corrected )
131
+
121
132
pos := token.Position {
122
133
Filename : filename ,
123
134
Line : diff .Line ,
124
135
Column : diff .Column + 1 ,
125
136
}
137
+
126
138
replacement := & result.Replacement {
127
139
Inline : & result.InlineFix {
128
140
StartCol : diff .Column ,
0 commit comments