@@ -29,7 +29,7 @@ func NewMisspell(settings *config.MisspellSettings) *goanalysis.Linter {
2929
3030 return goanalysis .NewLinter (
3131 misspellName ,
32- "Finds commonly misspelled English words in comments " ,
32+ "Finds commonly misspelled English words" ,
3333 []* analysis.Analyzer {analyzer },
3434 nil ,
3535 ).WithContextSetter (func (lintCtx * linter.Context ) {
@@ -40,7 +40,7 @@ func NewMisspell(settings *config.MisspellSettings) *goanalysis.Linter {
4040 return nil , ruleErr
4141 }
4242
43- issues , err := runMisspell (lintCtx , pass , replacer )
43+ issues , err := runMisspell (lintCtx , pass , replacer , settings . Mode )
4444 if err != nil {
4545 return nil , err
4646 }
@@ -60,15 +60,16 @@ func NewMisspell(settings *config.MisspellSettings) *goanalysis.Linter {
6060 }).WithLoadMode (goanalysis .LoadModeSyntax )
6161}
6262
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 ) {
6464 fileNames := getFileNames (pass )
6565
6666 var issues []goanalysis.Issue
6767 for _ , filename := range fileNames {
68- lintIssues , err := runMisspellOnFile (lintCtx , filename , replacer )
68+ lintIssues , err := runMisspellOnFile (lintCtx , filename , replacer , mode )
6969 if err != nil {
7070 return nil , err
7171 }
72+
7273 for i := range lintIssues {
7374 issues = append (issues , goanalysis .NewIssue (& lintIssues [i ], pass ))
7475 }
@@ -104,25 +105,36 @@ func createMisspellReplacer(settings *config.MisspellSettings) (*misspell.Replac
104105 return replacer , nil
105106}
106107
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 ) {
109109 fileContent , err := lintCtx .FileCache .GetFileBytes (filename )
110110 if err != nil {
111111 return nil , fmt .Errorf ("can't get file %s contents: %s" , filename , err )
112112 }
113113
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+
119129 for _ , diff := range diffs {
120130 text := fmt .Sprintf ("`%s` is a misspelling of `%s`" , diff .Original , diff .Corrected )
131+
121132 pos := token.Position {
122133 Filename : filename ,
123134 Line : diff .Line ,
124135 Column : diff .Column + 1 ,
125136 }
137+
126138 replacement := & result.Replacement {
127139 Inline : & result.InlineFix {
128140 StartCol : diff .Column ,
0 commit comments