Add "encoding/json" to denylist (#18)
- "encoding/json" should be avoided as we got a json module for that which get the correct json library when specified. - Allow to exemptions to the denylist (e.g. the json module need to import this package). Reviewed-on: https://gitea.com/gitea/gitea-vet/pulls/18 Reviewed-by: Lunny Xiao <xiaolunwen@gmail.com> Reviewed-by: Andrew Thornton <art27@cantab.net> Co-authored-by: Gusted <williamzijl7@hotmail.com> Co-committed-by: Gusted <williamzijl7@hotmail.com>
This commit is contained in:
parent
791434a6a7
commit
48ebc90254
|
@ -11,7 +11,7 @@ import (
|
|||
)
|
||||
|
||||
var (
|
||||
deniedImports = []string{"io/ioutil"}
|
||||
deniedImports = []string{"io/ioutil", "encoding/json"}
|
||||
DenylistImports = &analysis.Analyzer{
|
||||
Name: "denylist_imports",
|
||||
Doc: "check for denied imports",
|
||||
|
@ -27,7 +27,18 @@ func runDenylistImports(pass *analysis.Pass) (interface{}, error) {
|
|||
val = strings.TrimSuffix(val, `"`)
|
||||
for _, deniedImport := range deniedImports {
|
||||
if deniedImport == val {
|
||||
pass.Reportf(im.Path.Pos(), `"`+deniedImport+"\" is not allowed to be imported")
|
||||
// Allow a exemption when there is a comment 'Allow "package_name" import'
|
||||
allowed := false
|
||||
for _, comment := range file.Comments {
|
||||
if strings.Contains(comment.Text(), "Allow \""+val+"\" import") {
|
||||
allowed = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !allowed {
|
||||
pass.Reportf(im.Path.Pos(), `"`+deniedImport+"\" is not allowed to be imported")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue