Skip to content

Instantly share code, notes, and snippets.

@PEZ
Created February 18, 2024 08:06
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/1ac56f00b8e9e622dca2e78c66d4922a to your computer and use it in GitHub Desktop.
Save PEZ/1ac56f00b8e9e622dca2e78c66d4922a to your computer and use it in GitHub Desktop.
A clj-kondo hook for Dumdom components `.clj-kondo/hooks/dumdom.clj`
{:linters {:dumdom/component-options {:level :warning}}
:hooks {:analyze-call {dumdom.core/defcomponent hooks.dumdom/defcomponent}}}
(comment ; add-libs
'{:deps {clj-kondo/clj-kondo {:mvn/version "2024.02.12"}}})
(ns hooks.dumdom
(:require [clj-kondo.hooks-api :as api]))
(def valid-opts #{:on-mount
:on-update
:on-render
:on-unmount
:will-appear
:did-appear
:will-enter
:did-enter
:will-leave
:did-leave
:name})
(defn- extract-docstr
[[docstr? & forms :as remaining-forms]]
(if (api/string-node? docstr?)
[docstr? forms]
[(api/string-node "no docs") remaining-forms]))
(defn- extract-opts
([forms]
(extract-opts forms []))
([[k v & forms :as remaining-forms] opts]
(if (api/keyword-node? k)
(do
(when-not (valid-opts (api/sexpr k))
(api/reg-finding! (assoc (meta k)
:message (str "Invalid option: `" k "`")
:type :dumdom/component-options)))
(extract-opts forms (into opts [k v])))
[(api/map-node opts) remaining-forms])))
(defn ^:export defcomponent [{:keys [node]}]
(let [[name & forms] (rest (:children node))
[docstr forms] (extract-docstr forms)
[opts forms] (extract-opts forms)
new-node (api/list-node
(list*
(api/token-node 'defn)
name
docstr
opts
forms))]
{:node new-node}))
(comment
(def code (str '(defcomponent heading
""
:on-render (fn [dom-node val old-val])
:invalid (fn [])
[data]
(def data data)
[:h2 {:style {:background :black
:color :white}}
(pr-str (:text data))])))
(defcomponent {:node (api/parse-string code)})
(require '[clj-kondo.core :as clj-kondo])
(:findings (with-in-str (str "(require '[dumdom.core :refer [defcomponent]])"
" "
code)
(clj-kondo/run! {:lint ["-"]})))
:rcf)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment