arohner / scriptjure

a Clojure library for generating javascript
187 stars 15 forks source link

js* does not work if only followed by a single expression #17

Open habruening opened 3 months ago

habruening commented 3 months ago

This library seems to be very practicable for server based rendering. I don't know if it is still maintained. I think, I found a bug.

This example does not work correctly.

(let [world "World!"
      world-as-fragment (js* (clj (str "Hello " world)))] 
  (js (console.log (clj world-as-fragment))))

It should generate the js code console.log("Hello World"). But it does not resolve the clj macro and yields console.log(clj(\"Hello \" + world)).

habruening commented 3 months ago

I think, it has something to do with the quasiquote macro. It does not resolve the clj, because it is not in an inner expression.

Interestingly this workaround can be used.

(let [world "World!"
      world-as-fragment (js* [(clj (str "Hello " world))])] 
  (js (console.log (aget (clj world-as-fragment) 1))))
habruening commented 3 months ago

Perhaps the problem is not much relevant in practice. Instead of (js* (clj ..)) we can write (clj ...). I am not sure if this always works. But it looks so. And situations with more complex expressions are not affected by the problem.