From 6c57deabb53d41434ae0e6d34e82a706ceb0ce43 Mon Sep 17 00:00:00 2001 From: Abirdcfly Date: Fri, 16 Sep 2022 01:08:52 +0800 Subject: [PATCH] feat: add linter dupword (#3192) --- .golangci.reference.yml | 11 +++++++++++ go.mod | 1 + go.sum | 2 ++ pkg/config/linters_settings.go | 5 +++++ pkg/golinters/dupword.go | 29 +++++++++++++++++++++++++++++ pkg/lint/lintersdb/manager.go | 8 ++++++++ test/testdata/dupword.go | 15 +++++++++++++++ 7 files changed, 71 insertions(+) create mode 100644 pkg/golinters/dupword.go create mode 100644 test/testdata/dupword.go diff --git a/.golangci.reference.yml b/.golangci.reference.yml index 67828a8fe21f..3338c1945846 100644 --- a/.golangci.reference.yml +++ b/.golangci.reference.yml @@ -224,6 +224,15 @@ linters-settings: # Default: 150 threshold: 100 + dupword: + # Keywords for detecting duplicate words. + # If this list is not empty, only the words defined in this list will be detected. + # Default: [] + keywords: + - "the" + - "and" + - "a" + errcheck: # Report about not checking of errors in type assertions: `a := b.(MyStruct)`. # Such cases aren't reported by default. @@ -1979,6 +1988,7 @@ linters: - depguard - dogsled - dupl + - dupword - durationcheck - errcheck - errchkjson @@ -2085,6 +2095,7 @@ linters: - depguard - dogsled - dupl + - dupword - durationcheck - errcheck - errchkjson diff --git a/go.mod b/go.mod index fea2cdedc292..26aed1b14155 100644 --- a/go.mod +++ b/go.mod @@ -4,6 +4,7 @@ go 1.19 require ( 4d63.com/gochecknoglobals v0.1.0 + github.com/Abirdcfly/dupword v0.0.7 github.com/Antonboom/errname v0.1.7 github.com/Antonboom/nilnil v0.1.1 github.com/BurntSushi/toml v1.2.0 diff --git a/go.sum b/go.sum index 3e3c170f0f62..c924e9c2d742 100644 --- a/go.sum +++ b/go.sum @@ -38,6 +38,8 @@ cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RX cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0= cloud.google.com/go/storage v1.14.0/go.mod h1:GrKmX003DSIwi9o29oFT7YDnHYwZoctc3fOKtUw0Xmo= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= +github.com/Abirdcfly/dupword v0.0.7 h1:z14n0yytA3wNO2gpCD/jVtp/acEXPGmYu0esewpBt6Q= +github.com/Abirdcfly/dupword v0.0.7/go.mod h1:K/4M1kj+Zh39d2aotRwypvasonOyAMH1c/IZJzE0dmk= github.com/Antonboom/errname v0.1.7 h1:mBBDKvEYwPl4WFFNwec1CZO096G6vzK9vvDQzAwkako= github.com/Antonboom/errname v0.1.7/go.mod h1:g0ONh16msHIPgJSGsecu1G/dcF2hlYR/0SddnIAGavU= github.com/Antonboom/nilnil v0.1.1 h1:PHhrh5ANKFWRBh7TdYmyyq2gyT2lotnvFvvFbylF81Q= diff --git a/pkg/config/linters_settings.go b/pkg/config/linters_settings.go index 06bbd401ddd7..12ffde213401 100644 --- a/pkg/config/linters_settings.go +++ b/pkg/config/linters_settings.go @@ -140,6 +140,7 @@ type LintersSettings struct { Depguard DepGuardSettings Dogsled DogsledSettings Dupl DuplSettings + DupWord DupWordSettings Errcheck ErrcheckSettings ErrChkJSON ErrChkJSONSettings ErrorLint ErrorLintSettings @@ -257,6 +258,10 @@ type DuplSettings struct { Threshold int } +type DupWordSettings struct { + Keywords []string `mapstructure:"keywords"` +} + type ErrcheckSettings struct { DisableDefaultExclusions bool `mapstructure:"disable-default-exclusions"` CheckTypeAssertions bool `mapstructure:"check-type-assertions"` diff --git a/pkg/golinters/dupword.go b/pkg/golinters/dupword.go new file mode 100644 index 000000000000..ae85a6d002a6 --- /dev/null +++ b/pkg/golinters/dupword.go @@ -0,0 +1,29 @@ +package golinters + +import ( + "strings" + + "github.com/Abirdcfly/dupword" + "golang.org/x/tools/go/analysis" + + "github.com/golangci/golangci-lint/pkg/config" + "github.com/golangci/golangci-lint/pkg/golinters/goanalysis" +) + +func NewDupWord(setting *config.DupWordSettings) *goanalysis.Linter { + a := dupword.NewAnalyzer() + + cfgMap := map[string]map[string]interface{}{} + if setting != nil { + cfgMap[a.Name] = map[string]interface{}{ + "keyword": strings.Join(setting.Keywords, ","), + } + } + + return goanalysis.NewLinter( + a.Name, + "checks for duplicate words in the source code", + []*analysis.Analyzer{a}, + cfgMap, + ).WithLoadMode(goanalysis.LoadModeSyntax) +} diff --git a/pkg/lint/lintersdb/manager.go b/pkg/lint/lintersdb/manager.go index cc6da09b75e0..d0833f1ee622 100644 --- a/pkg/lint/lintersdb/manager.go +++ b/pkg/lint/lintersdb/manager.go @@ -108,6 +108,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { depGuardCfg *config.DepGuardSettings dogsledCfg *config.DogsledSettings duplCfg *config.DuplSettings + dupwordCfg *config.DupWordSettings errcheckCfg *config.ErrcheckSettings errchkjsonCfg *config.ErrChkJSONSettings errorlintCfg *config.ErrorLintSettings @@ -183,6 +184,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { depGuardCfg = &m.cfg.LintersSettings.Depguard dogsledCfg = &m.cfg.LintersSettings.Dogsled duplCfg = &m.cfg.LintersSettings.Dupl + dupwordCfg = &m.cfg.LintersSettings.DupWord errcheckCfg = &m.cfg.LintersSettings.Errcheck errchkjsonCfg = &m.cfg.LintersSettings.ErrChkJSON errorlintCfg = &m.cfg.LintersSettings.ErrorLint @@ -341,6 +343,12 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config { WithPresets(linter.PresetStyle). WithURL("https://github.com/mibk/dupl"), + linter.NewConfig(golinters.NewDupWord(dupwordCfg)). + WithSince("1.50.0"). + WithPresets(linter.PresetComment). + WithAutoFix(). + WithURL("https://github.com/Abirdcfly/dupword"), + linter.NewConfig(golinters.NewDurationCheck()). WithSince("v1.37.0"). WithPresets(linter.PresetBugs). diff --git a/test/testdata/dupword.go b/test/testdata/dupword.go new file mode 100644 index 000000000000..16220f132c6d --- /dev/null +++ b/test/testdata/dupword.go @@ -0,0 +1,15 @@ +//golangcitest:args -Edupword +package testdata + +import "fmt" + +func duplicateWordInComments() { + // this line include duplicated word the the // want `Duplicate words \(the\) found` + fmt.Println("hello") +} + +func duplicateWordInStr() { + a := "this line include duplicate word and and" // want `Duplicate words \(and\) found` + b := "print the\n the line, print the the \n\t the line. and and" // want `Duplicate words \(and,the\) found` + fmt.Println(a, b) +}