This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-08-13
Channels
- # babashka (3)
- # beginners (91)
- # calva (10)
- # cherry (1)
- # clj-commons (1)
- # clj-kondo (8)
- # clojure (19)
- # clojure-australia (1)
- # clojure-china (1)
- # clojure-europe (16)
- # clojure-filipino (1)
- # clojure-hk (1)
- # clojure-indonesia (1)
- # clojure-japan (1)
- # clojure-korea (1)
- # clojure-my (1)
- # clojure-norway (2)
- # clojure-sg (1)
- # clojure-taiwan (1)
- # cursive (12)
- # data-science (4)
- # datalevin (3)
- # emacs (16)
- # events (9)
- # hyperfiddle (1)
- # juxt (1)
- # lsp (2)
- # missionary (2)
- # music (1)
- # nbb (14)
- # off-topic (15)
- # pathom (5)
- # releases (1)
- # shadow-cljs (41)
- # sql (10)
- # squint (13)
- # vim (7)
- # xtdb (35)
Is it possible to get var usage form source code out of the box if I have kondo analysis result? I want something like this and I'm wondering if I need to code that manually or if there's a way to get it from kondo analysis by default:
(defn var-usages
"Finds usages of var with given name defined in given namespace.
Both `the-ns` and `var-name` should be symbols."
[kondo-analysis the-ns var-name]
(->> kondo-analysis
:var-usages
(filter #(and (= var-name (:name %))
(= the-ns (:to %))))))
(defn add-var-usage-source
[{:keys [row col end-row end-col filename] :as var-usage}]
(let [file-source (->> filename slurp str/split-lines)
matching-rows (subvec file-source (dec row) end-row)
matching-source (if (= 1 (count matching-rows))
(subs (first matching-rows) (dec col) (dec end-col))
(str/join
[(subs (first matching-rows) (dec col))
(subs (last matching-rows) 0 (dec end-col))]))]
(assoc var-usage :source matching-source)))
(defn var-usages-with-sources [kondo-analysis the-ns var-name]
(let [all-usages (var-usages kondo-analysis the-ns var-name)]
(map add-var-usage-source all-usages)))
I've added a wiki page here now: https://github.com/clj-kondo/clj-kondo/wiki/Analysis-tips-and-tricks Feel free to put it there