Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dev: improve revive construction #4817

Merged
merged 1 commit into from
Jun 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 32 additions & 12 deletions pkg/golinters/revive/revive.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,14 @@ func New(settings *config.ReviveSettings) *goanalysis.Linter {
[]*analysis.Analyzer{analyzer},
nil,
).WithContextSetter(func(lintCtx *linter.Context) {
w, err := newWrapper(settings)
if err != nil {
lintCtx.Log.Errorf("setup revive: %v", err)
return
}

analyzer.Run = func(pass *analysis.Pass) (any, error) {
issues, err := runRevive(lintCtx, pass, settings)
issues, err := w.run(lintCtx, pass)
if err != nil {
return nil, err
}
Expand All @@ -70,10 +76,15 @@ func New(settings *config.ReviveSettings) *goanalysis.Linter {
}).WithLoadMode(goanalysis.LoadModeSyntax)
}

func runRevive(lintCtx *linter.Context, pass *analysis.Pass, settings *config.ReviveSettings) ([]goanalysis.Issue, error) {
packages := [][]string{internal.GetFileNames(pass)}
type wrapper struct {
revive lint.Linter
formatter lint.Formatter
lintingRules []lint.Rule
conf *lint.Config
}

conf, err := getReviveConfig(settings)
func newWrapper(settings *config.ReviveSettings) (*wrapper, error) {
conf, err := getConfig(settings)
if err != nil {
return nil, err
}
Expand All @@ -83,14 +94,23 @@ func runRevive(lintCtx *linter.Context, pass *analysis.Pass, settings *config.Re
return nil, err
}

revive := lint.New(os.ReadFile, settings.MaxOpenFiles)

lintingRules, err := reviveConfig.GetLintingRules(conf, []lint.Rule{})
if err != nil {
return nil, err
}

failures, err := revive.Lint(packages, lintingRules, *conf)
return &wrapper{
revive: lint.New(os.ReadFile, settings.MaxOpenFiles),
formatter: formatter,
lintingRules: lintingRules,
conf: conf,
}, nil
}

func (w *wrapper) run(lintCtx *linter.Context, pass *analysis.Pass) ([]goanalysis.Issue, error) {
packages := [][]string{internal.GetFileNames(pass)}

failures, err := w.revive.Lint(packages, w.lintingRules, *w.conf)
if err != nil {
return nil, err
}
Expand All @@ -100,15 +120,15 @@ func runRevive(lintCtx *linter.Context, pass *analysis.Pass, settings *config.Re

var output string
go func() {
output, err = formatter.Format(formatChan, *conf)
output, err = w.formatter.Format(formatChan, *w.conf)
if err != nil {
lintCtx.Log.Errorf("Format error: %v", err)
}
exitChan <- true
}()

for f := range failures {
if f.Confidence < conf.Confidence {
if f.Confidence < w.conf.Confidence {
continue
}

Expand All @@ -126,13 +146,13 @@ func runRevive(lintCtx *linter.Context, pass *analysis.Pass, settings *config.Re

var issues []goanalysis.Issue
for i := range results {
issues = append(issues, reviveToIssue(pass, &results[i]))
issues = append(issues, toIssue(pass, &results[i]))
}

return issues, nil
}

func reviveToIssue(pass *analysis.Pass, object *jsonObject) goanalysis.Issue {
func toIssue(pass *analysis.Pass, object *jsonObject) goanalysis.Issue {
lineRangeTo := object.Position.End.Line
if object.RuleName == (&rule.ExportedRule{}).Name() {
lineRangeTo = object.Position.Start.Line
Expand Down Expand Up @@ -160,7 +180,7 @@ func reviveToIssue(pass *analysis.Pass, object *jsonObject) goanalysis.Issue {
// https://github.com/golangci/golangci-lint/issues/1745
// https://github.com/mgechev/revive/blob/v1.3.7/config/config.go#L217
// https://github.com/mgechev/revive/blob/v1.3.7/config/config.go#L169-L174
func getReviveConfig(cfg *config.ReviveSettings) (*lint.Config, error) {
func getConfig(cfg *config.ReviveSettings) (*lint.Config, error) {
conf := defaultConfig()

if !reflect.DeepEqual(cfg, &config.ReviveSettings{}) {
Expand Down
Loading