From f25d03b09694376dfb2f00b383c045367a702872 Mon Sep 17 00:00:00 2001 From: Oleksandr Redko Date: Fri, 21 Jun 2024 14:15:04 +0300 Subject: [PATCH] misspell: support multiple correction words --- .golangci.next.reference.yml | 5 ++++- pkg/golinters/misspell/misspell.go | 9 ++++++--- pkg/golinters/misspell/misspell_test.go | 6 +++++- pkg/golinters/misspell/testdata/fix/in/misspell.go | 2 ++ pkg/golinters/misspell/testdata/fix/out/misspell.go | 2 ++ pkg/golinters/misspell/testdata/misspell_custom.go | 1 + pkg/golinters/misspell/testdata/misspell_custom.yml | 2 ++ pkg/golinters/misspell/testdata/misspell_fix.yml | 5 +++++ 8 files changed, 27 insertions(+), 5 deletions(-) create mode 100644 pkg/golinters/misspell/testdata/misspell_fix.yml diff --git a/.golangci.next.reference.yml b/.golangci.next.reference.yml index f82b65bfc790..8facc4ab1021 100644 --- a/.golangci.next.reference.yml +++ b/.golangci.next.reference.yml @@ -1321,7 +1321,8 @@ linters-settings: ignore-words: - someword # Extra word corrections. - # `typo` and `correction` should only contain letters. + # `typo` should only contain letters. + # `correction` can contains one or more words separated by commas without spaces. # The words are case-insensitive. # Default: [] extra-words: @@ -1329,6 +1330,8 @@ linters-settings: correction: "if" - typo: "cancelation" correction: "cancellation" + - typo: "successed" + correction: "successful,success,succeeded" # Mode of the analysis: # - default: checks all the file content. # - restricted: checks only comments. diff --git a/pkg/golinters/misspell/misspell.go b/pkg/golinters/misspell/misspell.go index 44409cec9dee..9761e91b864f 100644 --- a/pkg/golinters/misspell/misspell.go +++ b/pkg/golinters/misspell/misspell.go @@ -134,7 +134,10 @@ func runMisspellOnFile(lintCtx *linter.Context, filename string, replacer *missp var res []result.Issue for _, diff := range diffs { - text := fmt.Sprintf("`%s` is a misspelling of `%s`", diff.Original, diff.Corrected) + allCorrections := diff.Corrected + text := fmt.Sprintf("`%s` is a misspelling of `%s`", diff.Original, allCorrections) + // The first suggestion is the most likely to be correct. + correction := strings.Split(allCorrections, ",")[0] pos := token.Position{ Filename: filename, @@ -146,7 +149,7 @@ func runMisspellOnFile(lintCtx *linter.Context, filename string, replacer *missp Inline: &result.InlineFix{ StartCol: diff.Column, Length: len(diff.Original), - NewString: diff.Corrected, + NewString: correction, }, } @@ -176,7 +179,7 @@ func appendExtraWords(replacer *misspell.Replacer, extraWords []config.MisspellE 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) }) { + if strings.ContainsFunc(word.Correction, func(r rune) bool { return r != ',' && !unicode.IsLetter(r) }) { return fmt.Errorf("the word %q in the 'correction' field should only contain letters", word.Correction) } diff --git a/pkg/golinters/misspell/misspell_test.go b/pkg/golinters/misspell/misspell_test.go index 3766d1799224..c38d5de7cee7 100644 --- a/pkg/golinters/misspell/misspell_test.go +++ b/pkg/golinters/misspell/misspell_test.go @@ -20,6 +20,10 @@ func Test_appendExtraWords(t *testing.T) { Typo: "canCELation", Correction: "canceLLaTION", }, + { + Typo: "successed", + Correction: "successful,success,succeeded", + }, } replacer := &misspell.Replacer{} @@ -27,7 +31,7 @@ func Test_appendExtraWords(t *testing.T) { err := appendExtraWords(replacer, extraWords) require.NoError(t, err) - expected := []string{"iff", "if", "cancelation", "cancellation"} + expected := []string{"iff", "if", "cancelation", "cancellation", "successed", "successful,success,succeeded"} assert.Equal(t, expected, replacer.Replacements) } diff --git a/pkg/golinters/misspell/testdata/fix/in/misspell.go b/pkg/golinters/misspell/testdata/fix/in/misspell.go index 07d0ae5b241a..599112737c23 100644 --- a/pkg/golinters/misspell/testdata/fix/in/misspell.go +++ b/pkg/golinters/misspell/testdata/fix/in/misspell.go @@ -1,4 +1,5 @@ //golangcitest:args -Emisspell +//golangcitest:config_path testdata/misspell_fix.yml //golangcitest:expected_exitcode 0 package p @@ -8,6 +9,7 @@ import "log" // lala langauge // langauge // langauge langauge +// successed // check Langauge // and check langAuge diff --git a/pkg/golinters/misspell/testdata/fix/out/misspell.go b/pkg/golinters/misspell/testdata/fix/out/misspell.go index 474dd3e0de66..a52fa46c00c6 100644 --- a/pkg/golinters/misspell/testdata/fix/out/misspell.go +++ b/pkg/golinters/misspell/testdata/fix/out/misspell.go @@ -1,4 +1,5 @@ //golangcitest:args -Emisspell +//golangcitest:config_path testdata/misspell_fix.yml //golangcitest:expected_exitcode 0 package p @@ -8,6 +9,7 @@ import "log" // lala language // language // language language +// successful // check Language // and check langAuge diff --git a/pkg/golinters/misspell/testdata/misspell_custom.go b/pkg/golinters/misspell/testdata/misspell_custom.go index d1b294dd2322..afec7ebc864c 100644 --- a/pkg/golinters/misspell/testdata/misspell_custom.go +++ b/pkg/golinters/misspell/testdata/misspell_custom.go @@ -8,3 +8,4 @@ func Misspell() { // 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\\`" +// the word successed should be reported here // want "\\`successed\\` is a misspelling of \\`successful,success,succeeded\\`" diff --git a/pkg/golinters/misspell/testdata/misspell_custom.yml b/pkg/golinters/misspell/testdata/misspell_custom.yml index c0f3a27cfddb..62d83a93ca1b 100644 --- a/pkg/golinters/misspell/testdata/misspell_custom.yml +++ b/pkg/golinters/misspell/testdata/misspell_custom.yml @@ -5,3 +5,5 @@ linters-settings: correction: "if" - typo: "cancelation" correction: "cancellation" + - typo: "successed" + correction: "successful,success,succeeded" diff --git a/pkg/golinters/misspell/testdata/misspell_fix.yml b/pkg/golinters/misspell/testdata/misspell_fix.yml new file mode 100644 index 000000000000..5d664f8da894 --- /dev/null +++ b/pkg/golinters/misspell/testdata/misspell_fix.yml @@ -0,0 +1,5 @@ +linters-settings: + misspell: + extra-words: + - typo: "successed" + correction: "successful,success,succeeded"