Skip to content

Commit 876adab

Browse files
committed
feat: add linter dupword
Signed-off-by: Abirdcfly <[email protected]>
1 parent 70d595e commit 876adab

File tree

7 files changed

+72
-0
lines changed

7 files changed

+72
-0
lines changed

.golangci.reference.yml

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

227+
dupword:
228+
# key words for detecting duplicate words, will override default value
229+
# Default: the, and, a
230+
keyword:
231+
- "the"
232+
- "and"
233+
- "a"
234+
227235
errcheck:
228236
# Report about not checking of errors in type assertions: `a := b.(MyStruct)`.
229237
# Such cases aren't reported by default.
@@ -1913,6 +1921,7 @@ linters:
19131921
- depguard
19141922
- dogsled
19151923
- dupl
1924+
- dupword
19161925
- durationcheck
19171926
- errcheck
19181927
- errchkjson
@@ -2018,6 +2027,7 @@ linters:
20182027
- depguard
20192028
- dogsled
20202029
- dupl
2030+
- dupword
20212031
- durationcheck
20222032
- errcheck
20232033
- 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.6
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

+8
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ var defaultLintersSettings = LintersSettings{
1919
Dogsled: DogsledSettings{
2020
MaxBlankIdentifiers: 2,
2121
},
22+
DupWord: DupWordSettings{
23+
KeyWord: []string{"the", "and", "a"},
24+
},
2225
ErrorLint: ErrorLintSettings{
2326
Errorf: true,
2427
Asserts: true,
@@ -122,6 +125,7 @@ type LintersSettings struct {
122125
Depguard DepGuardSettings
123126
Dogsled DogsledSettings
124127
Dupl DuplSettings
128+
DupWord DupWordSettings
125129
Errcheck ErrcheckSettings
126130
ErrChkJSON ErrChkJSONSettings
127131
ErrorLint ErrorLintSettings
@@ -238,6 +242,10 @@ type DuplSettings struct {
238242
Threshold int
239243
}
240244

245+
type DupWordSettings struct {
246+
KeyWord []string `mapstructure:"keyword"`
247+
}
248+
241249
type ErrcheckSettings struct {
242250
DisableDefaultExclusions bool `mapstructure:"disable-default-exclusions"`
243251
CheckTypeAssertions bool `mapstructure:"check-type-assertions"`

pkg/golinters/dupword.go

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

pkg/lint/lintersdb/manager.go

+9
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
@@ -182,6 +183,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
182183
depGuardCfg = &m.cfg.LintersSettings.Depguard
183184
dogsledCfg = &m.cfg.LintersSettings.Dogsled
184185
duplCfg = &m.cfg.LintersSettings.Dupl
186+
dupwordCfg = &m.cfg.LintersSettings.DupWord
185187
errcheckCfg = &m.cfg.LintersSettings.Errcheck
186188
errchkjsonCfg = &m.cfg.LintersSettings.ErrChkJSON
187189
errorlintCfg = &m.cfg.LintersSettings.ErrorLint
@@ -339,6 +341,13 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
339341
WithPresets(linter.PresetStyle).
340342
WithURL("https://github.com/mibk/dupl"),
341343

344+
linter.NewConfig(golinters.NewDupWord(dupwordCfg)).
345+
WithSince("1.50.0").
346+
WithPresets(linter.PresetComment).
347+
WithLoadForGoAnalysis().
348+
WithAutoFix().
349+
WithURL("https://github.com/Abirdcfly/dupword"),
350+
342351
linter.NewConfig(golinters.NewDurationCheck()).
343352
WithSince("v1.37.0").
344353
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 \(the,and\) found`
14+
fmt.Println(a, b)
15+
}

0 commit comments

Comments
 (0)