Skip to content

Commit

Permalink
pkg/golinters: add noreplace
Browse files Browse the repository at this point in the history
Add support for enforcing that go.mod should not contain replace
clauses.
  • Loading branch information
carnott-snap committed Mar 3, 2021
1 parent d4ee818 commit 91c44b0
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 3 deletions.
8 changes: 6 additions & 2 deletions assets/github-action-config.json
@@ -1,8 +1,8 @@
{
"MinorVersionToConfig": {
"latest": {
"TargetVersion": "v1.37.1",
"AssetURL": "https://github.com/golangci/golangci-lint/releases/download/v1.37.1/golangci-lint-1.37.1-linux-amd64.tar.gz"
"TargetVersion": "v1.38.0",
"AssetURL": "https://github.com/golangci/golangci-lint/releases/download/v1.38.0/golangci-lint-1.38.0-linux-amd64.tar.gz"
},
"v1.10": {
"Error": "golangci-lint version 'v1.10' isn't supported: we support only v1.14.0 and later versions"
Expand Down Expand Up @@ -115,6 +115,10 @@
"TargetVersion": "v1.37.1",
"AssetURL": "https://github.com/golangci/golangci-lint/releases/download/v1.37.1/golangci-lint-1.37.1-linux-amd64.tar.gz"
},
"v1.38": {
"TargetVersion": "v1.38.0",
"AssetURL": "https://github.com/golangci/golangci-lint/releases/download/v1.38.0/golangci-lint-1.38.0-linux-amd64.tar.gz"
},
"v1.4": {
"Error": "golangci-lint version 'v1.4' isn't supported: we support only v1.14.0 and later versions"
},
Expand Down
1 change: 1 addition & 0 deletions go.mod
Expand Up @@ -4,6 +4,7 @@ go 1.14

require (
4d63.com/gochecknoglobals v0.0.0-20201008074935-acfc0b28355a
git.sr.ht/~urandom/noreplace v0.0.0-20210303085701-97f39e5a1bba
github.com/BurntSushi/toml v0.3.1
github.com/Djarvur/go-err113 v0.0.0-20210108212216-aea10b59be24
github.com/OpenPeeDeeP/depguard v1.0.1
Expand Down
5 changes: 4 additions & 1 deletion go.sum

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

53 changes: 53 additions & 0 deletions pkg/golinters/noreplace.go
@@ -0,0 +1,53 @@
package golinters

import (
"sync"

"git.sr.ht/~urandom/noreplace"
"golang.org/x/tools/go/analysis"

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

const noreplaceName = "noreplace"

// NewNoreplace returns a new noreplace linter.
func NewNoreplace() *goanalysis.Linter {
var issues []goanalysis.Issue
var mu sync.Mutex
analyzer := &analysis.Analyzer{
Name: goanalysis.TheOnlyAnalyzerName,
Doc: goanalysis.TheOnlyanalyzerDoc,
}
return goanalysis.NewLinter(
noreplaceName,
"Block the use of replace directives Go modules.",
[]*analysis.Analyzer{analyzer},
nil,
).WithContextSetter(func(lintCtx *linter.Context) {
analyzer.Run = func(pass *analysis.Pass) (interface{}, error) {
pp, err := noreplace.Check()
if err != nil {
lintCtx.Log.Warnf("running %s failed: %s: if you are not using go modules "+
"it is suggested to disable this linter", noreplaceName, err)
return nil, nil
}
mu.Lock()
defer mu.Unlock()
for _, p := range pp {
issues = append(issues, goanalysis.NewIssue(&result.Issue{
FromLinter: noreplaceName,
Pos: p[0],
LineRange: &result.Range{From: p[0].Line, To: p[1].Line},
Replacement: &result.Replacement{NeedOnlyDelete: true},
Text: noreplace.Text,
}, pass))
}
return nil, nil
}
}).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue {
return issues
})
}
3 changes: 3 additions & 0 deletions pkg/lint/lintersdb/manager.go
Expand Up @@ -394,6 +394,9 @@ func (m Manager) GetAllSupportedLinterConfigs() []*linter.Config {
WithPresets(linter.PresetStyle).
WithLoadForGoAnalysis().
WithURL("https://github.com/gostaticanalysis/forcetypeassert"),
linter.NewConfig(golinters.NewNoreplace()).
WithAutoFix().
WithURL("https://git.sr.ht/~urandom/noreplace"),

// 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 91c44b0

Please sign in to comment.