From d45036ac01d5f8038da53ecb10fc6bbc0e97c24a Mon Sep 17 00:00:00 2001 From: ccoVeille <3875889+ccoVeille@users.noreply.github.com> Date: Wed, 15 Jan 2025 17:18:08 +0100 Subject: [PATCH] dev: displays revive rules inside debug logs (#5325) Co-authored-by: Fernandez Ludovic --- .golangci.next.reference.yml | 2 +- pkg/golinters/revive/revive.go | 44 +++++++++++++++++++++++++++++++--- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/.golangci.next.reference.yml b/.golangci.next.reference.yml index c8ffc45edfca..aeda8070e4ae 100644 --- a/.golangci.next.reference.yml +++ b/.golangci.next.reference.yml @@ -2371,7 +2371,7 @@ linters-settings: # This means that linting errors with less than 0.8 confidence will be ignored. # Default: 0.8 confidence: 0.1 - + # Run `GL_DEBUG=revive golangci-lint run --enable-only=revive` to see default, all available rules, and enabled rules. rules: # /~https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#add-constant - name: add-constant diff --git a/pkg/golinters/revive/revive.go b/pkg/golinters/revive/revive.go index ec621ccfba28..00b5c8c417e6 100644 --- a/pkg/golinters/revive/revive.go +++ b/pkg/golinters/revive/revive.go @@ -8,6 +8,8 @@ import ( "go/token" "os" "reflect" + "slices" + "strings" "sync" "github.com/BurntSushi/toml" @@ -27,7 +29,10 @@ import ( const linterName = "revive" -var debugf = logutils.Debug(logutils.DebugKeyRevive) +var ( + debugf = logutils.Debug(logutils.DebugKeyRevive) + isDebug = logutils.HaveDebugTag(logutils.DebugKeyRevive) +) // jsonObject defines a JSON object of a failure type jsonObject struct { @@ -91,6 +96,8 @@ func newWrapper(settings *config.ReviveSettings) (*wrapper, error) { return nil, err } + displayRules(conf) + conf.GoVersion, err = hcversion.NewVersion(settings.Go) if err != nil { return nil, err @@ -236,8 +243,6 @@ func getConfig(cfg *config.ReviveSettings) (*lint.Config, error) { conf.Rules[k] = r } - debugf("revive configuration: %#v", conf) - return conf, nil } @@ -447,3 +452,36 @@ func defaultConfig() *lint.Config { } return &defaultConfig } + +func displayRules(conf *lint.Config) { + if !isDebug { + return + } + + var enabledRules []string + for k, r := range conf.Rules { + if !r.Disabled { + enabledRules = append(enabledRules, k) + } + } + + slices.Sort(enabledRules) + + debugf("All available rules (%d): %s.", len(allRules), strings.Join(extractRulesName(allRules), ", ")) + debugf("Default rules (%d): %s.", len(allRules), strings.Join(extractRulesName(allRules), ", ")) + debugf("Enabled by config rules (%d): %s.", len(enabledRules), strings.Join(enabledRules, ", ")) + + debugf("revive configuration: %#v", conf) +} + +func extractRulesName(rules []lint.Rule) []string { + var names []string + + for _, r := range rules { + names = append(names, r.Name()) + } + + slices.Sort(names) + + return names +}