Skip to content

Commit

Permalink
feat: add linter dupword (#3192)
Browse files Browse the repository at this point in the history
  • Loading branch information
Abirdcfly committed Sep 15, 2022
1 parent c0532b3 commit febe5fd
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 0 deletions.
11 changes: 11 additions & 0 deletions .golangci.reference.yml
Expand Up @@ -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.
Expand Down Expand Up @@ -1954,6 +1963,7 @@ linters:
- depguard
- dogsled
- dupl
- dupword
- durationcheck
- errcheck
- errchkjson
Expand Down Expand Up @@ -2060,6 +2070,7 @@ linters:
- depguard
- dogsled
- dupl
- dupword
- durationcheck
- errcheck
- errchkjson
Expand Down
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions pkg/config/linters_settings.go
Expand Up @@ -140,6 +140,7 @@ type LintersSettings struct {
Depguard DepGuardSettings
Dogsled DogsledSettings
Dupl DuplSettings
DupWord DupWordSettings
Errcheck ErrcheckSettings
ErrChkJSON ErrChkJSONSettings
ErrorLint ErrorLintSettings
Expand Down Expand Up @@ -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"`
Expand Down
29 changes: 29 additions & 0 deletions 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)
}
8 changes: 8 additions & 0 deletions pkg/lint/lintersdb/manager.go
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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).
Expand Down
15 changes: 15 additions & 0 deletions 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)
}

0 comments on commit febe5fd

Please sign in to comment.