Skip to content

Instantly share code, notes, and snippets.

@borkdude
Last active January 20, 2023 09:13
Show Gist options
  • Save borkdude/841d85d5ad04c517337166b3928697bd to your computer and use it in GitHub Desktop.
Save borkdude/841d85d5ad04c517337166b3928697bd to your computer and use it in GitHub Desktop.
Babashka script to find var usages in current project
#!/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
(require '[babashka.pods :as pods])
(pods/load-pod 'borkdude/clj-kondo "2021.02.13")
(ns find-var
(:require [clojure.edn :as edn]
[pod.borkdude.clj-kondo :as clj-kondo]))
(defn find-references [{:keys [:var :lint]}]
(let [analysis (-> (clj-kondo/run! {:config {:output {:analysis true}}
:lint 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