Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Jan 28, 2025
1 parent c2f23d6 commit f85fdaf
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 74 deletions.
8 changes: 2 additions & 6 deletions pkg/config/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,8 @@ func (l *Loader) Load(opts LoadOptions) error {
l.applyStringSliceHack()

if l.cfg.Linters.LinterExclusions.Generated == "" {
// This is always non-empty because of the flag default value.
if l.cfg.Issues.ExcludeGenerated != "" {
l.cfg.Linters.LinterExclusions.Generated = l.cfg.Issues.ExcludeGenerated
} else {
l.cfg.Linters.LinterExclusions.Generated = "strict"
}
// `l.cfg.Issues.ExcludeGenerated` is always non-empty because of the flag default value.
l.cfg.Linters.LinterExclusions.Generated = cmp.Or(l.cfg.Issues.ExcludeGenerated, "strict")
}

if l.cfg.Issues.UseDefaultExcludes {
Expand Down
6 changes: 0 additions & 6 deletions pkg/golinters/gocritic/testdata/gocritic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ linters-settings:
sizeThreshold: 24
ruleguard:
failOn: dsl,import
# Comma-separated paths to ruleguard files.
# The ${configDir} is substituted by the directory containing the golangci-lint config file.
# Note about the directory structure for functional tests:
# The ruleguard files used in functional tests cannot be under the 'testdata' directory.
# This is because they import the 'github.com/quasilyte/go-ruleguard/dsl' package,
# which needs to be added to go.mod. The testdata directory is ignored by go mod.
rules: '${base-path}/ruleguard/preferWriteString.go,${base-path}/ruleguard/stringsSimplify.go'

run:
Expand Down
6 changes: 0 additions & 6 deletions pkg/golinters/gocritic/testdata/gocritic_configDir.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,4 @@ linters-settings:
sizeThreshold: 24
ruleguard:
failOn: dsl,import
# Comma-separated paths to ruleguard files.
# The ${configDir} is substituted by the directory containing the golangci-lint config file.
# Note about the directory structure for functional tests:
# The ruleguard files used in functional tests cannot be under the 'testdata' directory.
# This is because they import the 'github.com/quasilyte/go-ruleguard/dsl' package,
# which needs to be added to go.mod. The testdata directory is ignored by go mod.
rules: '${configDir}/ruleguard/preferWriteString.go,${configDir}/ruleguard/stringsSimplify.go'
2 changes: 1 addition & 1 deletion pkg/golinters/internal/commons.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var LinterLogger = logutils.NewStderrLog(logutils.DebugKeyLinter)
const (
PlaceholderBasePath = "${base-path}"
// Deprecated: it must be removed in v2.
// [PlaceholderBasePath] will be the only one placeholder as it is a dynamic value base on
// [PlaceholderBasePath] will be the only one placeholder as it is a dynamic value based on
// [github.com/golangci/golangci-lint/pkg/config.Run.RelativePathMode].
PlaceholderConfigDir = "${configDir}"
)
94 changes: 47 additions & 47 deletions pkg/result/processors/exclusion_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,36 +14,6 @@ import (

var _ Processor = (*ExclusionRules)(nil)

type excludeRule struct {
baseRule
}

func (e excludeRule) String() string {
var msg []string

if e.text != nil && e.text.String() != "" {
msg = append(msg, fmt.Sprintf("Text: %q", e.text))
}

if e.source != nil && e.source.String() != "" {
msg = append(msg, fmt.Sprintf("Source: %q", e.source))
}

if e.path != nil && e.path.String() != "" {
msg = append(msg, fmt.Sprintf("Path: %q", e.path))
}

if e.pathExcept != nil && e.pathExcept.String() != "" {
msg = append(msg, fmt.Sprintf("Path Except: %q", e.pathExcept))
}

if len(e.linters) > 0 {
msg = append(msg, fmt.Sprintf("Linters: %q", strings.Join(e.linters, ", ")))
}

return strings.Join(msg, ", ")
}

type ExclusionRules struct {
log logutils.Log
files *fsutils.Files
Expand All @@ -68,18 +38,18 @@ func NewExclusionRules(log logutils.Log, files *fsutils.Files, cfg *config.Linte
prefix = ""
}

excludeRules := slices.Clone(cfg.Rules)
excludeRules = append(excludeRules, filterInclude(getDefaultLintersExclusions(cfg.Default), oldCfg.IncludeDefaultExcludes)...)
excludeRules := slices.Concat(slices.Clone(cfg.Rules),
filterInclude(getDefaultLintersExclusions(cfg.Default), oldCfg.IncludeDefaultExcludes))

p.rules = createRules(excludeRules, prefix)
p.rules = createExcludeRules(excludeRules, prefix)

// TODO(ldez): should be removed in v2.
for _, pattern := range oldCfg.ExcludePatterns {
if pattern == "" {
continue
}

rule := createRule(&config.ExcludeRule{
rule := newRule(&config.ExcludeRule{
BaseRule: config.BaseRule{
Path: `.+\.go`,
Text: pattern,
Expand Down Expand Up @@ -135,21 +105,11 @@ func (p *ExclusionRules) Finish() {
}
}

func createRules(rules []config.ExcludeRule, prefix string) []excludeRule {
if len(rules) == 0 {
return nil
}

parsedRules := make([]excludeRule, 0, len(rules))

for _, rule := range rules {
parsedRules = append(parsedRules, createRule(&rule, prefix))
}

return parsedRules
type excludeRule struct {
baseRule
}

func createRule(rule *config.ExcludeRule, prefix string) excludeRule {
func newRule(rule *config.ExcludeRule, prefix string) excludeRule {
parsedRule := excludeRule{}
parsedRule.linters = rule.Linters
parsedRule.internalReference = rule.InternalReference
Expand All @@ -173,6 +133,46 @@ func createRule(rule *config.ExcludeRule, prefix string) excludeRule {
return parsedRule
}

func (e excludeRule) String() string {
var msg []string

if e.text != nil && e.text.String() != "" {
msg = append(msg, fmt.Sprintf("Text: %q", e.text))
}

if e.source != nil && e.source.String() != "" {
msg = append(msg, fmt.Sprintf("Source: %q", e.source))
}

if e.path != nil && e.path.String() != "" {
msg = append(msg, fmt.Sprintf("Path: %q", e.path))
}

if e.pathExcept != nil && e.pathExcept.String() != "" {
msg = append(msg, fmt.Sprintf("Path Except: %q", e.pathExcept))
}

if len(e.linters) > 0 {
msg = append(msg, fmt.Sprintf("Linters: %q", strings.Join(e.linters, ", ")))
}

return strings.Join(msg, ", ")
}

func createExcludeRules(rules []config.ExcludeRule, prefix string) []excludeRule {
if len(rules) == 0 {
return nil
}

parsedRules := make([]excludeRule, 0, len(rules))

for _, rule := range rules {
parsedRules = append(parsedRules, newRule(&rule, prefix))
}

return parsedRules
}

// TODO(ldez): must be removed in v2, only for compatibility with exclude-use-default/include.
func filterInclude(rules []config.ExcludeRule, refs []string) []config.ExcludeRule {
if len(refs) == 0 {
Expand Down
10 changes: 5 additions & 5 deletions pkg/result/processors/path_relativity.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

var _ Processor = (*PathRelativity)(nil)

// PathRelativity computes [result.Issue.RelativePath] and [result.Issue.WorkingDirectoryRelativePath],
// PathRelativity computes [result.Issue.RelativePath] and [result.Issue.WorkingDirectoryRelativePath],
// based on the base path.
type PathRelativity struct {
log logutils.Log
Expand All @@ -34,22 +34,22 @@ func NewPathRelativity(log logutils.Log, basePath string) (*PathRelativity, erro

func (p *PathRelativity) Process(issues []result.Issue) ([]result.Issue, error) {
return transformIssues(issues, func(issue *result.Issue) *result.Issue {
newIssue := issue
newIssue := *issue

var err error
newIssue.RelativePath, err = filepath.Rel(p.basePath, issue.FilePath())
if err != nil {
p.log.Warnf("relative path (basepath): %v", err)
p.log.Warnf("Getting relative path (basepath): %v", err)
return nil
}

newIssue.WorkingDirectoryRelativePath, err = filepath.Rel(p.wd, issue.FilePath())
if err != nil {
p.log.Warnf("relative path (wd): %v", err)
p.log.Warnf("Getting relative path (wd): %v", err)
return nil
}

return newIssue
return &newIssue
}), nil
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/result/processors/uniq_by_line.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ var _ Processor = (*UniqByLine)(nil)
// UniqByLine filters reports to keep only one report by line of code.
type UniqByLine struct {
fileLineCounter fileLineCounter
enable bool
enabled bool
}

func NewUniqByLine(enable bool) *UniqByLine {
return &UniqByLine{
fileLineCounter: fileLineCounter{},
enable: enable,
enabled: enable,
}
}

Expand All @@ -26,7 +26,7 @@ func (*UniqByLine) Name() string {
}

func (p *UniqByLine) Process(issues []result.Issue) ([]result.Issue, error) {
if !p.enable {
if !p.enabled {
return issues, nil
}

Expand Down

0 comments on commit f85fdaf

Please sign in to comment.