feat: add linter dupword ()

This commit is contained in:
Abirdcfly 2022-09-16 01:08:52 +08:00 committed by GitHub
parent c0532b3a5f
commit febe5fdb33
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 71 additions and 0 deletions

@ -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.
@ -1954,6 +1963,7 @@ linters:
- depguard
- dogsled
- dupl
- dupword
- durationcheck
- errcheck
- errchkjson
@ -2060,6 +2070,7 @@ linters:
- depguard
- dogsled
- dupl
- dupword
- durationcheck
- errcheck
- errchkjson

1
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

2
go.sum generated

@ -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=

@ -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"`

29
pkg/golinters/dupword.go Normal file

@ -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)
}

@ -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).

15
test/testdata/dupword.go vendored Normal file

@ -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)
}