directives.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package lintcmd
  2. import (
  3. "strings"
  4. "honnef.co/go/tools/lintcmd/runner"
  5. )
  6. func parseDirectives(dirs []runner.SerializedDirective) ([]ignore, []diagnostic) {
  7. var ignores []ignore
  8. var diagnostics []diagnostic
  9. for _, dir := range dirs {
  10. cmd := dir.Command
  11. args := dir.Arguments
  12. switch cmd {
  13. case "ignore", "file-ignore":
  14. if len(args) < 2 {
  15. p := diagnostic{
  16. Diagnostic: runner.Diagnostic{
  17. Position: dir.NodePosition,
  18. Message: "malformed linter directive; missing the required reason field?",
  19. Category: "compile",
  20. },
  21. Severity: severityError,
  22. }
  23. diagnostics = append(diagnostics, p)
  24. continue
  25. }
  26. default:
  27. // unknown directive, ignore
  28. continue
  29. }
  30. checks := strings.Split(args[0], ",")
  31. pos := dir.NodePosition
  32. var ig ignore
  33. switch cmd {
  34. case "ignore":
  35. ig = &lineIgnore{
  36. File: pos.Filename,
  37. Line: pos.Line,
  38. Checks: checks,
  39. Pos: dir.DirectivePosition,
  40. }
  41. case "file-ignore":
  42. ig = &fileIgnore{
  43. File: pos.Filename,
  44. Checks: checks,
  45. }
  46. }
  47. ignores = append(ignores, ig)
  48. }
  49. return ignores, diagnostics
  50. }