Skip to content

Commit fd4d8cf

Browse files
wagnerd3Abirdcfly
authored andcommitted
check excludeWords without commas
1 parent d1376bb commit fd4d8cf

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

dupword.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ func CheckOneKey(raw, key string) (new string, findWord string, find bool) {
249249
*/
250250
symbol := raw[spaceStart:i]
251251
if ((key != "" && curWord == key) || key == "") && curWord == preWord && curWord != "" {
252-
if !ExcludeWords(curWord) {
252+
if !ExcludeWords(cutTrailingCommas(curWord)) {
253253
find = true
254254
findWordMap[curWord] = true
255255
newLine.WriteString(lastSpace)
@@ -270,7 +270,7 @@ func CheckOneKey(raw, key string) (new string, findWord string, find bool) {
270270
// last position
271271
word := raw[wordStart:]
272272
if ((key != "" && word == key) || key == "") && word == preWord {
273-
if !ExcludeWords(word) {
273+
if !ExcludeWords(cutTrailingCommas(word)) {
274274
find = true
275275
findWordMap[word] = true
276276
}
@@ -341,3 +341,11 @@ func isExampleOutputStart(comment string) bool {
341341
strings.HasPrefix(comment, "// Unordered output:") ||
342342
strings.HasPrefix(comment, "// unordered output:")
343343
}
344+
345+
// cutTrailingCommas is used to remove trailing commas of words.
346+
// The excludeWords are provided as comma-separated list, so it is
347+
// impossible to ignore "[word], [word]," matches otherwise
348+
func cutTrailingCommas(s string) string {
349+
result, _ := strings.CutSuffix(s, ",")
350+
return result
351+
}

dupword_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ func Test(t *testing.T) {
4242
analyzer2 := dupword.NewAnalyzer()
4343
_ = analyzer.Flags.Set("ignore", "the,and")
4444
analysistest.Run(t, analysistest.TestData(), analyzer2, "a_ignore_the_and")
45+
46+
analyzer3 := dupword.NewAnalyzer()
47+
_ = analyzer3.Flags.Set("ignore", "anything")
48+
analysistest.Run(t, analysistest.TestData(), analyzer3, "ignore_commas")
4549
}
4650

4751
func Test_checkOneKey(t *testing.T) {

testdata/src/ignore_commas/a.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// MIT License
2+
//
3+
// Copyright (c) 2022 Abirdcfly
4+
//
5+
// Permission is hereby granted, free of charge, to any person obtaining a copy
6+
// of this software and associated documentation files (the "Software"), to deal
7+
// in the Software without restriction, including without limitation the rights
8+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
// copies of the Software, and to permit persons to whom the Software is
10+
// furnished to do so, subject to the following conditions:
11+
//
12+
// The above copyright notice and this permission notice shall be included in all
13+
// copies or substantial portions of the Software.
14+
//
15+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
// SOFTWARE.
22+
23+
package a
24+
25+
import "fmt"
26+
27+
func A() {
28+
line := "this line contains some duplicate words with comma to ignore: anything, anything,"
29+
fmt.Println(line)
30+
}

0 commit comments

Comments
 (0)