- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 1.5k
Add Depguard to supported linters #47
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| package golinters | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add a test file to  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can't run test locally... https://github.com/golangci/golangci-lint/blob/master/pkg/enabled_linters_test.go#L93 GOROOT does not have a test directory for me. Am I missing something? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Found it. I use Fedora and the golang package that I installed strips out those files... | ||
|  | ||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "strings" | ||
|  | ||
| depguardAPI "github.com/OpenPeeDeeP/depguard" | ||
| "github.com/golangci/golangci-lint/pkg/result" | ||
| ) | ||
|  | ||
| type Depguard struct{} | ||
|  | ||
| func (Depguard) Name() string { | ||
| return "depguard" | ||
| } | ||
|  | ||
| func (Depguard) Desc() string { | ||
| return "Go linter that checks if package imports are in a list of acceptable packages" | ||
| } | ||
|  | ||
| func (d Depguard) Run(ctx context.Context, lintCtx *Context) ([]result.Issue, error) { | ||
| dg := &depguardAPI.Depguard{ | ||
| Packages: lintCtx.Settings().Depguard.Packages, | ||
| IncludeGoRoot: lintCtx.Settings().Depguard.IncludeGoRoot, | ||
| } | ||
| listType := lintCtx.Settings().Depguard.ListType | ||
| var found bool | ||
| dg.ListType, found = depguardAPI.StringToListType[strings.ToLower(listType)] | ||
| if !found { | ||
| if listType != "" { | ||
| return nil, fmt.Errorf("unsure what list type %s is", listType) | ||
| } | ||
| dg.ListType = depguardAPI.LTBlacklist | ||
| } | ||
|  | ||
| issues, err := dg.Run(lintCtx.LoaderConfig, lintCtx.Program) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if len(issues) == 0 { | ||
| return nil, nil | ||
| } | ||
| msgSuffix := "is in the blacklist" | ||
| if dg.ListType == depguardAPI.LTWhitelist { | ||
| msgSuffix = "is not in the whitelist" | ||
| } | ||
| res := make([]result.Issue, 0, len(issues)) | ||
| for _, i := range issues { | ||
| res = append(res, result.Issue{ | ||
| Pos: i.Position, | ||
| Text: fmt.Sprintf("%s %s", formatCode(i.PackageName, lintCtx.Cfg), msgSuffix), | ||
| FromLinter: d.Name(), | ||
| }) | ||
| } | ||
| return res, nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package testdata | ||
|  | ||
| import ( | ||
| "log" // ERROR "`log` is in the blacklist" | ||
| ) | ||
|  | ||
| func SpewDebugInfo() { | ||
| log.Println("Debug info") | ||
| } | 
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,6 +1,6 @@ | ||
| package testdata | ||
|  | ||
| import "log" | ||
| import "log" // nolint:depguard | ||
|  | ||
| func Unconvert() { | ||
| a := 1 | ||
|  | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add it to thanks section please