|
| 1 | +(ns babashka.fs |
| 2 | + (:require [clj-kondo.hooks-api :as api])) |
| 3 | + |
| 4 | +(defn- symbol-node? [node] |
| 5 | + (and (api/token-node? node) |
| 6 | + (symbol? (api/sexpr node)))) |
| 7 | + |
| 8 | +(defn with-temp-dir |
| 9 | + [{:keys [node]}] |
| 10 | + (let [args (rest (:children node)) |
| 11 | + binding-like-vector (first args) |
| 12 | + body (rest args)] |
| 13 | + (when-not (zero? (count args)) ;; let clj-kondo report on arity |
| 14 | + (if-not (api/vector-node? binding-like-vector) |
| 15 | + (api/reg-finding! (assoc (meta binding-like-vector) |
| 16 | + :message "babashka.fs/with-temp-dir requires a vector for first arg" |
| 17 | + :type :babashka-fs/with-temp-dir-first-arg-not-vector)) |
| 18 | + (let [[binding-sym options & rest-in-vec] (:children binding-like-vector)] |
| 19 | + (when (not (symbol-node? binding-sym)) |
| 20 | + (api/reg-finding! (assoc (meta (or binding-sym binding-like-vector)) |
| 21 | + :message "babashka.fs/with-temp-dir vector arg requires binding-name symbol as first value" |
| 22 | + :type :babashka-fs/with-temp-dir-vector-arg-needs-binding-symbol))) |
| 23 | + (doseq [extra-vector-arg rest-in-vec] |
| 24 | + (api/reg-finding! (assoc (meta extra-vector-arg) |
| 25 | + :message "babashka.fs/with-temp-dir vector arg accepts at most 2 values" |
| 26 | + :type :babashka-fs/with-temp-dir-vector-arg-extra-value))) |
| 27 | + |
| 28 | + (when binding-sym |
| 29 | + {:node (api/list-node |
| 30 | + ;; satisfy linter by creating binding for for binding-sym |
| 31 | + (list* |
| 32 | + (api/token-node 'let) |
| 33 | + ;; it doesn't really matter what we bind to, so long as it is bound |
| 34 | + (api/vector-node [binding-sym (api/token-node nil)]) |
| 35 | + options ;; avoid unused binding when options is a binding |
| 36 | + body))})))))) |
0 commit comments