Skip to content

Commit

Permalink
add .clj-kondo directory to make ornament's defprops, defrules, defst…
Browse files Browse the repository at this point in the history
…yled work well with clj-kondo
  • Loading branch information
humorless committed Oct 15, 2024
1 parent f90dd55 commit 2e87ae9
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .clj-kondo/config.edn
Original file line number Diff line number Diff line change
@@ -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}}}
42 changes: 42 additions & 0 deletions .clj-kondo/hooks/ornament.clj
Original file line number Diff line number Diff line change
@@ -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})))
4 changes: 2 additions & 2 deletions deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 28 additions & 2 deletions repl-sessions/ornament_poke.clj
Original file line number Diff line number Diff line change
@@ -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"}])
Expand All @@ -22,7 +48,7 @@
(def active? true)

(o/defstyled wrapper :section
[my-compo ]
[my-compo]
([]
[my-compo "a" "b"
[:h1.title "xxxx"]]))
Expand All @@ -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")))

0 comments on commit 2e87ae9

Please sign in to comment.