Skip to content

Instantly share code, notes, and snippets.

@xlfe
Forked from borkdude/find-var.clj
Last active January 20, 2023 09:15
Show Gist options
  • Save xlfe/dd02ee50a9af95b0de4e1909c69f6f58 to your computer and use it in GitHub Desktop.
Save xlfe/dd02ee50a9af95b0de4e1909c69f6f58 to your computer and use it in GitHub Desktop.
Babashka script to find var usages in current project (using installed clj-kondo instead of clj-kondo pod)
#!/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*)})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment