Skip to content

Commit

Permalink
feat: customize ignore keywords
Browse files Browse the repository at this point in the history
Signed-off-by: Abirdcfly <[email protected]>
  • Loading branch information
Abirdcfly committed Sep 22, 2023
1 parent 185b5a6 commit d06e0fc
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 3 deletions.
28 changes: 26 additions & 2 deletions dupword.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ This analyzer checks miswritten duplicate words in comments or package doc or st
var (
defaultWord = []string{}
// defaultWord = []string{"the", "and", "a"}
ignoreWord = map[string]bool{}
)

type analyzer struct {
Expand All @@ -70,7 +71,26 @@ func (a *analyzer) Set(w string) error {
return nil
}

type ignore struct {
}

func (a *ignore) String() string {
t := make([]string,0, len(ignoreWord))
for k := range ignoreWord {
t = append(t, k)
}
return strings.Join(t, ",")
}

func (a *ignore) Set(w string) error {
for _, k := range strings.Split(w, ","){
ignoreWord[k] = true
}
return nil
}

func NewAnalyzer() *analysis.Analyzer {
ignore := &ignore{}
analyzer := &analyzer{KeyWord: defaultWord}
a := &analysis.Analyzer{
Name: Name,
Expand All @@ -80,7 +100,8 @@ func NewAnalyzer() *analysis.Analyzer {
RunDespiteErrors: true,
}
a.Flags.Init(Name, flag.ExitOnError)
a.Flags.Var(analyzer, "keyword", "key words for detecting duplicate words")
a.Flags.Var(analyzer, "keyword", "keywords for detecting duplicate words")
a.Flags.Var(ignore, "ignore", "ignore words")
a.Flags.Var(version{}, "V", "print version and exit")
return a
}
Expand Down Expand Up @@ -176,7 +197,7 @@ func (a *analyzer) fixDuplicateWordInString(pass *analysis.Pass, lit *ast.BasicL
}
}

// CheckOneKey use to check there is defined duplicate word in a string.
// CheckOneKey use to check there is a defined duplicate word in a string.
// raw is checked line. key is the keyword to check. empty means just check duplicate word.
func CheckOneKey(raw, key string) (new string, findWord string, find bool) {
if key == "" {
Expand Down Expand Up @@ -298,5 +319,8 @@ func ExcludeWords(word string) (exclude bool) {
if unicode.IsSymbol(firstRune) {
return true
}
if _, exist := ignoreWord[word]; exist {
return true
}
return false
}
10 changes: 9 additions & 1 deletion dupword_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ func Test(t *testing.T) {
analyzer := dupword.NewAnalyzer()
tests := []string{"a", "good"}
analysistest.Run(t, analysistest.TestData(), analyzer, tests...)

analyzer1 := dupword.NewAnalyzer()
analyzer1.Flags.Set("ignore", "the")

Check failure on line 38 in dupword_test.go

View workflow job for this annotation

GitHub Actions / build

Error return value of `analyzer1.Flags.Set` is not checked (errcheck)
analysistest.Run(t, analysistest.TestData(), analyzer1, "a_ignore_the")

analyzer2 := dupword.NewAnalyzer()
analyzer.Flags.Set("ignore", "the,and")

Check failure on line 42 in dupword_test.go

View workflow job for this annotation

GitHub Actions / build

Error return value of `analyzer.Flags.Set` is not checked (errcheck)
analysistest.Run(t, analysistest.TestData(), analyzer2, "a_ignore_the_and")
}

func Test_checkOneKey(t *testing.T) {
Expand Down Expand Up @@ -187,7 +195,7 @@ func TestExcludeWords(t *testing.T) {
wantExclude bool
}{
{
name: "normal word should not exclude",
name: "normal words should not exclude",
args: args{word: "normal"},
wantExclude: false,
},
Expand Down
37 changes: 37 additions & 0 deletions testdata/src/a_ignore_the/a.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// MIT License
//
// Copyright (c) 2022 Abirdcfly
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

package a

import "fmt"

func A() {
// print the the line
fmt.Println("hello")
// print the line
fmt.Println("word")
line := "print the\n the line, and and print the the \n\t the line." // want `Duplicate words \(and\) found`
fmt.Println(line)
// so so this file done. // want `Duplicate words \(so\) found`
// 中文重复复单词无法检测,因为中文不使用空格来分割字符,我们都是用肉眼看的,哈哈哈哈。
// 除 非 写 成 这 样 样 子 ! // want `Duplicate words \(样\) found`
}
40 changes: 40 additions & 0 deletions testdata/src/a_ignore_the/a_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// MIT License
//
// Copyright (c) 2022 Abirdcfly
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

package a

import (
"testing"
)

func TestA(t *testing.T) {
tests := []struct {
name string
}{
// TODO: Add the the test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
A()
})
}
}
2 changes: 2 additions & 0 deletions testdata/src/a_ignore_the/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package a this comment include duplicated word and and // want `Duplicate words \(and\) found`
package a
37 changes: 37 additions & 0 deletions testdata/src/a_ignore_the_and/a.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// MIT License
//
// Copyright (c) 2022 Abirdcfly
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

package a

import "fmt"

func A() {
// print the the line
fmt.Println("hello")
// print the line
fmt.Println("word")
line := "print the\n the line, and and print the the \n\t the line."
fmt.Println(line)
// so so this file done. // want `Duplicate words \(so\) found`
// 中文重复复单词无法检测,因为中文不使用空格来分割字符,我们都是用肉眼看的,哈哈哈哈。
// 除 非 写 成 这 样 样 子 ! // want `Duplicate words \(样\) found`
}
40 changes: 40 additions & 0 deletions testdata/src/a_ignore_the_and/a_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// MIT License
//
// Copyright (c) 2022 Abirdcfly
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

package a

import (
"testing"
)

func TestA(t *testing.T) {
tests := []struct {
name string
}{
// TODO: Add the the test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
A()
})
}
}
2 changes: 2 additions & 0 deletions testdata/src/a_ignore_the_and/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package a this comment include duplicated word and and
package a

0 comments on commit d06e0fc

Please sign in to comment.