From 098b56d14fef2284fedc4a9d061fe3266b4ef26c Mon Sep 17 00:00:00 2001 From: Rajkumar Natarajan Date: Tue, 30 May 2023 13:47:07 -0700 Subject: [PATCH 1/2] Add clojure-kondo for mount.defstate --- src/clj-kondo.exports/mount/mount/config.edn | 1 + 1 file changed, 1 insertion(+) create mode 100644 src/clj-kondo.exports/mount/mount/config.edn diff --git a/src/clj-kondo.exports/mount/mount/config.edn b/src/clj-kondo.exports/mount/mount/config.edn new file mode 100644 index 0000000..3092d4c --- /dev/null +++ b/src/clj-kondo.exports/mount/mount/config.edn @@ -0,0 +1 @@ +{:lint-as {mount.core/defstate clojure.core/def}} \ No newline at end of file From 5df6c941f786fe5c6a4b7b4841e700a553437beb Mon Sep 17 00:00:00 2001 From: Rajkumar Natarajan Date: Sat, 2 Mar 2024 15:15:55 -0800 Subject: [PATCH 2/2] incorporate the review comments for .clj-kondo --- .../clj-kondo.exports/hooks/defstate.clj | 39 +++++++++++++++++++ .../clj-kondo.exports/mount/mount/config.edn | 3 ++ src/clj-kondo.exports/mount/mount/config.edn | 1 - 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 resources/clj-kondo.exports/hooks/defstate.clj create mode 100644 resources/clj-kondo.exports/mount/mount/config.edn delete mode 100644 src/clj-kondo.exports/mount/mount/config.edn diff --git a/resources/clj-kondo.exports/hooks/defstate.clj b/resources/clj-kondo.exports/hooks/defstate.clj new file mode 100644 index 0000000..f8e7aca --- /dev/null +++ b/resources/clj-kondo.exports/hooks/defstate.clj @@ -0,0 +1,39 @@ +(ns hooks.defstate + (:require [clj-kondo.hooks-api :as api])) + +(defn defstate [{:keys [:node]}] + (let [args (rest (:children node)) + args (if (string? (api/sexpr (first args))) + (next args) + args) + [n & args] args + m (when-let [m (first (:meta n))] + (api/sexpr m)) + m (if (map? m) m {}) + ks (cond-> (take 1 args) + (> (count args) 2) (conj (nth args 2))) + invalid-key (first (remove (comp (partial contains? #{:start :stop}) api/sexpr) ks))] + (cond + invalid-key + (api/reg-finding! + {:message (str "lifecycle functions can only contain `:start` and `:stop`. illegal function found: " (api/sexpr invalid-key)) + :type :mount/defstate + :row (:row (meta invalid-key)) + :col (:col (meta invalid-key))}) + (not (contains? (set (map api/sexpr ks)) :start)) + (throw (ex-info "lifecycle functions must include `:start`" {})) + ((complement contains?) #{2 4} (count args)) + (throw (ex-info "lifecycle functions must consist of no more than 2 pair forms: `:start` and `:stop`" {})) + (and (contains? m :on-reload) (not (contains? #{:noop :stop} (:on-reload m)))) + (api/reg-finding! + {:message "metadata `:on-reload` key can only have value of `noop` or `stop`" + :type :mount/defstate + :row (:row (meta n)) + :col (:col (meta n))}) + :else + {:node (api/list-node + (list* + (api/token-node 'fn*) + (api/vector-node [n]) + n + args))}))) diff --git a/resources/clj-kondo.exports/mount/mount/config.edn b/resources/clj-kondo.exports/mount/mount/config.edn new file mode 100644 index 0000000..7cbb873 --- /dev/null +++ b/resources/clj-kondo.exports/mount/mount/config.edn @@ -0,0 +1,3 @@ +{:lint-as {mount.core/defstate clojure.core/def-catch-all} + :linters {:mount/defstate {:level :warning}} + :hooks {:analyze-call {mount.core/defstate hooks.defstate/defstate}}} diff --git a/src/clj-kondo.exports/mount/mount/config.edn b/src/clj-kondo.exports/mount/mount/config.edn deleted file mode 100644 index 3092d4c..0000000 --- a/src/clj-kondo.exports/mount/mount/config.edn +++ /dev/null @@ -1 +0,0 @@ -{:lint-as {mount.core/defstate clojure.core/def}} \ No newline at end of file