You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dot-imports.go 1.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package rule
  2. import (
  3. "go/ast"
  4. "github.com/mgechev/revive/lint"
  5. )
  6. // DotImportsRule lints given else constructs.
  7. type DotImportsRule struct{}
  8. // Apply applies the rule to given file.
  9. func (r *DotImportsRule) Apply(file *lint.File, _ lint.Arguments) []lint.Failure {
  10. var failures []lint.Failure
  11. fileAst := file.AST
  12. walker := lintImports{
  13. file: file,
  14. fileAst: fileAst,
  15. onFailure: func(failure lint.Failure) {
  16. failures = append(failures, failure)
  17. },
  18. }
  19. ast.Walk(walker, fileAst)
  20. return failures
  21. }
  22. // Name returns the rule name.
  23. func (r *DotImportsRule) Name() string {
  24. return "dot-imports"
  25. }
  26. type lintImports struct {
  27. file *lint.File
  28. fileAst *ast.File
  29. onFailure func(lint.Failure)
  30. }
  31. func (w lintImports) Visit(_ ast.Node) ast.Visitor {
  32. for i, is := range w.fileAst.Imports {
  33. _ = i
  34. if is.Name != nil && is.Name.Name == "." && !w.file.IsTest() {
  35. w.onFailure(lint.Failure{
  36. Confidence: 1,
  37. Failure: "should not use dot imports",
  38. Node: is,
  39. Category: "imports",
  40. })
  41. }
  42. }
  43. return nil
  44. }