Skip to content

Commit

Permalink
misspell: add mode option (#4275)
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez authored Dec 22, 2023
1 parent 95bc7a8 commit d99c02c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .golangci.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1333,6 +1333,11 @@ linters-settings:
# Default: []
ignore-words:
- someword
# Mode of the analysis:
# - default: checks all the file content.
# - restricted: checks only comments.
# Default: ""
mode: restricted

musttag:
# A set of custom functions to check in addition to the builtin ones.
Expand Down
3 changes: 2 additions & 1 deletion pkg/config/linters_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,8 +659,9 @@ type MalignedSettings struct {
}

type MisspellSettings struct {
Mode string `mapstructure:"mode"`
Locale string
// TODO(ldez): v2 the options must be renamed to `IgnoredRules`.
// TODO(ldez): v2 the option must be renamed to `IgnoredRules`.
IgnoreWords []string `mapstructure:"ignore-words"`
}

Expand Down
34 changes: 23 additions & 11 deletions pkg/golinters/misspell.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func NewMisspell(settings *config.MisspellSettings) *goanalysis.Linter {

return goanalysis.NewLinter(
misspellName,
"Finds commonly misspelled English words in comments",
"Finds commonly misspelled English words",
[]*analysis.Analyzer{analyzer},
nil,
).WithContextSetter(func(lintCtx *linter.Context) {
Expand All @@ -40,7 +40,7 @@ func NewMisspell(settings *config.MisspellSettings) *goanalysis.Linter {
return nil, ruleErr
}

issues, err := runMisspell(lintCtx, pass, replacer)
issues, err := runMisspell(lintCtx, pass, replacer, settings.Mode)
if err != nil {
return nil, err
}
Expand All @@ -60,15 +60,16 @@ func NewMisspell(settings *config.MisspellSettings) *goanalysis.Linter {
}).WithLoadMode(goanalysis.LoadModeSyntax)
}

func runMisspell(lintCtx *linter.Context, pass *analysis.Pass, replacer *misspell.Replacer) ([]goanalysis.Issue, error) {
func runMisspell(lintCtx *linter.Context, pass *analysis.Pass, replacer *misspell.Replacer, mode string) ([]goanalysis.Issue, error) {
fileNames := getFileNames(pass)

var issues []goanalysis.Issue
for _, filename := range fileNames {
lintIssues, err := runMisspellOnFile(lintCtx, filename, replacer)
lintIssues, err := runMisspellOnFile(lintCtx, filename, replacer, mode)
if err != nil {
return nil, err
}

for i := range lintIssues {
issues = append(issues, goanalysis.NewIssue(&lintIssues[i], pass))
}
Expand Down Expand Up @@ -104,25 +105,36 @@ func createMisspellReplacer(settings *config.MisspellSettings) (*misspell.Replac
return replacer, nil
}

func runMisspellOnFile(lintCtx *linter.Context, filename string, replacer *misspell.Replacer) ([]result.Issue, error) {
var res []result.Issue
func runMisspellOnFile(lintCtx *linter.Context, filename string, replacer *misspell.Replacer, mode string) ([]result.Issue, error) {
fileContent, err := lintCtx.FileCache.GetFileBytes(filename)
if err != nil {
return nil, fmt.Errorf("can't get file %s contents: %s", filename, err)
}

// use r.Replace, not r.ReplaceGo because r.ReplaceGo doesn't find
// issues inside strings: it searches only inside comments. r.Replace
// searches all words: it treats input as a plain text. A standalone misspell
// tool uses r.Replace by default.
_, diffs := replacer.Replace(string(fileContent))
// `r.ReplaceGo` doesn't find issues inside strings: it searches only inside comments.
// `r.Replace` searches all words: it treats input as a plain text.
// The standalone misspell tool uses `r.Replace` by default.
var replace func(input string) (string, []misspell.Diff)
switch strings.ToLower(mode) {
case "restricted":
replace = replacer.ReplaceGo
default:
replace = replacer.Replace
}

_, diffs := replace(string(fileContent))

var res []result.Issue

for _, diff := range diffs {
text := fmt.Sprintf("`%s` is a misspelling of `%s`", diff.Original, diff.Corrected)

pos := token.Position{
Filename: filename,
Line: diff.Line,
Column: diff.Column + 1,
}

replacement := &result.Replacement{
Inline: &result.InlineFix{
StartCol: diff.Column,
Expand Down

0 comments on commit d99c02c

Please sign in to comment.