Skip to content

Commit

Permalink
Merge pull request #52 from PEZ/add-clj-kondo-config
Browse files Browse the repository at this point in the history
Add clj-kondo hook and config for linting `defcomponent`
  • Loading branch information
cjohansen authored Feb 19, 2024
2 parents 4e7f31d + c186603 commit eebf107
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 1 deletion.
1 change: 1 addition & 0 deletions .clj-kondo/config.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{:config-paths ["../resources/clj-kondo.exports/cjohansen/dumdom"]}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ target
checkouts
.cpcache
cljs-test-runner-out
.clj-kondo/cjohansen/dumdom
13 changes: 13 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,19 @@ would `devcards.core`:
(my-dumdom-component {:value 0}))
```

## Linting

This library exports
[clj-kondo configuration](https://github.com/clj-kondo/clj-kondo/blob/master/doc/config.md#importing)
for linting the `defcomponent` macro. You may need to import the config. If you
are using clojure-lsp, this should happen automatically.

If you are not using clj-kondo, you could get away with using a `defcomponent`
macro that supports linting as `defn`. Like
[this one](https://gist.github.com/PEZ/357df3589dc49e83e49da77cb8943723). It
won't help you spot errors with using the wrong dumdom component options,
but at least will silence false positive warnings.

## Contribute

Feel free to report bugs and, even better, provide bug fixing pull requests!
Expand Down
2 changes: 1 addition & 1 deletion deps.edn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{:paths ["src"]
{:paths ["src" "resources"]
:deps {org.clojure/clojure {:mvn/version "1.10.3"}
org.clojure/clojurescript {:mvn/version "1.10.866"}}
:aliases {:dev {:extra-paths ["dev" "test" "dev-resources"]
Expand Down
2 changes: 2 additions & 0 deletions resources/clj-kondo.exports/cjohansen/dumdom/config.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{:linters {:dumdom/component-options {:level :warning}}
:hooks {:analyze-call {dumdom.core/defcomponent dumdom.defcomponent/defcomponent}}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
(comment ; add-libs
'{:deps {clj-kondo/clj-kondo {:mvn/version "2024.02.12"}}})

(ns dumdom.defcomponent
(: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)

0 comments on commit eebf107

Please sign in to comment.