Skip to content

Commit

Permalink
Merge pull request #202 from Garmelon/clj-kondo-support
Browse files Browse the repository at this point in the history
Add clj-kondo support for defelem and defhtml
  • Loading branch information
weavejester committed Jul 3, 2023
2 parents cb28ffc + bfd6605 commit acb6572
Show file tree
Hide file tree
Showing 5 changed files with 45 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/hiccup/hiccup"]}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ pom.xml.asc
*.class
.lein-*
.nrepl-port
.clj-kondo/*
!.clj-kodo/config.edn
3 changes: 2 additions & 1 deletion deps.edn
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
{:deps {org.clojure/clojure {:mvn/version "1.7.0"}}}
{:paths ["src" "resources"]
:deps {org.clojure/clojure {:mvn/version "1.7.0"}}}
2 changes: 2 additions & 0 deletions resources/clj-kondo.exports/hiccup/hiccup/config.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{:lint-as {hiccup.def/defhtml clojure.core/defn}
:hooks {:analyze-call {hiccup.def/defelem hiccup.hooks/defelem}}}
38 changes: 38 additions & 0 deletions resources/clj-kondo.exports/hiccup/hiccup/hiccup/hooks.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
(ns hiccup.hooks
(:require [clj-kondo.hooks-api :as api]
[clojure.set :as set]))

;; See https://github.com/clj-kondo/clj-kondo/blob/master/doc/hooks.md

(defn- parse-defn [elems]
(let [[fhead fbody] (split-with #(not (or (api/vector-node? %)
(api/list-node? %)))
elems)
arities (if (api/vector-node? (first fbody))
(list (api/list-node fbody))
fbody)]
[fhead arities]))

(defn- count-args [arity]
(let [args (first (api/sexpr arity))]
(if (= '& (fnext (reverse args)))
true ; unbounded args
(count args))))

(defn- dummy-arity [arg-count]
(api/list-node
(list
(api/vector-node
(vec (repeat arg-count (api/token-node '_)))))))

(defn defelem [{:keys [node]}]
(let [[_ & rest] (:children node)
[fhead arities] (parse-defn rest)
arg-counts (set (filter number? (map count-args arities)))
dummy-arg-counts (set/difference (set (map inc arg-counts)) arg-counts)
dummy-arities (for [n dummy-arg-counts] (dummy-arity n))]
{:node
(api/list-node
(list*
(api/token-node 'clojure.core/defn)
(concat fhead arities dummy-arities)))}))

0 comments on commit acb6572

Please sign in to comment.