Skip to content

Commit 08ccc56

Browse files
Abirdcflyldez
authored andcommitted
feat: add linter dupword
Signed-off-by: Abirdcfly <[email protected]>
1 parent 5967201 commit 08ccc56

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.
@@ -1954,6 +1962,7 @@ linters:
19541962
- depguard
19551963
- dogsled
19561964
- dupl
1965+
- dupword
19571966
- durationcheck
19581967
- errcheck
19591968
- errchkjson
@@ -2060,6 +2069,7 @@ linters:
20602069
- depguard
20612070
- dogsled
20622071
- dupl
2072+
- dupword
20632073
- durationcheck
20642074
- errcheck
20652075
- 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,
@@ -140,6 +143,7 @@ type LintersSettings struct {
140143
Depguard DepGuardSettings
141144
Dogsled DogsledSettings
142145
Dupl DuplSettings
146+
DupWord DupWordSettings
143147
Errcheck ErrcheckSettings
144148
ErrChkJSON ErrChkJSONSettings
145149
ErrorLint ErrorLintSettings
@@ -257,6 +261,10 @@ type DuplSettings struct {
257261
Threshold int
258262
}
259263

264+
type DupWordSettings struct {
265+
KeyWord []string `mapstructure:"keyword"`
266+
}
267+
260268
type ErrcheckSettings struct {
261269
DisableDefaultExclusions bool `mapstructure:"disable-default-exclusions"`
262270
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
@@ -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,13 @@ 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+
WithLoadForGoAnalysis().
350+
WithAutoFix().
351+
WithURL("https://github.com/Abirdcfly/dupword"),
352+
344353
linter.NewConfig(golinters.NewDurationCheck()).
345354
WithSince("v1.37.0").
346355
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)