mirror of
https://github.com/mgechev/revive.git
synced 2024-11-28 08:49:11 +02:00
optimizes import-blacklist (#123)
This commit is contained in:
parent
2474f6cecb
commit
e8c1baf8ac
@ -2,7 +2,6 @@ package rule
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go/ast"
|
||||
|
||||
"github.com/mgechev/revive/lint"
|
||||
)
|
||||
@ -13,6 +12,11 @@ type ImportsBlacklistRule struct{}
|
||||
// Apply applies the rule to given file.
|
||||
func (r *ImportsBlacklistRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
|
||||
var failures []lint.Failure
|
||||
|
||||
if file.IsTest() {
|
||||
return failures // skip, test file
|
||||
}
|
||||
|
||||
blacklist := make(map[string]bool, len(arguments))
|
||||
|
||||
for _, arg := range arguments {
|
||||
@ -20,25 +24,25 @@ func (r *ImportsBlacklistRule) Apply(file *lint.File, arguments lint.Arguments)
|
||||
if !ok {
|
||||
panic(fmt.Sprintf("Invalid argument to the imports-blacklist rule. Expecting a string, got %T", arg))
|
||||
}
|
||||
// we add quotes if nt present, because when parsed, the value of the AST node, will be quoted
|
||||
// we add quotes if not present, because when parsed, the value of the AST node, will be quoted
|
||||
if len(argStr) > 2 && argStr[0] != '"' && argStr[len(argStr)-1] != '"' {
|
||||
argStr = fmt.Sprintf(`"%s"`, argStr)
|
||||
}
|
||||
blacklist[argStr] = true
|
||||
}
|
||||
|
||||
fileAst := file.AST
|
||||
walker := blacklistedImports{
|
||||
file: file,
|
||||
fileAst: fileAst,
|
||||
onFailure: func(failure lint.Failure) {
|
||||
failures = append(failures, failure)
|
||||
},
|
||||
blacklist: blacklist,
|
||||
for _, is := range file.AST.Imports {
|
||||
path := is.Path
|
||||
if path != nil && blacklist[path.Value] {
|
||||
failures = append(failures, lint.Failure{
|
||||
Confidence: 1,
|
||||
Failure: "should not use the following blacklisted import: " + path.Value,
|
||||
Node: is,
|
||||
Category: "imports",
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
ast.Walk(walker, fileAst)
|
||||
|
||||
return failures
|
||||
}
|
||||
|
||||
@ -46,24 +50,3 @@ func (r *ImportsBlacklistRule) Apply(file *lint.File, arguments lint.Arguments)
|
||||
func (r *ImportsBlacklistRule) Name() string {
|
||||
return "imports-blacklist"
|
||||
}
|
||||
|
||||
type blacklistedImports struct {
|
||||
file *lint.File
|
||||
fileAst *ast.File
|
||||
onFailure func(lint.Failure)
|
||||
blacklist map[string]bool
|
||||
}
|
||||
|
||||
func (w blacklistedImports) Visit(_ ast.Node) ast.Visitor {
|
||||
for _, is := range w.fileAst.Imports {
|
||||
if is.Path != nil && !w.file.IsTest() && w.blacklist[is.Path.Value] {
|
||||
w.onFailure(lint.Failure{
|
||||
Confidence: 1,
|
||||
Failure: fmt.Sprintf("should not use the following blacklisted import: %s", is.Path.Value),
|
||||
Node: is,
|
||||
Category: "imports",
|
||||
})
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
@ -14,3 +14,13 @@ func TestImportsBlacklist(t *testing.T) {
|
||||
Arguments: args,
|
||||
})
|
||||
}
|
||||
|
||||
func BenchmarkImportsBlacklist(b *testing.B) {
|
||||
args := []interface{}{"crypto/md5", "crypto/sha1"}
|
||||
var t *testing.T
|
||||
for i := 0; i <= b.N; i++ {
|
||||
testRule(t, "imports-blacklist", &rule.ImportsBlacklistRule{}, &lint.RuleConfig{
|
||||
Arguments: args,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user