Skip to content

Commit

Permalink
gomodreplace -> gomoddirectives
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Mar 6, 2021
1 parent 9a328ca commit b4a8eaf
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 27 deletions.
10 changes: 7 additions & 3 deletions .golangci.example.yml
Expand Up @@ -449,12 +449,16 @@ linters-settings:
servingv1: knative.dev/serving/pkg/apis/serving/v1
# using `autoscalingv1alpha1` alias for `knative.dev/serving/pkg/apis/autoscaling/v1alpha1` package
autoscalingv1alpha1: knative.dev/serving/pkg/apis/autoscaling/v1alpha1
gomodreplace:
gomoddirectives:
# Allow local `replace` directives. Default is false.
local: false
replace-local: false
# List of allowed `replace` directives. Default is empty.
allow-list:
replace-allow-list:
- launchpad.net/gocheck
# Allow to not explain why the version has been retracted in the `retract` directives. Default is false.
retract-allow-no-explanation: false
# Forbid the use of the `exclude` directives. Default is false.
exclude-forbidden: false

# The custom section can be used to define linter plugins to be loaded at runtime. See README doc
# for more info.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -41,7 +41,7 @@ require (
github.com/kulti/thelper v0.4.0
github.com/kunwardeep/paralleltest v1.0.2
github.com/kyoh86/exportloopref v0.1.8
github.com/ldez/gomodreplace v0.2.0
github.com/ldez/gomoddirectives v0.1.0
github.com/maratori/testpackage v1.0.1
github.com/matoous/godox v0.0.0-20210227103229-6504466cf951 // v1.0
github.com/mattn/go-colorable v0.1.8
Expand Down
4 changes: 2 additions & 2 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions pkg/config/config.go
Expand Up @@ -276,7 +276,7 @@ type LintersSettings struct {
Predeclared PredeclaredSettings
Cyclop Cyclop
ImportAs ImportAsSettings
GoModReplace GoModReplaceSettings
GoModDirectives GoModDirectivesSettings

Custom map[string]CustomLinterSettings
}
Expand Down Expand Up @@ -465,9 +465,11 @@ type Cyclop struct {

type ImportAsSettings map[string]string

type GoModReplaceSettings struct {
AllowList []string `mapstructure:"allow-list"`
Local bool `mapstructure:"local"`
type GoModDirectivesSettings struct {
ReplaceAllowList []string `mapstructure:"replace-allow-list"`
ReplaceLocal bool `mapstructure:"replace-local"`
ExcludeForbidden bool `mapstructure:"exclude-forbidden"`
RetractAllowNoExplanation bool `mapstructure:"retract-allow-no-explanation"`
}

var defaultLintersSettings = LintersSettings{
Expand Down
31 changes: 18 additions & 13 deletions pkg/golinters/gomodreplace.go → pkg/golinters/gomoddirectives.go
@@ -1,54 +1,59 @@
package golinters

import (
"fmt"
"sync"

"github.com/golangci/golangci-lint/pkg/config"
"github.com/ldez/gomodreplace"
"github.com/ldez/gomoddirectives"
"golang.org/x/tools/go/analysis"

"github.com/golangci/golangci-lint/pkg/config"
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis"
"github.com/golangci/golangci-lint/pkg/lint/linter"
"github.com/golangci/golangci-lint/pkg/result"
)

const goModReplaceName = "gomodreplace"
const goModDirectivesName = "gomoddirectives"

// NewGoModReplace returns a new gomodreplace linter.
func NewGoModReplace(settings *config.GoModReplaceSettings) *goanalysis.Linter {
// NewGoModDirectives returns a new gomoddirectives linter.
func NewGoModDirectives(settings *config.GoModDirectivesSettings) *goanalysis.Linter {
var issues []goanalysis.Issue
var mu sync.Mutex

var opts gomodreplace.Options
var opts gomoddirectives.Options
if settings != nil {
opts.AllowLocal = settings.Local
opts.AllowList = settings.AllowList
opts.ReplaceAllowLocal = settings.ReplaceLocal
opts.ReplaceAllowList = settings.ReplaceAllowList
opts.RetractAllowNoExplanation = settings.RetractAllowNoExplanation
opts.ExcludeForbidden = settings.ExcludeForbidden
}

println(fmt.Printf("%#v\n", opts))

analyzer := &analysis.Analyzer{
Name: goanalysis.TheOnlyAnalyzerName,
Doc: goanalysis.TheOnlyanalyzerDoc,
}

return goanalysis.NewLinter(
goModReplaceName,
"Manage the use of replace directives in go.mod.",
goModDirectivesName,
"Manage the use of 'replace', 'retract', and 'excludes' directives in go.mod.",
[]*analysis.Analyzer{analyzer},
nil,
).WithContextSetter(func(lintCtx *linter.Context) {
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
results, err := gomodreplace.Analyze(opts)
results, err := gomoddirectives.Analyze(opts)
if err != nil {
lintCtx.Log.Warnf("running %s failed: %s: "+
"if you are not using go modules it is suggested to disable this linter", goModReplaceName, err)
"if you are not using go modules it is suggested to disable this linter", goModDirectivesName, err)
return nil, nil
}

mu.Lock()

for _, p := range results {
issues = append(issues, goanalysis.NewIssue(&result.Issue{
FromLinter: goModReplaceName,
FromLinter: goModDirectivesName,
Pos: p.Start,
Text: p.Reason,
}, pass))
Expand Down
8 changes: 4 additions & 4 deletions pkg/lint/lintersdb/manager.go
Expand Up @@ -100,7 +100,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
var reviveCfg *config.ReviveSettings
var cyclopCfg *config.Cyclop
var importAsCfg *config.ImportAsSettings
var goModReplaceCfg *config.GoModReplaceSettings
var goModDirectivesCfg *config.GoModDirectivesSettings
if m.cfg != nil {
govetCfg = &m.cfg.LintersSettings.Govet
testpackageCfg = &m.cfg.LintersSettings.Testpackage
Expand All @@ -113,7 +113,7 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
reviveCfg = &m.cfg.LintersSettings.Revive
cyclopCfg = &m.cfg.LintersSettings.Cyclop
importAsCfg = &m.cfg.LintersSettings.ImportAs
goModReplaceCfg = &m.cfg.LintersSettings.GoModReplace
goModDirectivesCfg = &m.cfg.LintersSettings.GoModDirectives
}
const megacheckName = "megacheck"
lcs := []*linter.Config{
Expand Down Expand Up @@ -396,10 +396,10 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithPresets(linter.PresetStyle).
WithLoadForGoAnalysis().
WithURL("https://github.com/gostaticanalysis/forcetypeassert"),
linter.NewConfig(golinters.NewGoModReplace(goModReplaceCfg)).
linter.NewConfig(golinters.NewGoModDirectives(goModDirectivesCfg)).
WithPresets(linter.PresetStyle).
WithLoadForGoAnalysis().
WithURL("https://github.com/ldez/gomodreplace"),
WithURL("https://github.com/ldez/gomoddirectives"),

// nolintlint must be last because it looks at the results of all the previous linters for unused nolint directives
linter.NewConfig(golinters.NewNoLintLint()).
Expand Down

0 comments on commit b4a8eaf

Please sign in to comment.