rewrite-clj

pithyless 2021-11-01T07:16:55.045700Z

@nbtheduke that reminds me of this post from CircleCI - before rewrite-clj was released :) https://circleci.com/blog/rewriting-your-test-suite-in-clojure-in-24-hours/

🎉 1
lread 2021-11-01T12:02:30.049100Z

Interesting @pithyless! > There was a long delay between actually rewriting the test suite, and the publishing of this blog post. In the meantime, https://github.com/xsc/rewrite-clj has been released. I haven’t used it, but it looks like exactly what we were missing.

2021-11-01T13:24:04.049500Z

that’s a great blog post! i’d forgotten about it

2021-11-01T15:27:36.050100Z

i’m using the node API cuz it’s more my style, and I’m struggling a bit when a node has metadata:

(<token: deftest> <whitespace: " "> <meta:
  ^{:card-title "15-minutes"}
    fifteen-minutes
>)

2021-11-01T15:29:20.050500Z

compared to a test that doesn’t have metadata: (<token: deftest> <whitespace: " "> <token: above-the-law>)

2021-11-01T15:30:23.051800Z

do i just have to check n/inner? on the deftest name to see if it has metadata and then (somehow) extract the symbol?

2021-11-01T15:31:51.052300Z

and then when i run n/children, i get back [<map: {:card-title "15-minutes"}> <newline: "\n"> <whitespace: " "> <token: fifteen-minutes>]

2021-11-01T15:37:07.052900Z

if this means i have to write a helper function to extract the symbol, then okay i guess

borkdude 2021-11-01T15:45:52.054700Z

@nbtheduke This is one minor thing that I don't find that intuitive about rewrite-clj. What I've done in clj-kondo is make a function which turns this upside down: a meta node is a child of the other node. I can get away with this because clj-kondo doesn't write code to text. Yes, a helper function is the answer I think.

borkdude 2021-11-01T15:46:15.055200Z

This tripped up clj-kondo badly in the beginning because metadata can be pretty much anywhere so you have to do these checks a lot.

2021-11-01T15:46:28.055400Z

coolio

lread 2021-11-01T16:23:33.056300Z

@nbtheduke if you are interested, we https://github.com/clj-commons/rewrite-clj/issues/115.

👍 1
2021-11-01T16:24:16.056800Z

i’ve subscribed so i can track it!

2021-11-01T16:33:57.058100Z

with the zipper api, the location is a point within the whole parsed text so at any point you can move forward and backwards. that doesn’t seem possible with the node API. have i missed something?

borkdude 2021-11-01T16:34:53.058400Z

Good old times with @sogaiu :)

❤️ 1
borkdude 2021-11-01T16:35:45.059100Z

@nbtheduke yes, a zipper location and moving that location (cursor if you will ) around is only something in the zipper API

2021-11-01T16:36:12.059300Z

rough, okay

2021-11-01T20:53:37.061Z

okay, switched to zipper api and while it’s more awkward, it’s a lot more powerful

2021-11-01T20:55:06.062700Z

i have filtered all of the testing branches from inside my tests so they’re in a list:

(defn get-testing-branches [zloc]
  (->> zloc
       (iterate z/right)
       (take 100)
       (filter #(testing? (z/down %)))
       doall))
which i remove and set aside with (let [testing-branches (get-testing-branches zloc) zloc (remove-testing-branches zloc)] …)

2021-11-01T20:58:02.064Z

now i want to insert these into the zipper after the deftests. (z/up zloc) gets me to the top-level, and then (z/insert-right ???) to insert a given testing block into the top level file?

2021-11-01T21:07:50.064600Z

if I just use (first testing-branches), i get something like:

(deftest ^{:card-title "15-minutes"}
    fifteen-minutes
    ;; 15 Minutes - check if it works correctly from both sides
  ) ^{:zip/branch? #function[rewrite-clj.node.protocols/eval50757/fn--50758/G--50742--50763], :zip/children #function[clojure.core/comp/fn--5529], :zip/make-node #function[rewrite-clj.node.protocols/eval50757/fn--50791/G--50746--50798], :rewrite-clj.zip/opts {:track-position? false, :auto-resolve #function[rewrite-clj.node.protocols/default-auto-resolve]}} [(testing "in corp score area"
...

2021-11-01T21:08:42.065500Z

idk how to convert a seq of zlocs to nodes i can insert into the document. everything in the node namespace seems to be about manually creating them

2021-11-01T21:08:52.065700Z

my apologies for the flurry of messages/spam lol

lread 2021-11-01T22:53:36.066300Z

It rarely snows here @nbtheduke, so your flurry is most welcome!

🌨️ 1
lread 2021-11-01T22:58:32.068700Z

So, if I understand here… you’ve saved a bunch of zipper locations and now you want to insert them as nodes to specific spots. You can always get the node at zipper location with rewrite-clj.zip/node. Does that help?

lread 2021-11-01T23:04:57.070700Z

I just re-read your use case @nbtheduke : > i’m considering using rewrite-clj to rewrite/rearrange my test suite. it’s currently written in the philosophy of “few `deftest`s, many `testing` branches”, which makes running specific branches hard. i’m looking to pluck each `testing` branch out of the deftests and then place them in a new `deftest` with some name. would this be possible with rewrite-clj? I’ve been meaning to beef up the examples included in the user guide. This sounds like a good one to eventually write up (so, thanks for your question!)

lread 2021-11-01T23:06:51.071400Z

I guess it might get a little bit trickier if you have nested testings…

2021-11-01T23:21:13.074Z

I’ll check out zip.node, I must have missed it. Thankfully, I don’t want to recurse to find nested testing blocks, we’ve been careful about keeping the top-level testing blocks as complete units. Maybe once I’m done and have this working, I’ll show my code and we can write up a longer guide to demonstrate!

2021-11-01T23:25:08.076600Z

Something I noticed is that insert-right and the related functions use item as the argument name does the object to be placed. Without examples or further description, I didn’t/don’t know what kind of object item is supposed to be, and my small experiments weren’t fruitful.

2021-11-01T23:25:55.077600Z

(I purposefully didn’t dive into the source cuz that can easily dovetail into reading the whole code base lol)

lread 2021-11-01T23:38:30.079100Z

Thanks for the great feedback on the vagueness of item! I’ll make an issue to update this and similarly vague docstrings.

lread 2021-11-01T23:46:33.080Z

Oh I didn’t answer your question about item… If it is not already a rewrite-clj node, rewrite-clj will attempt to coerce it to one.

lread 2021-11-01T23:47:07.080500Z

Except it won’t attempt to coerce a zloc to a node, as you’ve noticed!

👍 1