Babashka script to find var usages in current project (using installed clj-kondo instead of clj-kondo pod)

Felix Barbalet
2023-01-20T09:13:36Z

find-var.clj

| #!/usr/bin/env bb
|
| ;; usage:
| ;; $ find-var.clj babashka.main/main src:test
|
| ;; example output:
| ;; src/babashka/main.clj:672:32
| ;; src/babashka/main.clj:673:22
| ;; src/babashka/main.clj:676:28
| ;; test/babashka/test_utils.clj:31:59
| ;; test/babashka/test_utils.clj:32:32
| ;; test/babashka/profile.clj:11:24
| ;; test/babashka/main_test.clj:115:33
|
| (ns find-var
| (:require
| [babashka.process :refer [sh]]
| [clojure.edn :as edn]))
|
| (defn clj-kondo-run!
| [lint]
| (->
| (sh ["clj-kondo" "--config" "{:output {:analysis true :format :edn}}" "--lint" lint])
| :out
| edn/read-string))
|
| (defn find-references [{:keys [:var :lint]}]
| (let [analysis (-> (clj-kondo-run! lint)
| :analysis)
| ns-to-find (symbol (namespace var))
| name-to-find (symbol (name var))
| usages (:var-usages analysis)]
| (doseq [{:keys [:to :name] :as u} usages]
| (when (and (= to ns-to-find)
| (= name name-to-find))
| (let [{:keys [:filename :row :col]} u]
| (println (str filename ":" row ":" col)))))))
|
| (find-references {:var (edn/read-string (first *command-line-args*))
| :lint (second *command-line-args*)})