rewrite-clj 2024-10-21

how do you do conditional items in a list node? (n/list-node [(n/token-node 'a) (when foo (n/token-node 'b))]) doesn't work

(n/list-node (cond-> [...] foo (conj ...)))
? it's just clojure after all? ;)

maybe i should have used an actual example, there's stuff on both sides.

using cond-> works, it's just significantly more cumbersome

compare with str/join that treats nil as empty strings, so you can write (str/join [a (when foo b) c]) and it'll produce "helloworld" instead of "hellonilworld" or something else

just nice to nil pun

you could maybe extend nil to implement a no-op node or something

👍 1
➕ 1

in user space

if it's not a library

maybe we can invent rewrite-clj/hiccup where you can write:

[:list 1 2 (when-not foo [:set 4 5 6]))
;)

I used n/coerce for a bit, but i like controlling whitespace

Full examples are helpful @nbtheduke (especially for my old slow brain). From my REPL:

(require '[rewrite-clj.node :as n])

(def foo nil)

(str (n/list-node [(n/token-node 'a) (when foo (n/token-node 'b))]))
;; => Execution error (IllegalArgumentException) at rewrite-clj.node.protocols/eval11223$fn$G (protocols.cljc:9).
;;    No implementation of method: :string of protocol: #'rewrite-clj.node.protocols/Node found for class: nil
Does that show the problem you are hitting? I guess this boils down to:
(n/string nil)
;; => Execution error (IllegalArgumentException) at rewrite-clj.node.protocols/eval11223$fn$G (protocols.cljc:9).
;;    No implementation of method: :string of protocol: #'rewrite-clj.node.protocols/Node found for class: nil
Couldn't you just write a wee wrapper for n/list-node to suit your usage?
(defn noah-list-node [children]
  (n/list-node (keep identity children)))

(str (noah-list-node [(n/token-node 'a) (when foo (n/token-node 'b))]))
;; => "(a)"

a wrapper is a smart idae

i was surprised that it wasn't the default behavior

Cool, if the wrapper does the trick, that'd be great. I guess nobody stumbled on this as surprising before you! Which must mean you are exploring interesting usages of rewrite-clj! simple_smile

😅 1

or i'm running face first into a series of walls

many people have gone before you when writing clj-kondo hooks ;)

1
🪝 1