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 f85fdaf commit 3faf3af
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 29 deletions.
24 changes: 19 additions & 5 deletions pkg/config/linters_exclusions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@ package config

import (
"fmt"
"slices"
)

const (
DefaultExclusionComments = "comments"
DefaultExclusionStdErrorHandling = "stdErrorHandling"
DefaultExclusionCommonFalsePositives = "commonFalsePositives"
DefaultExclusionLegacy = "legacy"
ExclusionPresetComments = "comments"
ExclusionPresetStdErrorHandling = "stdErrorHandling"
ExclusionPresetCommonFalsePositives = "commonFalsePositives"
ExclusionPresetLegacy = "legacy"
)

const excludeRuleMinConditionsCount = 2

type LinterExclusions struct {
Generated string `mapstructure:"generated"`
WarnUnused bool `mapstructure:"warn-unused"`
Default []string `mapstructure:"default"`
Presets []string `mapstructure:"preset"`
Rules []ExcludeRule `mapstructure:"rules"`
Paths []string `mapstructure:"paths"`
PathsExcept []string `mapstructure:"paths-except"`
Expand All @@ -29,6 +30,19 @@ func (e *LinterExclusions) Validate() error {
}
}

allPresets := []string{
ExclusionPresetComments,
ExclusionPresetStdErrorHandling,
ExclusionPresetCommonFalsePositives,
ExclusionPresetLegacy,
}

for _, preset := range e.Presets {
if !slices.Contains(allPresets, preset) {
return fmt.Errorf("invalid preset: %s", preset)
}
}

return nil
}

Expand Down
12 changes: 7 additions & 5 deletions pkg/config/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,14 @@ func (l *Loader) Load(opts LoadOptions) error {
l.cfg.Linters.LinterExclusions.Generated = cmp.Or(l.cfg.Issues.ExcludeGenerated, "strict")
}

// Compatibility layer with v1.
// TODO(ldez): should be removed in v2.
if l.cfg.Issues.UseDefaultExcludes {
l.cfg.Linters.LinterExclusions.Default = []string{
DefaultExclusionComments,
DefaultExclusionStdErrorHandling,
DefaultExclusionCommonFalsePositives,
DefaultExclusionLegacy,
l.cfg.Linters.LinterExclusions.Presets = []string{
ExclusionPresetComments,
ExclusionPresetStdErrorHandling,
ExclusionPresetCommonFalsePositives,
ExclusionPresetLegacy,
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package processors

import "github.com/golangci/golangci-lint/pkg/config"

var defaultLintersExclusions = map[string][]config.ExcludeRule{
config.DefaultExclusionComments: {
var linterExclusionPresets = map[string][]config.ExcludeRule{
config.ExclusionPresetComments: {
{
// Annoying issue about not having a comment. The rare codebase has such comments.
// CheckPackageComment, CheckExportedFunctionDocs, CheckExportedTypeDocs, CheckExportedVarDocs
Expand Down Expand Up @@ -50,7 +50,7 @@ var defaultLintersExclusions = map[string][]config.ExcludeRule{
},
},
},
config.DefaultExclusionStdErrorHandling: {
config.ExclusionPresetStdErrorHandling: {
{
// Almost all programs ignore errors on these functions and in most cases it's ok.
BaseRule: config.BaseRule{
Expand All @@ -61,7 +61,7 @@ var defaultLintersExclusions = map[string][]config.ExcludeRule{
},
},
},
config.DefaultExclusionCommonFalsePositives: {
config.ExclusionPresetCommonFalsePositives: {
{
// Too many false-positives on 'unsafe' usage.
BaseRule: config.BaseRule{
Expand All @@ -87,7 +87,7 @@ var defaultLintersExclusions = map[string][]config.ExcludeRule{
},
},
},
config.DefaultExclusionLegacy: {
config.ExclusionPresetLegacy: {
{
// Common false positives.
BaseRule: config.BaseRule{
Expand Down Expand Up @@ -125,11 +125,13 @@ var defaultLintersExclusions = map[string][]config.ExcludeRule{
},
}

func getDefaultLintersExclusions(names []string) []config.ExcludeRule {
func getLinterExclusionPresets(names []string) []config.ExcludeRule {
var rules []config.ExcludeRule

for _, name := range names {
rules = append(rules, defaultLintersExclusions[name]...)
if p, ok := linterExclusionPresets[name]; ok {
rules = append(rules, p...)
}
}

return rules
Expand Down
2 changes: 1 addition & 1 deletion pkg/result/processors/exclusion_rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func NewExclusionRules(log logutils.Log, files *fsutils.Files, cfg *config.Linte
}

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

p.rules = createExcludeRules(excludeRules, prefix)

Expand Down
22 changes: 11 additions & 11 deletions pkg/result/processors/path_relativity.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ var _ Processor = (*PathRelativity)(nil)
// PathRelativity computes [result.Issue.RelativePath] and [result.Issue.WorkingDirectoryRelativePath],
// based on the base path.
type PathRelativity struct {
log logutils.Log
basePath string
wd string
log logutils.Log
basePath string
workingDirectory string
}

func NewPathRelativity(log logutils.Log, basePath string) (*PathRelativity, error) {
Expand All @@ -26,12 +26,16 @@ func NewPathRelativity(log logutils.Log, basePath string) (*PathRelativity, erro
}

return &PathRelativity{
log: log.Child(logutils.DebugKeyPathRelativity),
basePath: basePath,
wd: wd,
log: log.Child(logutils.DebugKeyPathRelativity),
basePath: basePath,
workingDirectory: wd,
}, nil
}

func (*PathRelativity) Name() string {
return "path_relativity"
}

func (p *PathRelativity) Process(issues []result.Issue) ([]result.Issue, error) {
return transformIssues(issues, func(issue *result.Issue) *result.Issue {
newIssue := *issue
Expand All @@ -43,7 +47,7 @@ func (p *PathRelativity) Process(issues []result.Issue) ([]result.Issue, error)
return nil
}

newIssue.WorkingDirectoryRelativePath, err = filepath.Rel(p.wd, issue.FilePath())
newIssue.WorkingDirectoryRelativePath, err = filepath.Rel(p.workingDirectory, issue.FilePath())
if err != nil {
p.log.Warnf("Getting relative path (wd): %v", err)
return nil
Expand All @@ -53,8 +57,4 @@ func (p *PathRelativity) Process(issues []result.Issue) ([]result.Issue, error)
}), nil
}

func (*PathRelativity) Name() string {
return "path_relativity"
}

func (*PathRelativity) Finish() {}

0 comments on commit 3faf3af

Please sign in to comment.