diff options
| author | Asko Nõmm <asko@nmm.ee> | 2025-10-08 20:48:49 +0300 |
|---|---|---|
| committer | Asko Nõmm <asko@nmm.ee> | 2025-10-08 20:48:49 +0300 |
| commit | 29c5b153843a29d5e0a7acf4fc78cd00a2c4f02f (patch) | |
| tree | ce1035969acc5ba4e5973f1a850a348bf48fa32a | |
| parent | 64792f47f54708f9ec46ea7da691ce21981b9605 (diff) | |
Fix CLJS nodes test
| -rw-r--r-- | resources/clj-kondo/config.edn | 6 | ||||
| -rw-r--r-- | resources/clj-kondo/hooks/dompa.clj | 10 | ||||
| -rw-r--r-- | src/dompa/nodes.clj | 26 | ||||
| -rw-r--r-- | src/dompa/nodes.cljs | 24 | ||||
| -rw-r--r-- | src/dompa/nodes/shared.cljc (renamed from src/dompa/nodes.cljc) | 43 | ||||
| -rw-r--r-- | test/dompa/nodes_test.cljc | 6 |
6 files changed, 64 insertions, 51 deletions
diff --git a/resources/clj-kondo/config.edn b/resources/clj-kondo/config.edn index 1fc2855..8ee11e4 100644 --- a/resources/clj-kondo/config.edn +++ b/resources/clj-kondo/config.edn @@ -1,5 +1,5 @@ {:hooks {:analyze-call {dompa.nodes/$ hooks.dompa/$ - dompa.nodes/defhtml hooks.dompa/defhtml}} + dompa.nodes.shared/defhtml hooks.dompa/defhtml}} :linters {:dompa.nodes/$-arg-validation {:level :warning} - :dompa.nodes/defhtml-arg-validation {:level :warning}} - :lint-as {dompa.nodes/defhtml clojure.core/defn}}
\ No newline at end of file + :dompa.nodes.shared/defhtml-arg-validation {:level :warning}} + :lint-as {dompa.nodes.shared/defhtml clojure.core/defn}} diff --git a/resources/clj-kondo/hooks/dompa.clj b/resources/clj-kondo/hooks/dompa.clj index 24597ef..dac5762 100644 --- a/resources/clj-kondo/hooks/dompa.clj +++ b/resources/clj-kondo/hooks/dompa.clj @@ -20,7 +20,7 @@ :message (str "Invalid argument type. When creating a text node, " "only literal values (strings, numbers and symbols) " "are allowed.") - :type :dompa.templates/$-arg-validation)))) + :type :dompa.nodes/$-arg-validation)))) ; if the first arg is a keyword, then the second argument can only be ; a sequence or a map. @@ -30,8 +30,8 @@ (api/list-node? (first rest-args))))) (api/reg-finding! (assoc (meta (first rest-args)) - :message (str "Invalid argument type. Argument must be a sequence or a map.") - :type :dompa.templates/$-arg-validation)) + :message "Invalid argument type. Argument must be a sequence or a map." + :type :dompa.nodes/$-arg-validation)) ; if the first arg is a keyword, the second arg is a list, then ; every arg has to be a list node. @@ -43,7 +43,7 @@ (assoc (meta arg) :message (str "Invalid argument type. Argument must be a $ macro " "or a sequence of $ macros.") - :type :dompa.templates/$-arg-validation))) + :type :dompa.nodes/$-arg-validation))) ; if the first arg is a keyword, the second arg is a map, then from ; the second forwards everything has to be a list node @@ -54,7 +54,7 @@ (assoc (meta (second rest-args)) :message (str "Invalid argument type. Argument must be a $ macro " "or a sequence of $ macros.") - :type :dompa.templates/$-arg-validation))))) + :type :dompa.nodes/$-arg-validation))))) (defn defhtml [{:keys [node]}] (let [[_ first-arg second-arg & rest-args] (:children node)] diff --git a/src/dompa/nodes.clj b/src/dompa/nodes.clj new file mode 100644 index 0000000..53020ba --- /dev/null +++ b/src/dompa/nodes.clj @@ -0,0 +1,26 @@ +(ns dompa.nodes) + +(defmacro $ + "Creates a new node. Particularly useful + where you need compile-time composition over run-time, like when + combined with the `defhtml` macro to create HTML string outputs. + + Usage: + + ```clojure + ($ :div + ($ \"hello world\" )) + ```" + [name & opts] + `(if (string? ~name) + {:node/name :dompa/text + :node/value (str ~name ~@opts)} + (let [opts# (list ~@opts) + first-opt# (first opts#) + attrs?# (and (map? first-opt#) + (not (contains? first-opt# :node/name))) + attrs# (if attrs?# first-opt# {}) + children# (if attrs?# (rest opts#) opts#)] + (cond-> {:node/name ~name} + attrs?# (assoc :node/attrs attrs#) + (seq children#) (assoc :node/children children#))))) diff --git a/src/dompa/nodes.cljs b/src/dompa/nodes.cljs new file mode 100644 index 0000000..8e19e2f --- /dev/null +++ b/src/dompa/nodes.cljs @@ -0,0 +1,24 @@ +(ns dompa.nodes) + +(defn $ + "Creates a new node + + Usage: + + ```clojure + ($ :div + ($ \"hello world\" )) + ```" + [name & opts] + (if (string? name) + {:node/name :dompa/text + :node/value (apply str name opts)} + (let [first-opt (first opts) + attrs? (and (map? first-opt) + (not (contains? first-opt :node/name))) + attrs (if attrs? first-opt {}) + children (if attrs? (rest opts) opts)] + (cond-> {:node/name name} + attrs? (assoc :node/attrs attrs) + (seq children) (assoc :node/children children))))) + diff --git a/src/dompa/nodes.cljc b/src/dompa/nodes/shared.cljc index 2453b88..b876048 100644 --- a/src/dompa/nodes.cljc +++ b/src/dompa/nodes/shared.cljc @@ -1,4 +1,4 @@ -(ns dompa.nodes) +(ns dompa.nodes.shared) (def ^:private default-void-nodes #{:!doctype :area :base :br :col :embed :hr :img :input @@ -92,43 +92,4 @@ [name & args-and-elements] (let [[args & elements] args-and-elements] `(defn ~name ~args - (->html (vector ~@elements))))) - -(defn $->flat-xf [] - (fn [rf] - (letfn [(step [result input] - (if (sequential? input) - (reduce step result input) - (rf result input)))] - (fn - ([] (rf)) - ([result] (rf result)) - ([result input] (step result input)))))) - -(defn $->flat [children] - (into [] ($->flat-xf) children)) - -(defmacro $ - "Creates a new node. Particularly useful - where you need compile-time composition over run-time, like when - combined with the `defhtml` macro to create HTML string outputs. - - Usage: - - ```clojure - ($ :div - ($ \"hello world\" )) - ```" - [name & opts] - `(if (string? ~name) - {:node/name :dompa/text - :node/value (str ~name ~@opts)} - (let [opts# (list ~@opts) - first-opt# (first opts#) - attrs?# (and (map? first-opt#) - (not (contains? first-opt# :node/name))) - attrs# (if attrs?# first-opt# {}) - children# (if attrs?# (rest opts#) opts#)] - (cond-> {:node/name ~name} - attrs?# (assoc :node/attrs attrs#) - (seq children#) (assoc :node/children ($->flat children#)))))) + (->html [~@elements])))) diff --git a/test/dompa/nodes_test.cljc b/test/dompa/nodes_test.cljc index 8544d51..eb86c7a 100644 --- a/test/dompa/nodes_test.cljc +++ b/test/dompa/nodes_test.cljc @@ -1,9 +1,11 @@ (ns dompa.nodes-test #?(:clj (:require [clojure.test :refer [deftest is testing]] - [dompa.nodes :refer [$ defhtml traverse ->html]] + [dompa.nodes :refer [$]] + [dompa.nodes.shared :refer [defhtml traverse ->html]] [dompa.html :as html])) #?(:cljs (:require [cljs.test :refer-macros [deftest testing is]] - [dompa.nodes :refer [$ defhtml traverse ->html]] + [dompa.nodes :refer [$]] + [dompa.nodes.shared :refer [traverse ->html] :refer-macros [defhtml]] [dompa.html :as html]))) (defhtml hello [who] |
