rewrite-clj

https://github.com/clj-commons/rewrite-clj
2019-06-07T01:12:19.088900Z

@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 🙂

lread 2019-06-07T02:57:02.090400Z

that’s great @sogaiu, I continue to be very interested in your sexpr adventures!

borkdude 2019-06-07T11:46:47.091700Z

@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

lread 2019-06-07T11:52:50.093100Z

interesting @borkdude, thanks! I’ll take a look

2019-06-07T21:08:37.104400Z

@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...

borkdude 2019-06-07T21:11:13.104800Z

@sogaiu fwiw, I have a very similar function like that in clj-kondo

borkdude 2019-06-07T21:12:45.105400Z

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

2019-06-07T21:36:49.106100Z

@borkdude thanks for the tip -- will take a look

2019-06-07T21:38:51.106400Z

btw, are you familiar w/ the destructure function in clojure's core.clj?

borkdude 2019-06-07T21:39:48.106700Z

I have looked/played with it

borkdude 2019-06-07T21:40:50.106900Z

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

2019-06-07T23:13:29.108900Z

@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?