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 7 commits
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 `mapstructure:"locale"`
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 `mapstructure:"typo"`
Correction string `mapstructure:"correction"`
}

type MustTagSettings struct {
Functions []struct {
Name string `mapstructure:"name"`
Expand Down
33 changes: 33 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, fmt.Errorf("process extra words: %w", err)
}

if len(settings.IgnoreWords) != 0 {
replacer.RemoveRule(settings.IgnoreWords)
}
Expand Down Expand Up @@ -153,3 +159,30 @@ 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
if len(extraWords) == 0 {
return nil
}

extra := make([]string, 0, len(extraWords)*2)

for _, word := range extraWords {
if word.Typo == "" || word.Correction == "" {
return fmt.Errorf("typo (%q) and correction (%q) fields should not be empty", word.Typo, word.Correction)
}

if strings.ContainsFunc(word.Typo, func(r rune) bool { return !unicode.IsLetter(r) }) {
return fmt.Errorf("the word %q in the 'typo' field should only contain letters", word.Typo)
}
if strings.ContainsFunc(word.Correction, func(r rune) bool { return !unicode.IsLetter(r) }) {
return fmt.Errorf("the word %q in the 'correction' field should only contain letters", word.Correction)
}

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

replacer.AddRuleList(extra)

return nil
}
94 changes: 94 additions & 0 deletions pkg/golinters/misspell_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package golinters

import (
"testing"

"github.com/golangci/misspell"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/golangci/golangci-lint/pkg/config"
)

func Test_appendExtraWords(t *testing.T) {
extraWords := []config.MisspellExtraWords{
{
Typo: "iff",
Correction: "if",
},
{
Typo: "cancelation",
Correction: "cancellation",
},
}
ldez marked this conversation as resolved.
Show resolved Hide resolved

replacer := &misspell.Replacer{}

err := appendExtraWords(replacer, extraWords)
require.NoError(t, err)

expected := []string{"iff", "if", "cancelation", "cancellation"}

assert.Equal(t, replacer.Replacements, expected)
}

func Test_appendExtraWords_error(t *testing.T) {
testCases := []struct {
desc string
extraWords []config.MisspellExtraWords
expected string
}{
{
desc: "empty fields",
extraWords: []config.MisspellExtraWords{{
Typo: "",
Correction: "",
}},
expected: `typo ("") and correction ("") fields should not be empty`,
},
{
desc: "empty typo",
extraWords: []config.MisspellExtraWords{{
Typo: "",
Correction: "if",
}},
expected: `typo ("") and correction ("if") fields should not be empty`,
},
{
desc: "empty correction",
extraWords: []config.MisspellExtraWords{{
Typo: "iff",
Correction: "",
}},
expected: `typo ("iff") and correction ("") fields should not be empty`,
},
{
desc: "invalid characters in typo",
extraWords: []config.MisspellExtraWords{{
Typo: "i'ff",
Correction: "if",
}},
expected: `the word "i'ff" in the 'typo' field should only contain letters`,
},
{
desc: "invalid characters in correction",
extraWords: []config.MisspellExtraWords{{
Typo: "iff",
Correction: "i'f",
}},
expected: `the word "i'f" in the 'correction' field should only contain letters`,
},
}

for _, test := range testCases {
test := test
t.Run(test.desc, func(t *testing.T) {
t.Parallel()

replacer := &misspell.Replacer{}

err := appendExtraWords(replacer, test.extraWords)
require.EqualError(t, err, test.expected)
})
}
}
7 changes: 7 additions & 0 deletions test/testdata/configs/misspell_custom.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
linters-settings:
misspell:
extra-words:
- typo: "iff"
ldez marked this conversation as resolved.
Show resolved Hide resolved
correction: "if"
- typo: "cancelation"
correction: "cancellation"
10 changes: 10 additions & 0 deletions test/testdata/misspell_custom.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//golangcitest:args -Emisspell
ldez marked this conversation as resolved.
Show resolved Hide resolved
//golangcitest:config_path testdata/configs/misspell_custom.yml
package testdata

func Misspell() {
// comment with incorrect spelling: occured // want "`occured` is a misspelling of `occurred`"
}

// the word iff should be reported here // want "\\`iff\\` is a misspelling of \\`if\\`"
// the word cancelation should be reported here // want "\\`cancelation\\` is a misspelling of \\`cancellation\\`"
Loading