2018-01-22 04:04:41 +02:00
|
|
|
package rule
|
2017-11-27 05:06:02 +02:00
|
|
|
|
|
|
|
import (
|
2023-11-26 11:07:12 +02:00
|
|
|
"fmt"
|
2017-11-27 05:06:02 +02:00
|
|
|
"go/ast"
|
2023-11-26 11:07:12 +02:00
|
|
|
"sync"
|
2017-11-27 05:06:02 +02:00
|
|
|
|
2018-01-25 01:44:03 +02:00
|
|
|
"github.com/mgechev/revive/lint"
|
2017-11-27 05:06:02 +02:00
|
|
|
)
|
|
|
|
|
2018-01-24 05:13:02 +02:00
|
|
|
// DotImportsRule lints given else constructs.
|
2023-11-26 11:07:12 +02:00
|
|
|
type DotImportsRule struct {
|
|
|
|
sync.Mutex
|
|
|
|
allowedPackages allowPackages
|
|
|
|
}
|
2017-11-27 05:06:02 +02:00
|
|
|
|
|
|
|
// Apply applies the rule to given file.
|
2023-11-26 11:07:12 +02:00
|
|
|
func (r *DotImportsRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
|
|
|
|
r.configure(arguments)
|
|
|
|
|
2018-01-25 01:44:03 +02:00
|
|
|
var failures []lint.Failure
|
2017-11-27 05:06:02 +02:00
|
|
|
|
2018-01-22 04:48:51 +02:00
|
|
|
fileAst := file.AST
|
2017-11-27 05:06:02 +02:00
|
|
|
walker := lintImports{
|
|
|
|
file: file,
|
|
|
|
fileAst: fileAst,
|
2018-01-25 01:44:03 +02:00
|
|
|
onFailure: func(failure lint.Failure) {
|
2017-11-27 05:06:02 +02:00
|
|
|
failures = append(failures, failure)
|
|
|
|
},
|
2023-11-26 11:07:12 +02:00
|
|
|
allowPackages: r.allowedPackages,
|
2017-11-27 05:06:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ast.Walk(walker, fileAst)
|
|
|
|
|
|
|
|
return failures
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the rule name.
|
2022-04-10 11:55:13 +02:00
|
|
|
func (*DotImportsRule) Name() string {
|
2018-01-24 05:13:02 +02:00
|
|
|
return "dot-imports"
|
2017-11-27 05:06:02 +02:00
|
|
|
}
|
|
|
|
|
2023-11-26 11:07:12 +02:00
|
|
|
func (r *DotImportsRule) configure(arguments lint.Arguments) {
|
|
|
|
r.Lock()
|
|
|
|
defer r.Unlock()
|
|
|
|
|
|
|
|
if r.allowedPackages != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
r.allowedPackages = make(allowPackages)
|
|
|
|
if len(arguments) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
args, ok := arguments[0].(map[string]any)
|
|
|
|
if !ok {
|
|
|
|
panic(fmt.Sprintf("Invalid argument to the dot-imports rule. Expecting a k,v map, got %T", arguments[0]))
|
|
|
|
}
|
|
|
|
|
|
|
|
if allowedPkgArg, ok := args["allowedPackages"]; ok {
|
|
|
|
if pkgs, ok := allowedPkgArg.([]any); ok {
|
|
|
|
for _, p := range pkgs {
|
|
|
|
if pkg, ok := p.(string); ok {
|
|
|
|
r.allowedPackages.add(pkg)
|
|
|
|
} else {
|
|
|
|
panic(fmt.Sprintf("Invalid argument to the dot-imports rule, string expected. Got '%v' (%T)", p, p))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
panic(fmt.Sprintf("Invalid argument to the dot-imports rule, []string expected. Got '%v' (%T)", allowedPkgArg, allowedPkgArg))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-27 05:06:02 +02:00
|
|
|
type lintImports struct {
|
2023-11-26 11:07:12 +02:00
|
|
|
file *lint.File
|
|
|
|
fileAst *ast.File
|
|
|
|
onFailure func(lint.Failure)
|
|
|
|
allowPackages allowPackages
|
2017-11-27 05:06:02 +02:00
|
|
|
}
|
|
|
|
|
2018-09-23 00:27:22 +02:00
|
|
|
func (w lintImports) Visit(_ ast.Node) ast.Visitor {
|
2023-08-30 08:06:58 +02:00
|
|
|
for _, is := range w.fileAst.Imports {
|
2023-11-26 11:07:12 +02:00
|
|
|
if is.Name != nil && is.Name.Name == "." && !w.allowPackages.isAllowedPackage(is.Path.Value) {
|
2018-01-25 01:44:03 +02:00
|
|
|
w.onFailure(lint.Failure{
|
2017-11-27 05:06:02 +02:00
|
|
|
Confidence: 1,
|
|
|
|
Failure: "should not use dot imports",
|
|
|
|
Node: is,
|
2018-01-24 05:13:02 +02:00
|
|
|
Category: "imports",
|
2017-11-27 05:06:02 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2023-11-26 11:07:12 +02:00
|
|
|
|
|
|
|
type allowPackages map[string]struct{}
|
|
|
|
|
|
|
|
func (ap allowPackages) add(pkg string) {
|
|
|
|
ap[fmt.Sprintf(`"%s"`, pkg)] = struct{}{} // import path strings are with double quotes
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ap allowPackages) isAllowedPackage(pkg string) bool {
|
|
|
|
_, allowed := ap[pkg]
|
|
|
|
return allowed
|
|
|
|
}
|