Fork me on GitHub
#rewrite-clj
<
2022-05-16
>
Ivar Refsdal11:05:58

I noticed a strange behaviour with z/assoc:

; As expected:
(-> (z/of-string "{}") (z/assoc :demo "string-value"))
=> [<map: {:demo "string-value"} > {:l [], :pnodes [<forms: {} >], :ppath nil, :r nil, :changed? true}]

; Quoting something gives a strange map:
(-> (z/of-string "{}") (z/assoc :demo "\"quoted\""))
=> [<map: {:demo "", quoted ""} > {:l [], :pnodes [<forms: {} >], :ppath nil, :r nil, :changed? true}]

; I would expect this:
(-> (z/of-string "{:demo \"\\\"quoted\\\"\" }"))
=> [<map: {:demo "\"quoted\""} > {:l [], :pnodes [<forms: {:demo "\"quoted\""} >], :ppath nil, :r nil}]
Or do I need to wrap the value to z/assoc in something? Edit1: Typos and: Thanks for a great library! Edit2: I'm using 1.0.767-alpha.

lread14:05:21

Hiya @ivar.refsdal thanks for your question! I’ll fire up a REPL soon and take a look.

❤️ 1
lread14:05:24

@ivar.refsdal Ok I’ll start with rerunning your example from a plain old clj repl.

