dev: improve revive construction (#4817)
This commit is contained in:
parent
bff93a148a
commit
df71654b26
@ -49,8 +49,14 @@ func New(settings *config.ReviveSettings) *goanalysis.Linter {
|
|||||||
[]*analysis.Analyzer{analyzer},
|
[]*analysis.Analyzer{analyzer},
|
||||||
nil,
|
nil,
|
||||||
).WithContextSetter(func(lintCtx *linter.Context) {
|
).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) {
|
analyzer.Run = func(pass *analysis.Pass) (any, error) {
|
||||||
issues, err := runRevive(lintCtx, pass, settings)
|
issues, err := w.run(lintCtx, pass)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -70,10 +76,15 @@ func New(settings *config.ReviveSettings) *goanalysis.Linter {
|
|||||||
}).WithLoadMode(goanalysis.LoadModeSyntax)
|
}).WithLoadMode(goanalysis.LoadModeSyntax)
|
||||||
}
|
}
|
||||||
|
|
||||||
func runRevive(lintCtx *linter.Context, pass *analysis.Pass, settings *config.ReviveSettings) ([]goanalysis.Issue, error) {
|
type wrapper struct {
|
||||||
packages := [][]string{internal.GetFileNames(pass)}
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -83,14 +94,23 @@ func runRevive(lintCtx *linter.Context, pass *analysis.Pass, settings *config.Re
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
revive := lint.New(os.ReadFile, settings.MaxOpenFiles)
|
|
||||||
|
|
||||||
lintingRules, err := reviveConfig.GetLintingRules(conf, []lint.Rule{})
|
lintingRules, err := reviveConfig.GetLintingRules(conf, []lint.Rule{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
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 {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@ -100,7 +120,7 @@ func runRevive(lintCtx *linter.Context, pass *analysis.Pass, settings *config.Re
|
|||||||
|
|
||||||
var output string
|
var output string
|
||||||
go func() {
|
go func() {
|
||||||
output, err = formatter.Format(formatChan, *conf)
|
output, err = w.formatter.Format(formatChan, *w.conf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
lintCtx.Log.Errorf("Format error: %v", err)
|
lintCtx.Log.Errorf("Format error: %v", err)
|
||||||
}
|
}
|
||||||
@ -108,7 +128,7 @@ func runRevive(lintCtx *linter.Context, pass *analysis.Pass, settings *config.Re
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
for f := range failures {
|
for f := range failures {
|
||||||
if f.Confidence < conf.Confidence {
|
if f.Confidence < w.conf.Confidence {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -126,13 +146,13 @@ func runRevive(lintCtx *linter.Context, pass *analysis.Pass, settings *config.Re
|
|||||||
|
|
||||||
var issues []goanalysis.Issue
|
var issues []goanalysis.Issue
|
||||||
for i := range results {
|
for i := range results {
|
||||||
issues = append(issues, reviveToIssue(pass, &results[i]))
|
issues = append(issues, toIssue(pass, &results[i]))
|
||||||
}
|
}
|
||||||
|
|
||||||
return issues, nil
|
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
|
lineRangeTo := object.Position.End.Line
|
||||||
if object.RuleName == (&rule.ExportedRule{}).Name() {
|
if object.RuleName == (&rule.ExportedRule{}).Name() {
|
||||||
lineRangeTo = object.Position.Start.Line
|
lineRangeTo = object.Position.Start.Line
|
||||||
@ -160,7 +180,7 @@ func reviveToIssue(pass *analysis.Pass, object *jsonObject) goanalysis.Issue {
|
|||||||
// https://github.com/golangci/golangci-lint/issues/1745
|
// 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#L217
|
||||||
// https://github.com/mgechev/revive/blob/v1.3.7/config/config.go#L169-L174
|
// 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()
|
conf := defaultConfig()
|
||||||
|
|
||||||
if !reflect.DeepEqual(cfg, &config.ReviveSettings{}) {
|
if !reflect.DeepEqual(cfg, &config.ReviveSettings{}) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user