Skip to content

Commit 025f62b

Browse files
committed
addresses #1 first pass at clj-kondo support
Signed-off-by: Sean Corfield <[email protected]>
1 parent e298e78 commit 025f62b

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
(ns clj-kondo.clj-commons.slingshot.try-plus
2+
(:require [clj-kondo.hooks-api :as api]))
3+
4+
(defn expand-catch [catch-node]
5+
(let [[catch catchee & exprs] (:children catch-node)
6+
catchee-sexpr (api/sexpr catchee)]
7+
(cond (vector? catchee-sexpr)
8+
(let [[selector & exprs] exprs]
9+
(api/list-node
10+
[catch (api/token-node 'Exception) (api/token-node '_e#)
11+
(api/list-node
12+
(list* (api/token-node 'let)
13+
(api/vector-node [selector (api/token-node nil)])
14+
exprs))]))
15+
(seq? catchee-sexpr)
16+
(api/list-node
17+
(list* catch (api/token-node 'Exception) (api/token-node '_e#)
18+
(api/list-node
19+
(list (api/token-node 'fn)
20+
(api/vector-node [(api/token-node '%)])
21+
catchee))
22+
exprs))
23+
:else catch-node)))
24+
25+
(defn try+ [{:keys [node]}]
26+
(let [children (rest (:children node))
27+
[body catches]
28+
(loop [body children
29+
body-exprs []
30+
catches []]
31+
(if (seq body)
32+
(let [f (first body)
33+
f-sexpr (api/sexpr f)]
34+
(if (and (seq? f-sexpr) (= 'catch (first f-sexpr)))
35+
(recur (rest body)
36+
body-exprs
37+
(conj catches (expand-catch f)))
38+
(recur (rest body)
39+
(conj body-exprs f)
40+
catches)))
41+
[body-exprs catches]))
42+
new-node (api/list-node
43+
[(api/token-node 'let)
44+
(api/vector-node
45+
[(api/token-node '&throw-context) (api/token-node nil)])
46+
(api/token-node '&throw-context) ;; use throw-context to avoid warning
47+
(with-meta (api/list-node (list* (api/token-node 'try)
48+
(concat body catches)))
49+
(meta node))])]
50+
{:node new-node}))
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{:hooks
2+
{:analyze-call {clj-commons.slingshot/try+
3+
clj-kondo.clj-commons.slingshot.try-plus/try+}}}

0 commit comments

Comments
 (0)