Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for enforcing that go.mod should not contain replace clauses.
- Loading branch information
1 parent
d4ee818
commit 596a91f
Showing
5 changed files
with
68 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
}).WithLoadMode(goanalysis.LoadModeSyntax) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters