Skip to content

Commit a898e40

Browse files
authoredDec 10, 2021
Add clj-kondo rewrite hook for with-out-binding (#740)
Allow clj-kondo to understand the with-out-binding form, so linters still get applied to it.
1 parent 077d9ea commit a898e40

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed
 

‎.clj-kondo/config.edn

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
{:linters {:unresolved-symbol {:exclude [(cider.nrepl/def-wrapper)
1+
{:hooks {:analyze-call {cider.nrepl.middleware.out/with-out-binding
2+
hooks.core/with-out-binding}}
3+
:linters {:unresolved-symbol {:exclude [(cider.nrepl/def-wrapper)
24
(cider.nrepl.middleware.util.instrument/definstrumenter)
35
(cider.nrepl.middleware.util.instrument/with-break)
46
(cider.nrepl.middleware.util.instrument/instrument-special-form)
57
(cider.nrepl.middleware.util.instrument/instrument-coll)
6-
(cider.nrepl.print-method/def-print-method)
7-
(cider.nrepl.middleware.out/with-out-binding)]}
8+
(cider.nrepl.print-method/def-print-method)]}
89
:unused-import {:level :off}
910
:unresolved-namespace {:exclude [clojure.main]}}
1011
:output {:progress true

‎.clj-kondo/hooks/core.clj

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
(ns hooks.core
2+
(:require
3+
[clj-kondo.hooks-api :as api]))
4+
5+
(defn with-out-binding
6+
"Rewrite with-out-binding as a let on the first symbol in the arg vector.
7+
The remaining args in the arg vector are evaluated."
8+
[{:keys [node]}]
9+
(let [[_ binding-vec & body] (:children node)
10+
[binding & others] (:children binding-vec)
11+
new-node (api/list-node
12+
(list*
13+
(api/token-node 'let)
14+
(api/vector-node
15+
(reduce
16+
into
17+
[binding (api/token-node 'nil)]
18+
(mapv
19+
#(vector (api/token-node '_) %)
20+
others)))
21+
body))]
22+
;; un-comment below to debug changes
23+
;; (prn :with-binding (api/sexpr new-node))
24+
{:node (with-meta new-node (meta node))}))

0 commit comments

Comments
 (0)
Please sign in to comment.