Skip to content

dev: displays revive rules inside debug logs #5325

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

Merged
merged 5 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
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
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
}
Loading