feat: check for dependency level
This commit is contained in:
parent
2b1561217f
commit
26c845daad
|
@ -0,0 +1,53 @@
|
|||
// Copyright 2023 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package checks
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"golang.org/x/tools/go/analysis"
|
||||
)
|
||||
|
||||
var Dependencies = &analysis.Analyzer{
|
||||
Name: "dependencies",
|
||||
Doc: "check for dependency level",
|
||||
Run: runDependencies,
|
||||
}
|
||||
|
||||
func runDependencies(pass *analysis.Pass) (interface{}, error) {
|
||||
level := getLevel(pass.Pkg.Path())
|
||||
if level == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
for _, file := range pass.Files {
|
||||
for _, im := range file.Imports {
|
||||
if getLevel(im.Path.Value) > level {
|
||||
pass.Reportf(im.Pos(), "import %v", im.Path.Value)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// See https://docs.gitea.io/zh-cn/guidelines-backend/#package-dependencies
|
||||
var dependencyLevels = map[string]int{
|
||||
"code.gitea.io/gitea/cmd": 5,
|
||||
"code.gitea.io/gitea/routers": 4,
|
||||
"code.gitea.io/gitea/services": 3,
|
||||
"code.gitea.io/gitea/models": 2,
|
||||
"code.gitea.io/gitea/modules": 1,
|
||||
}
|
||||
|
||||
func getLevel(pkg string) int {
|
||||
pkg = strings.Trim(pkg, "\"")
|
||||
if strings.HasSuffix(pkg, "/test") || strings.HasSuffix(pkg, "_test") {
|
||||
return 0
|
||||
}
|
||||
for k, v := range dependencyLevels {
|
||||
if strings.HasPrefix(pkg, k) {
|
||||
return v
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
Loading…
Reference in New Issue