rewrite-clj

2024-10-21T15:32:36.042579Z

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

borkdude 2024-10-21T15:35:48.763279Z

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

2024-10-21T15:36:28.366319Z

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

2024-10-21T15:36:37.301759Z

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

2024-10-21T15:40:03.550179Z

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

2024-10-21T15:40:42.461779Z

just nice to nil pun

borkdude 2024-10-21T15:53:16.767329Z

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

👍 1
➕ 1
borkdude 2024-10-21T15:53:19.953049Z

in user space

borkdude 2024-10-21T15:53:26.770409Z

if it's not a library

borkdude 2024-10-21T15:54:00.431549Z

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

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

2024-10-21T16:00:17.838329Z

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

lread 2024-10-21T18:53:04.384849Z

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)"

2024-10-21T18:53:35.915349Z

a wrapper is a smart idae

2024-10-21T18:53:46.176929Z

i was surprised that it wasn't the default behavior

lread 2024-10-21T18:56:28.024069Z

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
2024-10-21T18:56:50.427679Z

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

borkdude 2024-10-21T19:03:15.165939Z

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

1
🪝 1