add configuration files for clj-kondo + rum defc

pull/1524/head
Martin Klepsch 2021-03-21 10:42:55 +01:00 committed by Tienson Qin
parent b154c9e0f5
commit b1149309df
3 changed files with 78 additions and 1 deletions

9
.clj-kondo/config.edn Normal file
View File

@ -0,0 +1,9 @@
{:linters {:unresolved-symbol {:exclude [goog.DEBUG]}}
:hooks {:analyze-call {rum.core/defc hooks.rum/defc
rum.core/defcs hooks.rum/defcs}}
:lint-as {promesa.core/let clojure.core/let
garden.def/defstyles clojure.core/def
garden.def/defkeyframes clojure.core/def
rum.core/defcc rum.core/defc
clojure.test.check.clojure-test/defspec clojure.core/def
clojure.test.check.properties/for-all clojure.core/for}}

68
.clj-kondo/hooks/rum.clj Normal file
View File

@ -0,0 +1,68 @@
(ns hooks.rum
(:require [clj-kondo.hooks-api :as api]))
(defn fn-body? [x]
(and (seq? x)
(vector? (first x))))
(defn rewrite-body [mixins body defcs?]
(if defcs?
(let [[binding-vec & body] (:children body)
[state-arg & rest-args] (:children binding-vec)
;; the original vector without the state argument
fn-args (assoc binding-vec :children rest-args)
body (api/list-node
(list* (api/token-node 'let*)
(api/vector-node [state-arg (api/token-node nil)])
state-arg
(concat mixins body)))
body (api/list-node [fn-args body])]
body)
(let [[binding-vec & body] (:children body)]
(api/list-node (cons binding-vec (concat mixins body))))))
(defn rewrite
([node] (rewrite node false))
([node defcs?]
(let [args (rest (:children node))
component-name (first args)
?docstring (when (string? (api/sexpr (second args)))
(second args))
args (if ?docstring
(nnext args)
(next args))
bodies
(loop [args* (seq args)
mixins []
bodies []]
(if args*
(let [a (first args*)
a-sexpr (api/sexpr a)]
(cond (vector? a-sexpr) ;; a-sexpr is a binding vec and the rest is the body of the function
[(rewrite-body mixins (api/list-node args*) defcs?)]
(fn-body? a-sexpr)
(recur (next args*)
mixins
(conj bodies (rewrite-body mixins a defcs?)))
;; assume mixin
:else (recur (next args*)
(conj mixins a)
bodies)))
bodies))
new-node (with-meta
(api/list-node
(list* (api/token-node 'defn)
component-name
(if ?docstring
(cons ?docstring bodies)
bodies)))
(meta node))]
new-node)))
(defn defc [{:keys [:node]}]
(let [new-node (rewrite node)]
{:node new-node}))
(defn defcs [{:keys [:node]}]
(let [new-node (rewrite node true)]
{:node new-node}))

2
.gitignore vendored
View File

@ -29,5 +29,5 @@ strings.csv
.calva
resources/electron.js
.clj-kondo/
.clj-kondo/.cache
.lsp/