Skip to content

Commit

Permalink
Using upstrem goconst
Browse files Browse the repository at this point in the history
  • Loading branch information
iwankgb committed Nov 8, 2020
1 parent 3b06269 commit f4abafb
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 6 deletions.
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -17,7 +17,6 @@ require (
github.com/golangci/dupl v0.0.0-20180902072040-3e9179ac440a
github.com/golangci/errcheck v0.0.0-20181223084120-ef45e06d44b6
github.com/golangci/go-misc v0.0.0-20180628070357-927a3d87b613
github.com/golangci/goconst v0.0.0-20180610141641-041c5f2b40f3
github.com/golangci/gocyclo v0.0.0-20180528144436-0a533e8fa43d
github.com/golangci/gofmt v0.0.0-20190930125516-244bba706f1a
github.com/golangci/ineffassign v0.0.0-20190609212857-42439a7714cc
Expand All @@ -27,6 +26,7 @@ require (
github.com/golangci/prealloc v0.0.0-20180630174525-215b22d4de21
github.com/golangci/revgrep v0.0.0-20180526074752-d9c87f5ffaf0
github.com/golangci/unconvert v0.0.0-20180507085042-28b1c447d1f4
github.com/jgautheron/goconst v0.0.0-20201108141142-b58d7cf68fc6
github.com/jingyugao/rowserrcheck v0.0.0-20191204022205-72ab7603b68a
github.com/jirfag/go-printf-func-name v0.0.0-20191110105641-45db9963cdd3
github.com/kyoh86/exportloopref v0.1.7
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.

12 changes: 12 additions & 0 deletions pkg/commands/run.go
Expand Up @@ -158,12 +158,24 @@ func initFlagSet(fs *pflag.FlagSet, cfg *config.Config, m *lintersdb.Manager, is
150, "Dupl: Minimal threshold to detect copy-paste")
hideFlag("dupl.threshold")

fs.BoolVar(&lsc.Goconst.MatchWithConstants, "goconst.match-constant",
true, "Goconst: look for existing constants matching the values")
hideFlag("goconst.match-constant")
fs.IntVar(&lsc.Goconst.MinStringLen, "goconst.min-len",
3, "Goconst: minimum constant string length")
hideFlag("goconst.min-len")
fs.IntVar(&lsc.Goconst.MinOccurrencesCount, "goconst.min-occurrences",
3, "Goconst: minimum occurrences of constant string count to trigger issue")
hideFlag("goconst.min-occurrences")
fs.BoolVar(&lsc.Goconst.ParseNumbers, "goconst.numbers",
true, "Goconst: search also for duplicated numbers")
hideFlag("goconst.numbers")
fs.IntVar(&lsc.Goconst.NumberMin, "goconst.min",
3, "minimum value, only works with goconst.numbers")
hideFlag("goconst.min")
fs.IntVar(&lsc.Goconst.NumberMin, "goconst.max",
3, "maximum value, only works with goconst.numbers")
hideFlag("goconst.max")

// (@dixonwille) These flag is only used for testing purposes.
fs.StringSliceVar(&lsc.Depguard.Packages, "depguard.packages", nil,
Expand Down
8 changes: 6 additions & 2 deletions pkg/config/config.go
Expand Up @@ -185,8 +185,12 @@ type LintersSettings struct {
Threshold int
}
Goconst struct {
MinStringLen int `mapstructure:"min-len"`
MinOccurrencesCount int `mapstructure:"min-occurrences"`
MatchWithConstants bool `mapstructure:"match-constant"`
MinStringLen int `mapstructure:"min-len"`
MinOccurrencesCount int `mapstructure:"min-occurrences"`
ParseNumbers bool `mapstructure:"numbers"`
NumberMin int `mapstructure:"min"`
NumberMax int `mapstructure:"max"`
}
Gomnd struct {
Settings map[string]map[string]interface{}
Expand Down
9 changes: 6 additions & 3 deletions pkg/golinters/goconst.go
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"sync"

goconstAPI "github.com/golangci/goconst"
goconstAPI "github.com/jgautheron/goconst"
"golang.org/x/tools/go/analysis"

"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
Expand Down Expand Up @@ -47,9 +47,12 @@ func NewGoconst() *goanalysis.Linter {

func checkConstants(pass *analysis.Pass, lintCtx *linter.Context) ([]goanalysis.Issue, error) {
cfg := goconstAPI.Config{
MatchWithConstants: true,
MatchWithConstants: lintCtx.Settings().Goconst.MatchWithConstants,
MinStringLength: lintCtx.Settings().Goconst.MinStringLen,
MinOccurrences: lintCtx.Settings().Goconst.MinOccurrencesCount,
ParseNumbers: lintCtx.Settings().Goconst.ParseNumbers,
NumberMin: lintCtx.Settings().Goconst.NumberMin,
NumberMax: lintCtx.Settings().Goconst.NumberMax,
}

goconstIssues, err := goconstAPI.Run(pass.Files, pass.Fset, &cfg)
Expand All @@ -63,7 +66,7 @@ func checkConstants(pass *analysis.Pass, lintCtx *linter.Context) ([]goanalysis.

res := make([]goanalysis.Issue, 0, len(goconstIssues))
for _, i := range goconstIssues {
textBegin := fmt.Sprintf("string %s has %d occurrences", formatCode(i.Str, lintCtx.Cfg), i.OccurencesCount)
textBegin := fmt.Sprintf("string %s has %d occurrences", formatCode(i.Str, lintCtx.Cfg), i.OccurrencesCount)
var textEnd string
if i.MatchingConst == "" {
textEnd = ", make it a constant"
Expand Down

0 comments on commit f4abafb

Please sign in to comment.