Fork me on GitHub
#clj-kondo
<
2023-07-11
>
Brandon Stubbs19:07:59

Hey, while using the analysis api on the :var-definitions there is an option to get the arglists as a string. What would be the best way to get the definition sexp as a string, or is this out of scope?

borkdude20:07:07

just parse it as EDN, I'd say

Brandon Stubbs20:07:39

Just to check we are on the same page: Say I have the file foo.clj

(ns foo)

(defn foo
  []
  (inc 1))
I run analysis on this and I get:
[{:arglist-strs ["[]"]
  :col 1
  :defined-by clojure.core/defn
  :defined-by->lint-as clojure.core/defn
  :end-col 11
  :end-row 5
  :filename "/Users/brandonstubbs/projects/x/src/foo.clj"
  :fixed-arities #{0}
  :name foo
  :name-col 7
  :name-end-col 10
  :name-end-row 3
  :name-row 3
  :ns foo
  :row 3}]
In order to get "(defn foo\n []\n (inc 1))" parse it as edn for that?

borkdude20:07:31

Ah sorry, the entire definition. My recommendation is to use rewrite-clj and iterate over the nodes, until the positions match up. And then just use str on the node, this will give you the definition.

borkdude20:07:39

here is a similar function, but then the opposite: it removes definitions from the original code https://github.com/borkdude/carve/blob/cb621317ae1582869c8bd8f1a47cf57a3598d803/src/carve/impl.clj#L118

Brandon Stubbs20:07:06

Ok great thank you I will do that, is this something you do not want in scope of this lib?

borkdude20:07:35

not in scope for clj-kondo. it throws away all whitespace so it doesn't even know the original node anymore

Brandon Stubbs20:07:03

I would prefer the whitespace removed, what I am looking for is a quick way to say has this definition changed since this file was analysed

borkdude20:07:08

why not just look at the substrings based on the entire file

borkdude20:07:17

you don't even need rewrite-clj for this

Brandon Stubbs20:07:34

Busy writing something that does a bit of tree shaking for building, similar to turbopack in the js world. But on second thought, I think whitespace should matter as if someone adds a newline that’s important

borkdude20:07:08

ok, then you can just focus on the lines and columns that clj-kondo reports and check the contents in the file

borkdude20:07:23

just using raw string comparison

Brandon Stubbs20:07:06

Yeah that will actually be great! Thanks for the discussion 😄