large_vars.clj 2.0 KB

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