Skip to content

Commit c64e6a7

Browse files
committed
misspell: add extra-words
1 parent 64492b5 commit c64e6a7

File tree

3 files changed

+42
-2
lines changed

3 files changed

+42
-2
lines changed

.golangci.reference.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,6 +1364,13 @@ linters-settings:
13641364
# Default: []
13651365
ignore-words:
13661366
- someword
1367+
# Extra word corrections.
1368+
# Default: []
1369+
extra-words:
1370+
- typo: "iff"
1371+
correction: "if"
1372+
- typo: "cancelation"
1373+
correction: "cancellation"
13671374
# Mode of the analysis:
13681375
# - default: checks all the file content.
13691376
# - restricted: checks only comments.

pkg/config/linters_settings.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -663,12 +663,18 @@ type MalignedSettings struct {
663663
}
664664

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

673+
type MisspellExtraWords struct {
674+
Typo string
675+
Correction string
676+
}
677+
672678
type MustTagSettings struct {
673679
Functions []struct {
674680
Name string `mapstructure:"name"`

pkg/golinters/misspell.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"go/token"
66
"strings"
77
"sync"
8+
"unicode"
89

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

99+
err := appendExtraWords(replacer, settings.ExtraWords)
100+
if err != nil {
101+
return nil, err
102+
}
103+
98104
if len(settings.IgnoreWords) != 0 {
99105
replacer.RemoveRule(settings.IgnoreWords)
100106
}
@@ -153,3 +159,24 @@ func runMisspellOnFile(lintCtx *linter.Context, filename string, replacer *missp
153159

154160
return res, nil
155161
}
162+
163+
func appendExtraWords(replacer *misspell.Replacer, extraWords []config.MisspellExtraWords) error {
164+
var extra []string
165+
166+
for _, word := range extraWords {
167+
if word.Typo == "" || strings.ContainsFunc(word.Typo, func(r rune) bool { return !unicode.IsLetter(r) }) {
168+
return fmt.Errorf("the word in the 'typo' field should only contain letters")
169+
}
170+
if word.Correction == "" || strings.ContainsFunc(word.Correction, func(r rune) bool { return !unicode.IsLetter(r) }) {
171+
return fmt.Errorf("the word in the 'correction' field should only contain letters")
172+
}
173+
174+
extra = append(extra, strings.ToLower(word.Typo), strings.ToLower(word.Correction))
175+
}
176+
177+
if len(extra) > 0 {
178+
replacer.AddRuleList(extra)
179+
}
180+
181+
return nil
182+
}

0 commit comments

Comments
 (0)