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:
Gusted 2022-01-11 09:43:43 +08:00 committed by Lunny Xiao
parent 7c98703580
commit 791434a6a7
2 changed files with 37 additions and 0 deletions

View File

@ -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
}

View File

@ -12,6 +12,7 @@ import (
func main() {
unitchecker.Main(
checks.DenylistImports,
checks.Imports,
checks.License,
checks.Migrations,