Skip to content

Commit d99c02c

Browse files
authored
misspell: add mode option (#4275)
1 parent 95bc7a8 commit d99c02c

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

Diff for: .golangci.reference.yml

+5
Original file line numberDiff line numberDiff line change
@@ -1333,6 +1333,11 @@ linters-settings:
13331333
# Default: []
13341334
ignore-words:
13351335
- someword
1336+
# Mode of the analysis:
1337+
# - default: checks all the file content.
1338+
# - restricted: checks only comments.
1339+
# Default: ""
1340+
mode: restricted
13361341

13371342
musttag:
13381343
# A set of custom functions to check in addition to the builtin ones.

Diff for: pkg/config/linters_settings.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -659,8 +659,9 @@ type MalignedSettings struct {
659659
}
660660

661661
type MisspellSettings struct {
662+
Mode string `mapstructure:"mode"`
662663
Locale string
663-
// TODO(ldez): v2 the options must be renamed to `IgnoredRules`.
664+
// TODO(ldez): v2 the option must be renamed to `IgnoredRules`.
664665
IgnoreWords []string `mapstructure:"ignore-words"`
665666
}
666667

Diff for: pkg/golinters/misspell.go

+23-11
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)