File tree Expand file tree Collapse file tree 4 files changed +53
-18
lines changed
resources/clj-kondo.exports/org.clj-commons/slingshot
src/clj_commons/slingshot Expand file tree Collapse file tree 4 files changed +53
-18
lines changed Original file line number Diff line number Diff line change 1- (ns clj-kondo.clj-commons.slingshot.try-plus
2- (:require [clj-kondo.hooks-api :as api]))
1+ (ns clj-kondo.clj-commons.slingshot
2+ (:require [clj-kondo.hooks-api :as api]
3+ [clojure.walk]))
34
45(defn expand-catch [catch-node]
56 (let [[catch catchee & exprs] (:children catch-node)
4546 (api/vector-node
4647 [(api/token-node '&throw-context) (api/token-node nil )])
4748 (api/token-node '&throw-context) ; ; use throw-context to avoid warning
48- (with-meta (api/list-node (list* (api/token-node 'try)
49- (concat body catches)))
49+ (with-meta (api/list-node
50+ (list* (api/token-node (if (seq catches) 'try 'do))
51+ (concat body catches)))
5052 (meta node))])]
5153 {:node new-node}))
54+
55+ (defn- contains- %?
56+ " Returns true if % appears within coll at any nesting depth"
57+ [coll]
58+ (let [result (atom false )]
59+ (clojure.walk/postwalk
60+ (fn [t]
61+ (when (= '% t)
62+ (reset! result true )))
63+ coll)
64+ @result))
65+
66+ (defn throw+ [{:keys [node]}]
67+ (if-let [children (seq (rest (:children node)))]
68+ (if (contains-%? (map api/sexpr children))
69+ (let [new-node (api/list-node
70+ [(api/token-node 'throw)
71+ (api/list-node
72+ [(api/token-node 'new)
73+ (api/token-node 'Exception)
74+ (api/list-node
75+ [(api/token-node 'str)
76+ (api/list-node
77+ [(api/token-node 'fn)
78+ (api/vector-node [(api/token-node '%)])
79+ (api/list-node
80+ (list* (api/token-node 'str)
81+ children))])])])])]
82+ {:node new-node})
83+ {:node node})
84+ {:node node}))
Original file line number Diff line number Diff line change 11{:hooks
22 {:analyze-call {clj-commons.slingshot/try+
3- clj-kondo.clj-commons.slingshot.try-plus/try+}}}
3+ clj-kondo.clj-commons.slingshot/try+
4+ clj-commons.slingshot/throw+
5+ clj-kondo.clj-commons.slingshot/throw+}}}
Original file line number Diff line number Diff line change 4343 corresponding context with t assoc'd as the value for :wrapper, else
4444 returns nil"
4545 [^Throwable t]
46- (if -let [data (ex-data t)]
46+ (when -let [data (ex-data t)]
4747 (assoc (make-context t)
4848 :object (if (::wrapper? (meta data)) (:object data) data)
4949 :wrapper t)))
5555 returns nil."
5656 [^Throwable t]
5757 (or (unwrap t)
58- (if -let [cause (.getCause t)]
58+ (when -let [cause (.getCause t)]
5959 (recur cause))))
6060
6161(defn get-throwable
131131 [catch-clauses throw-sym threw?-sym]
132132 (letfn
133133 [(class-selector? [selector]
134- (if (symbol? selector)
134+ (when (symbol? selector)
135135 (let [resolved (resolve selector)]
136- (if (class? resolved)
136+ (when (class? resolved)
137137 resolved))))
138138 (cond-test [selector]
139139 (letfn
185185 (try
186186 (when-not @~threw?-sym
187187 ~@(rest else-clause))
188- ~(if finally-clause
188+ ~(when finally-clause
189189 finally-clause))))
190190 finally-clause
191191 (list finally-clause)))
196196 " Expands to sym if it names a local in the current environment or
197197 nil otherwise"
198198 [sym]
199- (if (contains? &env sym)
199+ (when (contains? &env sym)
200200 sym))
201201
202202(defn stack-trace
Original file line number Diff line number Diff line change 353353 [throw? catch? finally? broken-else?]
354354 (let [rec-sym (gensym " rec" )
355355 body (gen-body rec-sym throw?)
356- catch-clause (if catch? (gen-catch-clause rec-sym))
356+ catch-clause (when catch? (gen-catch-clause rec-sym))
357357 else-clause (gen-else-clause rec-sym broken-else?)
358- finally-clause (if finally? (gen-finally-clause rec-sym))]
358+ finally-clause (when finally? (gen-finally-clause rec-sym))]
359359 `(let [~rec-sym (atom [])]
360360 (try+
361361 ~(remove nil? `(try+
379379 actual (eval try-else-form)
380380 expected (vec (remove nil?
381381 [:body
382- (if (and throw? catch?) :catch )
383- (if (not throw?) :else )
384- (if finally? :finally )
382+ (when (and throw? catch?) :catch )
383+ (when (not throw?) :else )
384+ (when finally? :finally )
385385 ; ; expect an escaped exception when either:
386386 ; ; a) the else clause runs, and throws
387387 ; ; b) the body throws, and is not caught
388- (if (or (and (not throw?) broken-else?)
389- (and throw? (not catch?))) :bang! )]))]
388+ (when (or (and (not throw?) broken-else?)
389+ (and throw? (not catch?))) :bang! )]))]
390390 (is (= actual expected))))))
391391
392392(deftest test-reflection
You can’t perform that action at this time.
0 commit comments