rewrite-clj

https://github.com/clj-commons/rewrite-clj
2021-02-20T15:19:21.000500Z

quack

borkdude 2021-02-20T15:20:49.001200Z

If it looks like a sexpr...

mauricio.szabo 2021-02-20T19:07:00.003100Z

Hi there! Just a question, is there a way that, given a specific clojure code, find all ocurrences of, for example, (js/require...) and rewrite all of then to something else? Something like walk/postwalk-replace but using the sexp as argument so I can customize what will be returned....

lread 2021-02-20T19:49:42.004400Z

Heya @mauricio.szabo! Have you tried rewrite-clj.zip/prewalk and rewrite-clj.zip/postwalk yet?

borkdude 2021-02-20T19:53:09.005100Z

@mauricio.szabo are you dealing with s-expressions or rewrite clj nodes? this is pretty easy using normal sexprs outside of rewrite-clj as well

borkdude 2021-02-20T19:53:49.005700Z

@mauricio.szabo but if you're dealing with rewrite-clj you can simply loop through all nodes and when you encounter the one you are looking for, replace it.

borkdude 2021-02-20T19:54:41.006Z

You can "loop" using z/next

borkdude 2021-02-20T19:55:16.006300Z

Example here (but then removing instead of replacing): https://github.com/borkdude/carve/blob/a3a5b941d4327127e36541bf7322b15b33260386/src/carve/impl.clj#L103-L136

mauricio.szabo 2021-02-20T21:47:44.007900Z

@lee, no, I just saw I was using an old version of rewrite-cljs that didn't have this namespace. Will try it, thanks!

mauricio.szabo 2021-02-20T21:48:30.008700Z

@borkdude I'm dealing with strings so, anything that works is good for me 😆

borkdude 2021-02-20T21:49:06.009100Z

@mauricio.szabo is this in the context of sci (because you asked something similar in #sci) - i.e. are you going to eval this with sci?

mauricio.szabo 2021-02-20T23:43:42.011Z

Yes, what I want to test is if it's possible to somehow detect every js/require that will be sent to SCI, convert it (with webpack's API) to a single file (or a string), and then eval everything in the process

mauricio.szabo 2021-02-20T23:56:56.011500Z

Oh, yeah, there's no rewrite-clj.zip/prewalk available for ClojureScript 😞