Add denylist import (#17)
- As per https://github.com/go-gitea/gitea/pull/18222#issuecomment-1008386067 Reviewed-on: https://gitea.com/gitea/gitea-vet/pulls/17 Reviewed-by: techknowlogick <techknowlogick@gitea.io> Reviewed-by: Norwin <noerw@noreply.gitea.io> Co-authored-by: Gusted <williamzijl7@hotmail.com> Co-committed-by: Gusted <williamzijl7@hotmail.com>
This commit is contained in:
parent
7c98703580
commit
791434a6a7
|
@ -0,0 +1,36 @@
|
|||
// Copyright 2022 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package checks
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"golang.org/x/tools/go/analysis"
|
||||
)
|
||||
|
||||
var (
|
||||
deniedImports = []string{"io/ioutil"}
|
||||
DenylistImports = &analysis.Analyzer{
|
||||
Name: "denylist_imports",
|
||||
Doc: "check for denied imports",
|
||||
Run: runDenylistImports,
|
||||
}
|
||||
)
|
||||
|
||||
func runDenylistImports(pass *analysis.Pass) (interface{}, error) {
|
||||
for _, file := range pass.Files {
|
||||
for _, im := range file.Imports {
|
||||
val := im.Path.Value
|
||||
val = strings.TrimPrefix(val, `"`)
|
||||
val = strings.TrimSuffix(val, `"`)
|
||||
for _, deniedImport := range deniedImports {
|
||||
if deniedImport == val {
|
||||
pass.Reportf(im.Path.Pos(), `"`+deniedImport+"\" is not allowed to be imported")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil, nil
|
||||
}
|
Loading…
Reference in New Issue