@lee sorry if that was a torrent of stuff. i did manage to eliminate a fair number of calls to sexpr by using the following:
(defn string-value
"Return the string value for a node."
[node]
(:lines node))
(defn string-node?
"Returns true if node represents a string, else false."
[node]
(string? (string-value node)))
(defn symbol-value
"Return the symbol value for a node."
[node]
(:value node))
(defn symbol-node?
"Returns true if node represents a symbol, else false."
[node]
(symbol? (symbol-value node)))
there are still a few files to go. we'll see how it goes 🙂that’s great @sogaiu, I continue to be very interested in your sexpr adventures!
@lee after our conversation yesterday (about matching API changes over versions) I figured clj-kondo can just expose this information as a linter. Someone else asked for exposing the usages of a var or keyword. It could maybe also expose the found arities of public functions. Feel free to chime in here: https://github.com/borkdude/clj-kondo/issues/234
interesting @borkdude, thanks! I’ll take a look
@lee i have nearly eliminated all unsafe uses of sexpr locally. in one case, i "sanitized" a zipper before using sexpr on it. specifically, given a bindings vector (as a zipper) like:
[x 1
{:keys [alice bob]} a-map
[top bottom] a-vector]
i wanted to end up with just the names:
(x alice bob top bottom)
previously, i was using sexpr on the bindings vector zipper before handing it off to a function to determine just the names, but if i understand what you said about safety correctly, the init-exprs (i.e. 1, a-map, a-vector) are potentially unsafe for sexpr consumption in some situations (e.g. if one of them was a rational and the code was running in cljs).
so in this case since the init-exprs are not necessary anyway, i substituted 0 for each init-expr:
[x 0
{:keys [alice bob]} 0
[top bottom] 0]
before calling sexpr. does this seem to address the safety issue to you?
i have one more usage left that i got stuck on before going to sleep, but perhaps will be luckier today...@sogaiu fwiw, I have a very similar function like that in clj-kondo
it produces a map of bindings and their locations. it doesn’t use sexpr: https://github.com/borkdude/clj-kondo/blob/179fdcf64cdb8b48032cdc5cb2bdf8635495c79d/src/clj_kondo/impl/analyzer.clj#L46
@borkdude thanks for the tip -- will take a look
btw, are you familiar w/ the destructure function in clojure's core.clj?
I have looked/played with it
user=> (destructure '[[x y z] [1 2 3]])
[vec__6 [1 2 3] x (clojure.core/nth vec__6 0 nil) y (clojure.core/nth vec__6 1 nil) z (clojure.core/nth vec__6 2 nil)]
@borkdude it's going to take a while for me to get through extract-bindings in detail 🙂 so far i noticed it calls lift-meta-content and that looks like it can lead to a call to sexpr: https://github.com/borkdude/clj-kondo/blob/179fdcf64cdb8b48032cdc5cb2bdf8635495c79d/src/clj_kondo/impl/metadata.clj#L18 does that seem right?