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.

imports-blacklist.go 1.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. package rule
  2. import (
  3. "fmt"
  4. "github.com/mgechev/revive/lint"
  5. )
  6. // ImportsBlacklistRule lints given else constructs.
  7. type ImportsBlacklistRule struct{}
  8. // Apply applies the rule to given file.
  9. func (r *ImportsBlacklistRule) Apply(file *lint.File, arguments lint.Arguments) []lint.Failure {
  10. var failures []lint.Failure
  11. if file.IsTest() {
  12. return failures // skip, test file
  13. }
  14. blacklist := make(map[string]bool, len(arguments))
  15. for _, arg := range arguments {
  16. argStr, ok := arg.(string)
  17. if !ok {
  18. panic(fmt.Sprintf("Invalid argument to the imports-blacklist rule. Expecting a string, got %T", arg))
  19. }
  20. // we add quotes if not present, because when parsed, the value of the AST node, will be quoted
  21. if len(argStr) > 2 && argStr[0] != '"' && argStr[len(argStr)-1] != '"' {
  22. argStr = fmt.Sprintf(`"%s"`, argStr)
  23. }
  24. blacklist[argStr] = true
  25. }
  26. for _, is := range file.AST.Imports {
  27. path := is.Path
  28. if path != nil && blacklist[path.Value] {
  29. failures = append(failures, lint.Failure{
  30. Confidence: 1,
  31. Failure: "should not use the following blacklisted import: " + path.Value,
  32. Node: is,
  33. Category: "imports",
  34. })
  35. }
  36. }
  37. return failures
  38. }
  39. // Name returns the rule name.
  40. func (r *ImportsBlacklistRule) Name() string {
  41. return "imports-blacklist"
  42. }