main.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. package main
  2. import (
  3. "log"
  4. "io/ioutil"
  5. "os"
  6. "path/filepath"
  7. "regexp"
  8. "strings"
  9. sitter "github.com/smacker/go-tree-sitter"
  10. "github.com/smacker/go-tree-sitter/ruby"
  11. )
  12. const NUM_WORKERS = 32
  13. func gatherFiles(root string, ch chan string) {
  14. defer close(ch)
  15. i := 0
  16. err := filepath.Walk(root,
  17. func(path string, info os.FileInfo, err error) error {
  18. if err != nil {
  19. log.Printf("Error reading directory %s: %s\n", root, err)
  20. return nil
  21. }
  22. i += 1
  23. name := info.Name()
  24. if strings.HasSuffix(name, ".rb") {
  25. ch <- path
  26. }
  27. return nil
  28. })
  29. log.Printf("Checked %d files\n", i)
  30. if err != nil {
  31. log.Fatalf("Error reading directory %s: %s\n", root, err)
  32. return
  33. }
  34. }
  35. func findTypedLine(path string, tree *sitter.Tree, buf []byte) *string {
  36. r := regexp.MustCompile("# typed: (ignore|false|true|strict|strong|__STDLIB_INTERNAL)")
  37. root := tree.RootNode()
  38. for i := 0; i < int(root.ChildCount()); i++ {
  39. n := root.Child(i)
  40. if n.Type() == "comment" {
  41. matches := r.FindStringSubmatch(n.Content(buf))
  42. if matches == nil {
  43. continue
  44. }
  45. return &matches[1]
  46. }
  47. }
  48. return nil
  49. }
  50. func worker(ch chan string, finished chan int) {
  51. parser := sitter.NewParser()
  52. parser.SetLanguage(ruby.GetLanguage())
  53. for {
  54. path, more := <-ch
  55. if !more {
  56. finished <- -1
  57. return
  58. }
  59. buf, err := ioutil.ReadFile(path)
  60. if err != nil {
  61. log.Printf("Error reading %s: %s\n", path, err)
  62. continue
  63. }
  64. node := parser.Parse(nil, buf)
  65. sigil := findTypedLine(path, node, buf)
  66. if sigil == nil {
  67. log.Printf("File %s: missing typed sigil", path)
  68. } else if *sigil == "false" || *sigil == "ignore" {
  69. log.Printf("File %s: expected at least `true`, but got `%s`", path, *sigil)
  70. }
  71. finished <- 1
  72. }
  73. }
  74. func main() {
  75. ch := make(chan string)
  76. go gatherFiles(".", ch)
  77. finished := make(chan int)
  78. for i := 0; i < NUM_WORKERS; i++ {
  79. go worker(ch, finished)
  80. }
  81. all_files := 0
  82. done := 0
  83. for done < NUM_WORKERS {
  84. n := <- finished
  85. if n == -1 {
  86. done += 1
  87. } else {
  88. all_files += 1
  89. }
  90. }
  91. log.Printf("Parsed %d files\n", all_files)
  92. }