Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

misspell: add extra-words #4401

Merged
merged 8 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .golangci.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1364,6 +1364,14 @@ linters-settings:
# Default: []
ignore-words:
- someword
# Extra word corrections.
# `typo` and `correction` should only contain letters.
# 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
ldez marked this conversation as resolved.
Show resolved Hide resolved
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
ldez marked this conversation as resolved.
Show resolved Hide resolved
Correction string
ldez marked this conversation as resolved.
Show resolved Hide resolved
}

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
ldez marked this conversation as resolved.
Show resolved Hide resolved
}

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 {
ldez marked this conversation as resolved.
Show resolved Hide resolved
var extra []string
ldez marked this conversation as resolved.
Show resolved Hide resolved

for _, word := range extraWords {
if word.Typo == "" || strings.ContainsFunc(word.Typo, func(r rune) bool { return !unicode.IsLetter(r) }) {
ldez marked this conversation as resolved.
Show resolved Hide resolved
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) }) {
ldez marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Errorf("the word in the 'correction' field should only contain letters")
}

extra = append(extra, strings.ToLower(word.Typo), strings.ToLower(word.Correction))
ldez marked this conversation as resolved.
Show resolved Hide resolved
}

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

return nil
}
Loading