1
0
mirror of https://github.com/mgechev/revive.git synced 2024-11-24 08:32:22 +02:00
revive/rule/dot-imports.go

55 lines
1.0 KiB
Go
Raw Normal View History

2018-01-22 04:04:41 +02:00
package rule
2017-11-27 05:06:02 +02:00
import (
"go/ast"
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.
type DotImportsRule struct{}
2017-11-27 05:06:02 +02:00
// Apply applies the rule to given file.
2022-04-10 11:55:13 +02:00
func (*DotImportsRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
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)
},
}
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
}
type lintImports struct {
2018-01-25 01:44:03 +02:00
file *lint.File
2017-11-27 05:06:02 +02:00
fileAst *ast.File
2018-01-25 01:44:03 +02:00
onFailure func(lint.Failure)
2017-11-27 05:06:02 +02:00
}
func (w lintImports) Visit(_ ast.Node) ast.Visitor {
2018-01-24 05:13:02 +02:00
for i, is := range w.fileAst.Imports {
_ = i
if is.Name != nil && is.Name.Name == "." && !w.file.IsTest() {
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
}