large_vars.clj 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #!/usr/bin/env bb
  2. (ns large-vars
  3. "This script detects vars that are too large and that make it difficult for
  4. the team to maintain and understand them."
  5. (:require [babashka.pods :as pods]
  6. [clojure.pprint :as pprint]
  7. [clojure.edn :as edn]
  8. [clojure.set :as set]))
  9. (pods/load-pod 'clj-kondo/clj-kondo "2022.02.09")
  10. (require '[pod.borkdude.clj-kondo :as clj-kondo])
  11. (def default-config
  12. ;; TODO: Discuss with team and agree on lower number
  13. {:max-lines-count 100
  14. ;; Vars with these metadata flags are allowed. Name should indicate the reason
  15. ;; it is allowed
  16. :metadata-exceptions #{::data-var
  17. ;; TODO: Address vars tagged with cleanup-todo. These
  18. ;; are left mostly because they are not high priority
  19. ;; or not well understood
  20. ::cleanup-todo}})
  21. (defn -main
  22. [args]
  23. (let [paths [(or (first args) "src")]
  24. config (or (some->> (second args) edn/read-string (merge default-config))
  25. default-config)
  26. {{:keys [var-definitions]} :analysis}
  27. (clj-kondo/run!
  28. {:lint paths
  29. :config {:output {:analysis {:var-definitions {:meta true
  30. :lang :cljs}}}}})
  31. vars (->> var-definitions
  32. (keep (fn [m]
  33. (let [lines-count (inc (- (:end-row m) (:row m)))]
  34. (when (and (> lines-count (:max-lines-count config))
  35. (empty? (set/intersection (set (keys (:meta m)))
  36. (:metadata-exceptions config))))
  37. {:var (:name m)
  38. :lines-count lines-count
  39. :filename (:filename m)}))))
  40. (sort-by :lines-count (fn [x y] (compare y x))))]
  41. (if (seq vars)
  42. (do
  43. (println (format "\nThe following vars exceed the line count max of %s:"
  44. (:max-lines-count config)))
  45. (pprint/print-table vars)
  46. (System/exit 1))
  47. (println "All vars are below the max size!"))))
  48. (when (= *file* (System/getProperty "babashka.file"))
  49. (-main *command-line-args*))