Skip to content

Commit

Permalink
dev: displays revive rules inside debug logs (#5325)
Browse files Browse the repository at this point in the history
Co-authored-by: Fernandez Ludovic <ldez@users.noreply.github.com>
  • Loading branch information
ccoVeille and ldez authored Jan 15, 2025
1 parent 37bca37 commit d45036a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .golangci.next.reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
44 changes: 41 additions & 3 deletions pkg/golinters/revive/revive.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"go/token"
"os"
"reflect"
"slices"
"strings"
"sync"

"github.com/BurntSushi/toml"
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -236,8 +243,6 @@ func getConfig(cfg *config.ReviveSettings) (*lint.Config, error) {
conf.Rules[k] = r
}

debugf("revive configuration: %#v", conf)

return conf, nil
}

Expand Down Expand Up @@ -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
}

0 comments on commit d45036a

Please sign in to comment.