Add denylist import #17

Merged
lunny merged 2 commits from :denylist into master 2022-01-11 01:43:43 +00:00
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,