Fork me on GitHub
#clj-kondo
<
2022-08-13
>
jumar06:08:15

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)))

borkdude07:08:42

This is currently not part of the analysis

jumar16:08:45

Good to know, thanks for the confirmation. Could this be a useful future addition?

borkdude17:08:35

We could document this snippet perhaps, as a first step

👍 1
borkdude17:08:33

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

jumar18:08:45

Posted, thanks.

pinkfrog08:08:51

Maybe more related to the developer of meander, but I wonder why meander does not add this

:linters {:unresolved-symbol {:exclude [(meander.epsilon/match)
                                         (meander.epsilon/find)
                                         (meander.epsilon/search)
                                         (meander.epsilon/rewrite)]}}
to the library by default.