Skip to content

Commit

Permalink
misspell: support multiple correction words
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandear committed Jun 21, 2024
1 parent dbd0935 commit f25d03b
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 5 deletions.
5 changes: 4 additions & 1 deletion .golangci.next.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1321,14 +1321,17 @@ 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:
- typo: "iff"
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.
Expand Down
9 changes: 6 additions & 3 deletions pkg/golinters/misspell/misspell.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
},
}

Expand Down Expand Up @@ -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)
}

Expand Down
6 changes: 5 additions & 1 deletion pkg/golinters/misspell/misspell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@ func Test_appendExtraWords(t *testing.T) {
Typo: "canCELation",
Correction: "canceLLaTION",
},
{
Typo: "successed",
Correction: "successful,success,succeeded",
},
}

replacer := &misspell.Replacer{}

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)
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/golinters/misspell/testdata/fix/in/misspell.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//golangcitest:args -Emisspell
//golangcitest:config_path testdata/misspell_fix.yml
//golangcitest:expected_exitcode 0
package p

Expand All @@ -8,6 +9,7 @@ import "log"
// lala langauge
// langauge
// langauge langauge
// successed

// check Langauge
// and check langAuge
Expand Down
2 changes: 2 additions & 0 deletions pkg/golinters/misspell/testdata/fix/out/misspell.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//golangcitest:args -Emisspell
//golangcitest:config_path testdata/misspell_fix.yml
//golangcitest:expected_exitcode 0
package p

Expand All @@ -8,6 +9,7 @@ import "log"
// lala language
// language
// language language
// successful

// check Language
// and check langAuge
Expand Down
1 change: 1 addition & 0 deletions pkg/golinters/misspell/testdata/misspell_custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -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\\`"
2 changes: 2 additions & 0 deletions pkg/golinters/misspell/testdata/misspell_custom.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ linters-settings:
correction: "if"
- typo: "cancelation"
correction: "cancellation"
- typo: "successed"
correction: "successful,success,succeeded"
5 changes: 5 additions & 0 deletions pkg/golinters/misspell/testdata/misspell_fix.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
linters-settings:
misspell:
extra-words:
- typo: "successed"
correction: "successful,success,succeeded"

0 comments on commit f25d03b

Please sign in to comment.