-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
129 additions
and
272 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,200 +1,46 @@ | ||
package golinters | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"sync" | ||
|
||
"github.com/OpenPeeDeeP/depguard" | ||
"github.com/OpenPeeDeeP/depguard/v2" | ||
"golang.org/x/tools/go/analysis" | ||
"golang.org/x/tools/go/loader" //nolint:staticcheck // require changes in github.com/OpenPeeDeeP/depguard | ||
|
||
"github.com/golangci/golangci-lint/pkg/config" | ||
"github.com/golangci/golangci-lint/pkg/fsutils" | ||
"github.com/golangci/golangci-lint/pkg/golinters/goanalysis" | ||
"github.com/golangci/golangci-lint/pkg/lint/linter" | ||
"github.com/golangci/golangci-lint/pkg/result" | ||
) | ||
|
||
const depguardName = "depguard" | ||
|
||
func NewDepguard(settings *config.DepGuardSettings) *goanalysis.Linter { | ||
var mu sync.Mutex | ||
var resIssues []goanalysis.Issue | ||
|
||
analyzer := &analysis.Analyzer{ | ||
Name: depguardName, | ||
Doc: goanalysis.TheOnlyanalyzerDoc, | ||
Run: goanalysis.DummyRun, | ||
} | ||
|
||
return goanalysis.NewLinter( | ||
depguardName, | ||
"Go linter that checks if package imports are in a list of acceptable packages", | ||
[]*analysis.Analyzer{analyzer}, | ||
nil, | ||
).WithContextSetter(func(lintCtx *linter.Context) { | ||
dg, err := newDepGuard(settings) | ||
|
||
analyzer.Run = func(pass *analysis.Pass) (any, error) { | ||
if err != nil { | ||
return nil, err | ||
} | ||
conf := depguard.LinterSettings{} | ||
|
||
issues, errRun := dg.run(pass) | ||
if errRun != nil { | ||
return nil, errRun | ||
if settings != nil { | ||
for s, rule := range settings.Rules { | ||
list := &depguard.List{ | ||
Files: rule.Files, | ||
Allow: rule.Allow, | ||
} | ||
|
||
mu.Lock() | ||
resIssues = append(resIssues, issues...) | ||
mu.Unlock() | ||
|
||
return nil, nil | ||
} | ||
}).WithIssuesReporter(func(*linter.Context) []goanalysis.Issue { | ||
return resIssues | ||
}).WithLoadMode(goanalysis.LoadModeSyntax) | ||
} | ||
|
||
type depGuard struct { | ||
loadConfig *loader.Config | ||
guardians []*guardian | ||
} | ||
|
||
func newDepGuard(settings *config.DepGuardSettings) (*depGuard, error) { | ||
ps, err := newGuardian(settings) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
d := &depGuard{ | ||
loadConfig: &loader.Config{ | ||
Cwd: "", // fallbacked to os.Getcwd | ||
Build: nil, // fallbacked to build.Default | ||
}, | ||
guardians: []*guardian{ps}, | ||
} | ||
|
||
for _, additional := range settings.AdditionalGuards { | ||
add := additional | ||
ps, err = newGuardian(&add) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
d.guardians = append(d.guardians, ps) | ||
} | ||
|
||
return d, nil | ||
} | ||
|
||
func (d depGuard) run(pass *analysis.Pass) ([]goanalysis.Issue, error) { | ||
prog := goanalysis.MakeFakeLoaderProgram(pass) | ||
|
||
var resIssues []goanalysis.Issue | ||
for _, g := range d.guardians { | ||
issues, errRun := g.run(d.loadConfig, prog, pass) | ||
if errRun != nil { | ||
return nil, errRun | ||
} | ||
|
||
resIssues = append(resIssues, issues...) | ||
} | ||
|
||
return resIssues, nil | ||
} | ||
|
||
type guardian struct { | ||
*depguard.Depguard | ||
pkgsWithErrorMessage map[string]string | ||
} | ||
|
||
func newGuardian(settings *config.DepGuardSettings) (*guardian, error) { | ||
var ignoreFileRules []string | ||
for _, rule := range settings.IgnoreFileRules { | ||
ignoreFileRules = append(ignoreFileRules, fsutils.NormalizePathInRegex(rule)) | ||
} | ||
|
||
dg := &depguard.Depguard{ | ||
Packages: settings.Packages, | ||
IncludeGoRoot: settings.IncludeGoRoot, | ||
IgnoreFileRules: ignoreFileRules, | ||
} | ||
|
||
var err error | ||
dg.ListType, err = getDepGuardListType(settings.ListType) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// if the list type was a denylist the packages with error messages should be included in the denylist package list | ||
if dg.ListType == depguard.LTBlacklist { | ||
noMessagePackages := make(map[string]bool) | ||
for _, pkg := range dg.Packages { | ||
noMessagePackages[pkg] = true | ||
} | ||
// because of bug with Viper parsing (split on dot) we use a list of struct instead of a map. | ||
// https://github.com/spf13/viper/issues/324 | ||
// https://github.com/golangci/golangci-lint/issues/3749#issuecomment-1492536630 | ||
|
||
for pkg := range settings.PackagesWithErrorMessage { | ||
if _, ok := noMessagePackages[pkg]; !ok { | ||
dg.Packages = append(dg.Packages, pkg) | ||
deny := map[string]string{} | ||
for _, r := range rule.Deny { | ||
deny[r.Pkg] = r.Desc | ||
} | ||
} | ||
} | ||
|
||
return &guardian{ | ||
Depguard: dg, | ||
pkgsWithErrorMessage: settings.PackagesWithErrorMessage, | ||
}, nil | ||
} | ||
|
||
func (g guardian) run(loadConfig *loader.Config, prog *loader.Program, pass *analysis.Pass) ([]goanalysis.Issue, error) { | ||
issues, err := g.Run(loadConfig, prog) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
res := make([]goanalysis.Issue, 0, len(issues)) | ||
|
||
for _, issue := range issues { | ||
res = append(res, | ||
goanalysis.NewIssue(&result.Issue{ | ||
Pos: issue.Position, | ||
Text: g.createMsg(issue.PackageName), | ||
FromLinter: depguardName, | ||
}, pass), | ||
) | ||
} | ||
|
||
return res, nil | ||
} | ||
|
||
func (g guardian) createMsg(pkgName string) string { | ||
msgSuffix := "is in the denylist" | ||
if g.ListType == depguard.LTWhitelist { | ||
msgSuffix = "is not in the allowlist" | ||
} | ||
list.Deny = deny | ||
|
||
var userSuppliedMsgSuffix string | ||
if g.pkgsWithErrorMessage != nil { | ||
userSuppliedMsgSuffix = g.pkgsWithErrorMessage[pkgName] | ||
if userSuppliedMsgSuffix != "" { | ||
userSuppliedMsgSuffix = ": " + userSuppliedMsgSuffix | ||
conf[s] = list | ||
} | ||
} | ||
|
||
return fmt.Sprintf("%s %s%s", formatCode(pkgName, nil), msgSuffix, userSuppliedMsgSuffix) | ||
} | ||
|
||
func getDepGuardListType(listType string) (depguard.ListType, error) { | ||
if listType == "" { | ||
return depguard.LTBlacklist, nil | ||
} | ||
|
||
listT, found := depguard.StringToListType[strings.ToLower(listType)] | ||
if !found { | ||
return depguard.LTBlacklist, fmt.Errorf("unsure what list type %s is", listType) | ||
a, err := depguard.NewAnalyzer(&conf) | ||
if err != nil { | ||
linterLogger.Fatalf("depguard: create analyzer: %v", err) | ||
} | ||
|
||
return listT, nil | ||
return goanalysis.NewLinter( | ||
a.Name, | ||
a.Doc, | ||
[]*analysis.Analyzer{a}, | ||
nil, | ||
).WithLoadMode(goanalysis.LoadModeSyntax) | ||
} |
Oops, something went wrong.