user=> (require '[rewrite-clj.zip :as z])
nil
user=> (-> (z/of-string "{}") (z/assoc :demo "string-value"))
[<map: {:demo "string-value"}> {:l [], :pnodes [<forms: {}>], :ppath nil, :r nil, :changed? true}]
user=> (-> (z/of-string "{}") (z/assoc :demo "\"quoted\""))
[<map: {:demo ""quoted""}> {:l [], :pnodes [<forms: {}>], :ppath nil, :r nil, :changed? true}]
user=> (-> (z/of-string "{:demo \"\\\"quoted\\\"\" }"))
[<map: {:demo "\"quoted\"" }> {:l [], :pnodes [<forms: {:demo "\"quoted\"" }>], :ppath nil, :r nil}]
The first thing I’m noticing is that our outputs for (-> (z/of-string "{}") (z/assoc :demo "\"quoted\"")) are different. To make sure we are on the page, can you rerun on your side and see if there is a difference?

lread14:05:10

I’m gonna guess something formatted your output weird and that we are on the same page.

lread14:05:42

So, I think maybe the issue is that you are expecting strings to the same as reading from a file?

lread14:05:53

The string "string-value" is internally the string string-value The string "\"quote\"" is internally the string "quoted" And those are what will be seen by rewrite-clj.

lread14:05:38

I could be entirely missing your question though, lemme know if that makes any sense.

Ivar Refsdal18:05:39

You are right in that the formatted output is weird in some way. It also works on the console for me (but not in IntelliJ/Cursive). However, here is a related problem (?)/thing I don't understand:

(-> "{}" z/of-string (z/assoc :demo "here is a quote:\". End.") (z/root-string))
=> "{:demo \"here is a quote:\". End.\"}"
I also tried with the example from the user guide: https://github.com/clj-commons/rewrite-clj/blob/main/doc/01-user-guide.adoc#finding-elements-with-the-zip-api and replaced
(-> zloc-desc (z/replace "My first Project.") z/root-string println)
with
(-> zloc-desc (z/replace "Project with a quote: \". A quote.") z/root-string println)
It outputs:
(defproject my-project "0.1.0-SNAPSHOT"
              :description "Project with a quote: ". A quote.")
There is something wrong, no? (Or am I just confused maybe?)

Ivar Refsdal18:05:21

Thanks for your time and reply 🙂

Ivar Refsdal18:05:25

1.0.767-alpha does not do it properly (I think!):

$ clj -Sdeps '{:deps {rewrite-clj/rewrite-clj {:mvn/version "1.0.767-alpha"}}}' -e '(do
(require (quote [rewrite-clj.zip :as z])) 
(-> (z/of-string "{}") 
    (z/assoc :demo "and a quote: \". <- A quote.")
    (z/root-string)
    (println))
)'
WARNING: Implicit use of clojure.main with options is deprecated, use -M
{:demo "and a quote: ". <- A quote."}
whereas 0.6.1 does it correctly:
$ clj -Sdeps '{:deps {rewrite-clj/rewrite-clj {:mvn/version "0.6.1"}}}' -e '(do
(require (quote [rewrite-clj.zip :as z])) 
(-> (z/of-string "{}") 
    (z/assoc :demo "and a quote: \". <- A quote.")
    (z/root-string)
    (println))
)'
WARNING: Implicit use of clojure.main with options is deprecated, use -M
{:demo "and a quote: \". <- A quote."}

Ivar Refsdal18:05:38

Head has same behaviour as .767:

$ clj -Sdeps '{:deps {rewrite-clj/rewrite-clj {:git/url "" :sha "0bf3ee3ac625738f7fa3e5e163039e1bcf47fff2"}}}' -e '(do
(require (quote [rewrite-clj.zip :as z])) 
(-> (z/of-string "{}") 
    (z/assoc :demo "and a quote: \". <- A quote.")
    (z/root-string)
    (println))
)'
Cloning: 
Checking out:  at 0bf3ee3ac625738f7fa3e5e163039e1bcf47fff2
WARNING: Implicit use of clojure.main with options is deprecated, use -M
{:demo "and a quote: ". <- A quote."}

lread19:05:45

> Thanks for your time and reply 🙂 My pleasure!

lread19:05:46

Thanks for comparing against v0.6.1, that is interesting. I’m more confused, frankly, by 0.6.1 behaviour than 1.0.767 behaviour. I’ll take a deeper look.

lread19:05:25

So more narrowed down to the coercion, we can see the difference: v1

❯ clj -Sdeps '{:deps {rewrite-clj/rewrite-clj {:mvn/version "1.0.767-alpha"}}}'        
Clojure 1.11.1
user=> (require '[rewrite-clj.node :as n])
nil
user=> (n/coerce "hey \" man")
<token: "hey " man">
v0
❯ clj -Sdeps '{:deps {rewrite-clj/rewrite-clj {:mvn/version "0.6.1"}}}'        
Clojure 1.11.1
user=> (require '[rewrite-clj.node :as n])
nil
user=> (n/coerce "hey \" man")
<token: "hey \" man">
I’ll pinpoint what change made v1 do things differently.

lread20:05:36

@U04V15CAJ would you mind having a peek and sharing your opinion on the issue? My current feeling is that I unknowingly (at least I don’t remember) fixed an issue in v0.

borkdude20:05:51

@UE21H2HHD Will do later this week!

borkdude20:05:53

Isn't this just the printing behavior?

lread20:05:08

That’s what I thought at first, but am now convinced otherwise.

lread20:05:52

Looks like v0 effectively escaped " in strings when coercing.

lread20:05:54

Which seems wrong to me, but wanted a borkpinion.

borkdude 2
borkdude20:05:23

yeah, that seems wrong to me too

borkdude20:05:02

I would expect it to print <token: "hey \" man"> though

borkdude20:05:22

although, maybe the nodes don't use prn but println style ?

lread20:05:31

yeah nodes override default pr with a println style.

borkdude20:05:29

ok, in that case I would say v1 makes more sense

lread20:05:38

I thought so too.

lread20:05:05

I don’t know that v0 meant to do what it was doing there.

lread21:05:38

Update examples in https://github.com/clj-commons/rewrite-clj/issues/176 to use pr to make things clearer (even to me!). We are wrapping a string in " so we need to wrap any inner " (I’ve flip flopped and now think rewrite-clj v0 is correct).

borkdude21:05:01

yes, agreed

lread21:05:07

So @ivar.refsdal, it took my a while to get there, but I finally understand! Thanks so much for raising this, I’ll follow up with a fix sometime soon.

lread23:05:38

(and also thanks @U04V15CAJ, as usual!)

Ivar Refsdal17:05:55

Thanks for following up, and looking forward to a fix in the future 🙂

👍 1