Skip to content

Commit

Permalink
misspell: add extra-words
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Feb 19, 2024
1 parent 64492b5 commit c64e6a7
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
7 changes: 7 additions & 0 deletions .golangci.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1364,6 +1364,13 @@ linters-settings:
# Default: []
ignore-words:
- someword
# Extra word corrections.
# Default: []
extra-words:
- typo: "iff"
correction: "if"
- typo: "cancelation"
correction: "cancellation"
# Mode of the analysis:
# - default: checks all the file content.
# - restricted: checks only comments.
Expand Down
10 changes: 8 additions & 2 deletions pkg/config/linters_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -663,12 +663,18 @@ type MalignedSettings struct {
}

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

type MisspellExtraWords struct {
Typo string
Correction string
}

type MustTagSettings struct {
Functions []struct {
Name string `mapstructure:"name"`
Expand Down
27 changes: 27 additions & 0 deletions pkg/golinters/misspell.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"go/token"
"strings"
"sync"
"unicode"

"github.com/golangci/misspell"
"golang.org/x/tools/go/analysis"
Expand Down Expand Up @@ -95,6 +96,11 @@ func createMisspellReplacer(settings *config.MisspellSettings) (*misspell.Replac
return nil, fmt.Errorf("unknown locale: %q", settings.Locale)
}

err := appendExtraWords(replacer, settings.ExtraWords)
if err != nil {
return nil, err
}

if len(settings.IgnoreWords) != 0 {
replacer.RemoveRule(settings.IgnoreWords)
}
Expand Down Expand Up @@ -153,3 +159,24 @@ func runMisspellOnFile(lintCtx *linter.Context, filename string, replacer *missp

return res, nil
}

func appendExtraWords(replacer *misspell.Replacer, extraWords []config.MisspellExtraWords) error {
var extra []string

for _, word := range extraWords {
if word.Typo == "" || strings.ContainsFunc(word.Typo, func(r rune) bool { return !unicode.IsLetter(r) }) {
return fmt.Errorf("the word in the 'typo' field should only contain letters")
}
if word.Correction == "" || strings.ContainsFunc(word.Correction, func(r rune) bool { return !unicode.IsLetter(r) }) {
return fmt.Errorf("the word in the 'correction' field should only contain letters")
}

extra = append(extra, strings.ToLower(word.Typo), strings.ToLower(word.Correction))
}

if len(extra) > 0 {
replacer.AddRuleList(extra)
}

return nil
}

0 comments on commit c64e6a7

Please sign in to comment.