1
0
mirror of https://github.com/mgechev/revive.git synced 2025-02-15 13:53:15 +02:00
revive/rule/imports.go

54 lines
1.0 KiB
Go
Raw Normal View History

2018-01-21 18:04:41 -08:00
package rule
2017-11-26 19:06:02 -08:00
import (
"go/ast"
2018-01-21 18:04:41 -08:00
"github.com/mgechev/revive/linter"
2017-11-26 19:06:02 -08:00
)
// ImportsRule lints given else constructs.
type ImportsRule struct{}
// Apply applies the rule to given file.
2018-01-21 18:04:41 -08:00
func (r *ImportsRule) Apply(file *linter.File, arguments linter.Arguments) []linter.Failure {
var failures []linter.Failure
2017-11-26 19:06:02 -08:00
2018-01-21 18:48:51 -08:00
fileAst := file.AST
2017-11-26 19:06:02 -08:00
walker := lintImports{
file: file,
fileAst: fileAst,
2018-01-21 18:04:41 -08:00
onFailure: func(failure linter.Failure) {
2017-11-26 19:06:02 -08:00
failures = append(failures, failure)
},
}
ast.Walk(walker, fileAst)
return failures
}
// Name returns the rule name.
func (r *ImportsRule) Name() string {
return "imports"
}
type lintImports struct {
2018-01-21 18:04:41 -08:00
file *linter.File
2017-11-26 19:06:02 -08:00
fileAst *ast.File
2018-01-21 18:04:41 -08:00
onFailure func(linter.Failure)
2017-11-26 19:06:02 -08:00
}
func (w lintImports) Visit(n ast.Node) ast.Visitor {
for _, is := range w.fileAst.Imports {
if is.Name != nil && is.Name.Name == "." && !isTest(w.file) {
2018-01-21 18:04:41 -08:00
w.onFailure(linter.Failure{
2017-11-26 19:06:02 -08:00
Confidence: 1,
Failure: "should not use dot imports",
Node: is,
})
}
}
return nil
}