Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add linter dupword #3192

Merged
merged 4 commits into from
Sep 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .golangci.reference.yml
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//golangcitest:args -Edupword
package testdata

import "fmt"
ldez marked this conversation as resolved.
Show resolved Hide resolved

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