Skip to content

Instantly share code, notes, and snippets.

@PEZ
Created February 18, 2024 09:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PEZ/357df3589dc49e83e49da77cb8943723 to your computer and use it in GitHub Desktop.
Save PEZ/357df3589dc49e83e49da77cb8943723 to your computer and use it in GitHub Desktop.
A workaround to make default linting resolve `dumdom.core/defcomponent` without complaints.
(ns my-dumdom.macros
(:require [dumdom.core :as dumdom]))
(defn- extract-docstr
[[docstr? & forms]]
(if (string? docstr?)
[docstr? forms]
["" (cons docstr? forms)]))
(defn- extract-opts
([[argvec ?opts & forms]]
(assert (vector? argvec))
(if (map? ?opts)
[?opts (cons argvec forms)]
[{} (list* argvec ?opts forms)])))
(defmacro defcomponent
"A workaround to make default linting resolve `defcomponent` without complaints.
Instead of this:
(defcomponent Widget
\"A Widget\"
:on-mount #(...)
:on-render #(...)
[value constant-value]
(some-child-components))
We can write this:
(defcomponent Widget
[value constant-value]
{:name \"A Widget\"
:on-mount #(...)
:on-render #(...)}
(some-child-components))"
[name & forms]
(let [[docstr forms] (extract-docstr forms)
[options forms] (extract-opts forms)
[argvec & body] forms
options (merge {:name (str (:name (:ns &env)) "/" name)} options)]
`(def ~name ~docstr (dumdom.core/component (fn ~argvec ~@body) ~options))))
@PEZ
Copy link
Author

PEZ commented Feb 18, 2024

I'm not the author of this macro. Don't know who is, actually.

NB: Before using this macro, consider importing the clj-kondo linting config that ships with the dumdom library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment