diff --git a/.clj-kondo/config.edn b/.clj-kondo/config.edn new file mode 100644 index 0000000..4585c52 --- /dev/null +++ b/.clj-kondo/config.edn @@ -0,0 +1,3 @@ +{:lint-as {lambdaisland.ornament/defprop clojure.core/def + lambdaisland.ornament/defrules clojure.core/def} + :hooks {:analyze-call {lambdaisland.ornament/defstyled hooks.ornament/defstyled}}} diff --git a/.clj-kondo/hooks/ornament.clj b/.clj-kondo/hooks/ornament.clj new file mode 100644 index 0000000..4927548 --- /dev/null +++ b/.clj-kondo/hooks/ornament.clj @@ -0,0 +1,42 @@ +(ns hooks.ornament + (:require [clj-kondo.hooks-api :as api])) + +(defn defstyled [{:keys [node]}] + (let [[class-name html-tag & more] (rest (:children node)) + _ (when-not (and (api/token-node? class-name) + (simple-symbol? (api/sexpr class-name))) + (api/reg-finding! {:row (:row (meta class-name)) + :col (:col (meta class-name)) + :message "Style name must be a symbol" + :type :lambdaisland.ornament/invalid-syntax})) + ; _ (prn :class-name class-name) + _ (when-not (api/keyword-node? html-tag) + (api/reg-finding! {:row (:row (meta html-tag)) + :col (:col (meta html-tag)) + :message "Html-tag must be a keyword" + :type :lambdaisland.ornament/invalid-syntax})) + ; _ (prn :html-tag html-tag) + ; _ (prn :more more) + fn-tag (first (drop-while (fn [x] + (or (api/keyword-node? x) + (api/map-node? x) + (api/vector-node? x))) + more)) + _ (prn :fn-tag fn-tag) + _ (when (or (api/list-node? fn-tag) + (nil? fn-tag)) + (api/reg-finding! {:row (:row (meta fn-tag)) + :col (:col (meta fn-tag)) + :message "fn-tag must be at least a list or nil" + :type :lambdaisland.ornament/invalid-syntax}))] + (if (api/list-node? fn-tag) + (let [[binding-vec & body] (:children fn-tag) + new-node (api/list-node + (list* + (api/token-node 'fn) + binding-vec + body))] + (prn :new-node (api/sexpr new-node)) + {:node new-node}) + ;; nil node + {:node true}))) diff --git a/deps.edn b/deps.edn index 1da640e..34c883d 100644 --- a/deps.edn +++ b/deps.edn @@ -40,8 +40,8 @@ :aliases {:dev - {:extra-paths ["dev"] - :extra-deps {}} + {:extra-paths ["dev" ".clj-kondo"] + :extra-deps {clj-kondo/clj-kondo {:mvn/version "LATEST"}}} :prod {:extra-deps diff --git a/repl-sessions/ornament_poke.clj b/repl-sessions/ornament_poke.clj index 485d0a1..7f86030 100644 --- a/repl-sessions/ornament_poke.clj +++ b/repl-sessions/ornament_poke.clj @@ -1,8 +1,34 @@ (ns repl-sessions.ornament-poke (:require + [clj-kondo.hooks-api :as api] [lambdaisland.ornament :as o] [lambdaisland.hiccup :as h])) +(comment + ;; This is for debugging clj-kondo hooks + ;; https://github.com/clj-kondo/clj-kondo/blob/master/doc/hooks.md + (load-file ".clj-kondo/hooks/ornament.clj")) + +(comment + (hooks.ornament/defstyled + {:node (api/parse-string + "(o/defstyled action-button :button + {:color \"red\"} + [:&:hover {:color \"black\"}]) + ")}) + + (hooks.ornament/defstyled + {:node (api/parse-string + "(o/defstyled action-button :button + {:color \"red\"} + [:&:hover {:color \"black\"}] + ([a b c] + [:<> + [:a {:class [action-button (when active? \"active\")]} a] + [:p.subtitle b] + [:p.content c]])) + ")})) + (o/defstyled action-button :button {:color "red"} [:&:hover {:color "black"}]) @@ -22,7 +48,7 @@ (def active? true) (o/defstyled wrapper :section - [my-compo ] + [my-compo] ([] [my-compo "a" "b" [:h1.title "xxxx"]])) @@ -33,6 +59,6 @@ [my-compo "a" "b" [:h1.title "xxxx"]]) -class="foo bar baz" +class= "foo bar baz" (str/join " " (cond-> ["foo" "bar" "baz"] active? (conj "active")))