Skip to content

Commit

Permalink
feat: add linter dupword
Browse files Browse the repository at this point in the history
Signed-off-by: Abirdcfly <fp544037857@gmail.com>
  • Loading branch information
Abirdcfly committed Sep 5, 2022
1 parent 70d595e commit 766c936
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .golangci.reference.yml
Expand Up @@ -224,6 +224,14 @@ linters-settings:
# Default: 150
threshold: 100

dupword:
# key words for detecting duplicate words, will override default value
# Default: the, and, a
keyword:
- "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 @@ -1913,6 +1921,7 @@ linters:
- depguard
- dogsled
- dupl
- dupword
- durationcheck
- errcheck
- errchkjson
Expand Down Expand Up @@ -2018,6 +2027,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.5
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.

8 changes: 8 additions & 0 deletions pkg/config/linters_settings.go
Expand Up @@ -19,6 +19,9 @@ var defaultLintersSettings = LintersSettings{
Dogsled: DogsledSettings{
MaxBlankIdentifiers: 2,
},
DupWord: DupWordSettings{
KeyWord: []string{"the", "and", "a"},
},
ErrorLint: ErrorLintSettings{
Errorf: true,
Asserts: true,
Expand Down Expand Up @@ -122,6 +125,7 @@ type LintersSettings struct {
Depguard DepGuardSettings
Dogsled DogsledSettings
Dupl DuplSettings
DupWord DupWordSettings
Errcheck ErrcheckSettings
ErrChkJSON ErrChkJSONSettings
ErrorLint ErrorLintSettings
Expand Down Expand Up @@ -238,6 +242,10 @@ type DuplSettings struct {
Threshold int
}

type DupWordSettings struct {
KeyWord []string `mapstructure:"keyword"`
}

type ErrcheckSettings struct {
DisableDefaultExclusions bool `mapstructure:"disable-default-exclusions"`
CheckTypeAssertions bool `mapstructure:"check-type-assertions"`
Expand Down
27 changes: 27 additions & 0 deletions pkg/golinters/dupword.go
@@ -0,0 +1,27 @@
package golinters

import (
"strings"

"github.com/Abirdcfly/dupword"
"github.com/golangci/golangci-lint/pkg/config"
"golang.org/x/tools/go/analysis"

"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.KeyWord, ","),
}
}
return goanalysis.NewLinter(
"dupword",
"checks for duplicate words in the source code (usually miswritten)",
[]*analysis.Analyzer{a},
cfgMap,
).WithLoadMode(goanalysis.LoadModeSyntax)
}
9 changes: 9 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 @@ -182,6 +183,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 @@ -339,6 +341,13 @@ 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).
WithLoadForGoAnalysis().
WithAutoFix().
WithURL("https://github.com/Abirdcfly/dupword"),

linter.NewConfig(golinters.NewDurationCheck()).
WithSince("v1.37.0").
WithPresets(linter.PresetBugs).
Expand Down
11 changes: 11 additions & 0 deletions test/testdata/dupword.go
@@ -0,0 +1,11 @@
//golangcitest:args -Edupword
package testdata

func duplicateWordInComments() {
// this line include duplicated word the the // want `Duplicate words \(the\) found`
}

func duplicateWordInStr() {
_ = "this line include duplicate word and and" // want `Duplicate words \(and\) found`
_ = "print the\n the line, print the the \n\t the line.and and" // want `Duplicate words \(the,and\) found`
}

0 comments on commit 766c936

Please sign in to comment.