diff --git a/checks/denylisted-imports.go b/checks/denylisted-imports.go new file mode 100644 index 0000000..969ef4b --- /dev/null +++ b/checks/denylisted-imports.go @@ -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 +} diff --git a/main.go b/main.go index 5d4193d..52cd30f 100644 --- a/main.go +++ b/main.go @@ -12,6 +12,7 @@ import ( func main() { unitchecker.Main( + checks.DenylistImports, checks.Imports, checks.License, checks.Migrations,