Skip to content

Commit febe5fd

Browse files
authored
feat: add linter dupword (#3192)
1 parent c0532b3 commit febe5fd

File tree

7 files changed

+71
-0
lines changed

7 files changed

+71
-0
lines changed

.golangci.reference.yml

+11
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,15 @@ linters-settings:
224224
# Default: 150
225225
threshold: 100
226226

227+
dupword:
228+
# Keywords for detecting duplicate words.
229+
# If this list is not empty, only the words defined in this list will be detected.
230+
# Default: []
231+
keywords:
232+
- "the"
233+
- "and"
234+
- "a"
235+
227236
errcheck:
228237
# Report about not checking of errors in type assertions: `a := b.(MyStruct)`.
229238
# Such cases aren't reported by default.
@@ -1954,6 +1963,7 @@ linters:
19541963
- depguard
19551964
- dogsled
19561965
- dupl
1966+
- dupword
19571967
- durationcheck
19581968
- errcheck
19591969
- errchkjson
@@ -2060,6 +2070,7 @@ linters:
20602070
- depguard
20612071
- dogsled
20622072
- dupl
2073+
- dupword
20632074
- durationcheck
20642075
- errcheck
20652076
- errchkjson

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.19
44

55
require (
66
4d63.com/gochecknoglobals v0.1.0
7+
github.com/Abirdcfly/dupword v0.0.7
78
github.com/Antonboom/errname v0.1.7
89
github.com/Antonboom/nilnil v0.1.1
910
github.com/BurntSushi/toml v1.2.0

go.sum

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/config/linters_settings.go

+5
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ type LintersSettings struct {
140140
Depguard DepGuardSettings
141141
Dogsled DogsledSettings
142142
Dupl DuplSettings
143+
DupWord DupWordSettings
143144
Errcheck ErrcheckSettings
144145
ErrChkJSON ErrChkJSONSettings
145146
ErrorLint ErrorLintSettings
@@ -257,6 +258,10 @@ type DuplSettings struct {
257258
Threshold int
258259
}
259260

261+
type DupWordSettings struct {
262+
Keywords []string `mapstructure:"keywords"`
263+
}
264+
260265
type ErrcheckSettings struct {
261266
DisableDefaultExclusions bool `mapstructure:"disable-default-exclusions"`
262267
CheckTypeAssertions bool `mapstructure:"check-type-assertions"`

pkg/golinters/dupword.go

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package golinters
2+
3+
import (
4+
"strings"
5+
6+
"github.com/Abirdcfly/dupword"
7+
"golang.org/x/tools/go/analysis"
8+
9+
"github.com/golangci/golangci-lint/pkg/config"
10+
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
11+
)
12+
13+
func NewDupWord(setting *config.DupWordSettings) *goanalysis.Linter {
14+
a := dupword.NewAnalyzer()
15+
16+
cfgMap := map[string]map[string]interface{}{}
17+
if setting != nil {
18+
cfgMap[a.Name] = map[string]interface{}{
19+
"keyword": strings.Join(setting.Keywords, ","),
20+
}
21+
}
22+
23+
return goanalysis.NewLinter(
24+
a.Name,
25+
"checks for duplicate words in the source code",
26+
[]*analysis.Analyzer{a},
27+
cfgMap,
28+
).WithLoadMode(goanalysis.LoadModeSyntax)
29+
}

pkg/lint/lintersdb/manager.go

+8
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
108108
depGuardCfg *config.DepGuardSettings
109109
dogsledCfg *config.DogsledSettings
110110
duplCfg *config.DuplSettings
111+
dupwordCfg *config.DupWordSettings
111112
errcheckCfg *config.ErrcheckSettings
112113
errchkjsonCfg *config.ErrChkJSONSettings
113114
errorlintCfg *config.ErrorLintSettings
@@ -183,6 +184,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
183184
depGuardCfg = &m.cfg.LintersSettings.Depguard
184185
dogsledCfg = &m.cfg.LintersSettings.Dogsled
185186
duplCfg = &m.cfg.LintersSettings.Dupl
187+
dupwordCfg = &m.cfg.LintersSettings.DupWord
186188
errcheckCfg = &m.cfg.LintersSettings.Errcheck
187189
errchkjsonCfg = &m.cfg.LintersSettings.ErrChkJSON
188190
errorlintCfg = &m.cfg.LintersSettings.ErrorLint
@@ -341,6 +343,12 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
341343
WithPresets(linter.PresetStyle).
342344
WithURL("https://github.com/mibk/dupl"),
343345

346+
linter.NewConfig(golinters.NewDupWord(dupwordCfg)).
347+
WithSince("1.50.0").
348+
WithPresets(linter.PresetComment).
349+
WithAutoFix().
350+
WithURL("https://github.com/Abirdcfly/dupword"),
351+
344352
linter.NewConfig(golinters.NewDurationCheck()).
345353
WithSince("v1.37.0").
346354
WithPresets(linter.PresetBugs).

test/testdata/dupword.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//golangcitest:args -Edupword
2+
package testdata
3+
4+
import "fmt"
5+
6+
func duplicateWordInComments() {
7+
// this line include duplicated word the the // want `Duplicate words \(the\) found`
8+
fmt.Println("hello")
9+
}
10+
11+
func duplicateWordInStr() {
12+
a := "this line include duplicate word and and" // want `Duplicate words \(and\) found`
13+
b := "print the\n the line, print the the \n\t the line. and and" // want `Duplicate words \(and,the\) found`
14+
fmt.Println(a, b)
15+
}

0 commit comments

Comments
 (0)