mirror of
https://github.com/mgechev/revive.git
synced 2025-11-23 22:04:49 +02:00
optimizes import-blacklist (#123)
This commit is contained in:
@@ -2,7 +2,6 @@ package rule
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"go/ast"
|
|
||||||
|
|
||||||
"github.com/mgechev/revive/lint"
|
"github.com/mgechev/revive/lint"
|
||||||
)
|
)
|
||||||
@@ -13,6 +12,11 @@ type ImportsBlacklistRule struct{}
|
|||||||
// Apply applies the rule to given file.
|
// Apply applies the rule to given file.
|
||||||
func (r *ImportsBlacklistRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
|
func (r *ImportsBlacklistRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
|
||||||
var failures []lint.Failure
|
var failures []lint.Failure
|
||||||
|
|
||||||
|
if file.IsTest() {
|
||||||
|
return failures // skip, test file
|
||||||
|
}
|
||||||
|
|
||||||
blacklist := make(map[string]bool, len(arguments))
|
blacklist := make(map[string]bool, len(arguments))
|
||||||
|
|
||||||
for _, arg := range arguments {
|
for _, arg := range arguments {
|
||||||
@@ -20,25 +24,25 @@ func (r *ImportsBlacklistRule) Apply(file *lint.File, arguments lint.Arguments)
|
|||||||
if !ok {
|
if !ok {
|
||||||
panic(fmt.Sprintf("Invalid argument to the imports-blacklist rule. Expecting a string, got %T", arg))
|
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] != '"' {
|
if len(argStr) > 2 && argStr[0] != '"' && argStr[len(argStr)-1] != '"' {
|
||||||
argStr = fmt.Sprintf(`"%s"`, argStr)
|
argStr = fmt.Sprintf(`"%s"`, argStr)
|
||||||
}
|
}
|
||||||
blacklist[argStr] = true
|
blacklist[argStr] = true
|
||||||
}
|
}
|
||||||
|
|
||||||
fileAst := file.AST
|
for _, is := range file.AST.Imports {
|
||||||
walker := blacklistedImports{
|
path := is.Path
|
||||||
file: file,
|
if path != nil && blacklist[path.Value] {
|
||||||
fileAst: fileAst,
|
failures = append(failures, lint.Failure{
|
||||||
onFailure: func(failure lint.Failure) {
|
Confidence: 1,
|
||||||
failures = append(failures, failure)
|
Failure: "should not use the following blacklisted import: " + path.Value,
|
||||||
},
|
Node: is,
|
||||||
blacklist: blacklist,
|
Category: "imports",
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ast.Walk(walker, fileAst)
|
|
||||||
|
|
||||||
return failures
|
return failures
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -46,24 +50,3 @@ func (r *ImportsBlacklistRule) Apply(file *lint.File, arguments lint.Arguments)
|
|||||||
func (r *ImportsBlacklistRule) Name() string {
|
func (r *ImportsBlacklistRule) Name() string {
|
||||||
return "imports-blacklist"
|
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,
|
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,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user