Skip to content

Commit f25d03b

Browse files
committed
misspell: support multiple correction words
1 parent dbd0935 commit f25d03b

File tree

8 files changed

+27
-5
lines changed

8 files changed

+27
-5
lines changed

.golangci.next.reference.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1321,14 +1321,17 @@ linters-settings:
13211321
ignore-words:
13221322
- someword
13231323
# Extra word corrections.
1324-
# `typo` and `correction` should only contain letters.
1324+
# `typo` should only contain letters.
1325+
# `correction` can contains one or more words separated by commas without spaces.
13251326
# The words are case-insensitive.
13261327
# Default: []
13271328
extra-words:
13281329
- typo: "iff"
13291330
correction: "if"
13301331
- typo: "cancelation"
13311332
correction: "cancellation"
1333+
- typo: "successed"
1334+
correction: "successful,success,succeeded"
13321335
# Mode of the analysis:
13331336
# - default: checks all the file content.
13341337
# - restricted: checks only comments.

pkg/golinters/misspell/misspell.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,10 @@ func runMisspellOnFile(lintCtx *linter.Context, filename string, replacer *missp
134134
var res []result.Issue
135135

136136
for _, diff := range diffs {
137-
text := fmt.Sprintf("`%s` is a misspelling of `%s`", diff.Original, diff.Corrected)
137+
allCorrections := diff.Corrected
138+
text := fmt.Sprintf("`%s` is a misspelling of `%s`", diff.Original, allCorrections)
139+
// The first suggestion is the most likely to be correct.
140+
correction := strings.Split(allCorrections, ",")[0]
138141

139142
pos := token.Position{
140143
Filename: filename,
@@ -146,7 +149,7 @@ func runMisspellOnFile(lintCtx *linter.Context, filename string, replacer *missp
146149
Inline: &result.InlineFix{
147150
StartCol: diff.Column,
148151
Length: len(diff.Original),
149-
NewString: diff.Corrected,
152+
NewString: correction,
150153
},
151154
}
152155

@@ -176,7 +179,7 @@ func appendExtraWords(replacer *misspell.Replacer, extraWords []config.MisspellE
176179
if strings.ContainsFunc(word.Typo, func(r rune) bool { return !unicode.IsLetter(r) }) {
177180
return fmt.Errorf("the word %q in the 'typo' field should only contain letters", word.Typo)
178181
}
179-
if strings.ContainsFunc(word.Correction, func(r rune) bool { return !unicode.IsLetter(r) }) {
182+
if strings.ContainsFunc(word.Correction, func(r rune) bool { return r != ',' && !unicode.IsLetter(r) }) {
180183
return fmt.Errorf("the word %q in the 'correction' field should only contain letters", word.Correction)
181184
}
182185

pkg/golinters/misspell/misspell_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,18 @@ func Test_appendExtraWords(t *testing.T) {
2020
Typo: "canCELation",
2121
Correction: "canceLLaTION",
2222
},
23+
{
24+
Typo: "successed",
25+
Correction: "successful,success,succeeded",
26+
},
2327
}
2428

2529
replacer := &misspell.Replacer{}
2630

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

30-
expected := []string{"iff", "if", "cancelation", "cancellation"}
34+
expected := []string{"iff", "if", "cancelation", "cancellation", "successed", "successful,success,succeeded"}
3135

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

pkg/golinters/misspell/testdata/fix/in/misspell.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//golangcitest:args -Emisspell
2+
//golangcitest:config_path testdata/misspell_fix.yml
23
//golangcitest:expected_exitcode 0
34
package p
45

@@ -8,6 +9,7 @@ import "log"
89
// lala langauge
910
// langauge
1011
// langauge langauge
12+
// successed
1113

1214
// check Langauge
1315
// and check langAuge

pkg/golinters/misspell/testdata/fix/out/misspell.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
//golangcitest:args -Emisspell
2+
//golangcitest:config_path testdata/misspell_fix.yml
23
//golangcitest:expected_exitcode 0
34
package p
45

@@ -8,6 +9,7 @@ import "log"
89
// lala language
910
// language
1011
// language language
12+
// successful
1113

1214
// check Language
1315
// and check langAuge

pkg/golinters/misspell/testdata/misspell_custom.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ func Misspell() {
88

99
// the word iff should be reported here // want "\\`iff\\` is a misspelling of \\`if\\`"
1010
// the word cancelation should be reported here // want "\\`cancelation\\` is a misspelling of \\`cancellation\\`"
11+
// the word successed should be reported here // want "\\`successed\\` is a misspelling of \\`successful,success,succeeded\\`"

pkg/golinters/misspell/testdata/misspell_custom.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ linters-settings:
55
correction: "if"
66
- typo: "cancelation"
77
correction: "cancellation"
8+
- typo: "successed"
9+
correction: "successful,success,succeeded"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
linters-settings:
2+
misspell:
3+
extra-words:
4+
- typo: "successed"
5+
correction: "successful,success,succeeded"

0 commit comments

Comments
 (0)