large_vars.clj 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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.set :as set]))
  7. (pods/load-pod 'clj-kondo/clj-kondo "2021.12.19")
  8. (require '[pod.borkdude.clj-kondo :as clj-kondo])
  9. (def config
  10. ;; TODO: Discuss with team and agree on lower number
  11. {:max-lines-count 100
  12. ;; Vars with these metadata flags are allowed. Name should indicate the reason
  13. ;; it is allowed
  14. :metadata-exceptions #{::data-var}})
  15. (defn -main
  16. [args]
  17. (let [paths (or args ["src"])
  18. {{:keys [var-definitions]} :analysis}
  19. (clj-kondo/run!
  20. {:lint paths
  21. :config {:output {:analysis {:var-definitions {:meta true}}}}})
  22. vars (->> var-definitions
  23. (keep (fn [m]
  24. (let [lines-count (inc (- (:end-row m) (:row m)))]
  25. (when (and (> lines-count (:max-lines-count config))
  26. (empty? (set/intersection (set (keys (:meta m)))
  27. (:metadata-exceptions config))))
  28. {:var (:name m)
  29. :lines-count lines-count
  30. :filename (:filename m)}))))
  31. (sort-by :lines-count (fn [x y] (compare y x))))]
  32. (if (seq vars)
  33. (do (prn vars)
  34. (System/exit 1))
  35. (println "All vars are below the max size!"))))
  36. (when (= *file* (System/getProperty "babashka.file"))
  37. (-main *command-line-args*))