|
| 1 | +(ns manifold.hooks |
| 2 | + (:require [clj-kondo.hooks-api :as api])) |
| 3 | + |
| 4 | +(defn- cons-vector-node |
| 5 | + [node parent] |
| 6 | + (api/vector-node (cons node (:children parent)))) |
| 7 | + |
| 8 | +(defn def-sink-or-source [call] |
| 9 | + (let [[name bindings & body] (-> call :node :children rest) |
| 10 | + extended-bindings |
| 11 | + (cons-vector-node (api/token-node 'lock) bindings)] |
| 12 | + |
| 13 | + {:node |
| 14 | + (api/list-node |
| 15 | + (list |
| 16 | + (api/token-node 'do) |
| 17 | + |
| 18 | + (api/list-node |
| 19 | + (list* |
| 20 | + (api/token-node 'deftype) |
| 21 | + name |
| 22 | + extended-bindings |
| 23 | + body)) |
| 24 | + |
| 25 | + (api/list-node |
| 26 | + (list |
| 27 | + (api/token-node 'defn) |
| 28 | + (api/token-node (symbol (str "->" (:string-value name)))) |
| 29 | + bindings))))})) |
| 30 | + |
| 31 | +(defn- seq-node? [node] |
| 32 | + (or (api/vector-node? node) |
| 33 | + (api/list-node? node))) |
| 34 | + |
| 35 | +(defn- nth-child [node n] (nth (:children node) n)) |
| 36 | + |
| 37 | +(defn both [call] |
| 38 | + (let [body (-> call :node :children second :children) |
| 39 | + expand-nth |
| 40 | + (fn [n item] |
| 41 | + (if (and (seq-node? item) (= 'either (:value (nth-child item 0)))) |
| 42 | + (:children (nth-child item n)) |
| 43 | + [item]))] |
| 44 | + |
| 45 | + {:node |
| 46 | + (api/list-node |
| 47 | + (list |
| 48 | + (api/token-node 'do) |
| 49 | + |
| 50 | + (api/list-node |
| 51 | + (->> body (mapcat (partial expand-nth 1)))) |
| 52 | + |
| 53 | + (api/list-node |
| 54 | + (->> body (mapcat (partial expand-nth 2))))))})) |
| 55 | + |
| 56 | + |
| 57 | +(def fallback-value |
| 58 | + "The fallback value used for declaration of local variables whose |
| 59 | + values are unknown at lint time." |
| 60 | + (api/list-node |
| 61 | + (list |
| 62 | + (api/token-node 'new) |
| 63 | + (api/token-node 'java.lang.Object)))) |
| 64 | + |
| 65 | +(defn success-error-unrealized [call] |
| 66 | + |
| 67 | + (let [[deferred |
| 68 | + success-value success-clause |
| 69 | + error-value error-clause |
| 70 | + unrealized-clause] (-> call :node :children rest)] |
| 71 | + |
| 72 | + (when-not (and deferred success-value success-clause error-value |
| 73 | + error-clause unrealized-clause) |
| 74 | + (throw (ex-info "Missing success-error-unrealized arguments" {}))) |
| 75 | + |
| 76 | + {:node |
| 77 | + (api/list-node |
| 78 | + (list |
| 79 | + (api/token-node 'do) |
| 80 | + |
| 81 | + (api/list-node |
| 82 | + (list |
| 83 | + (api/token-node 'let) |
| 84 | + (api/vector-node (vector success-value fallback-value)) |
| 85 | + success-clause)) |
| 86 | + |
| 87 | + (api/list-node |
| 88 | + (list |
| 89 | + (api/token-node 'let) |
| 90 | + (api/vector-node (vector error-value fallback-value)) |
| 91 | + error-clause)) |
| 92 | + |
| 93 | + unrealized-clause))})) |
0 commit comments