Fork me on GitHub
#clj-kondo
<
2024-01-17
>
J15:01:35

Hi guys! I'm learning hooks for a specific macro and I'm wondering what the best transformation for this kind of macro:

(f/match (my-fn)
  [result] (str "OK ->" result)
  [error] (str "KO ->" error))

borkdude15:01:51

What does the macro expand into in reality?

J15:01:51

Just a wrapper around merr/error:

(defmacro match [tst & clauses]
  (clojure.core/let [[result-vec result-form error-vec error-form] clauses
                     result-bind (result-vec 0)
                     error-bind (error-vec 0)]
    `(clojure.core/let [res# ~tst]
       (if-not (error? res#)
         (clojure.core/let [~result-bind res#]
           ~result-form)
         (clojure.core/let [~error-bind res#]
           ~error-form)))))

borkdude15:01:35

You could try this:

(let [[result error] (my-fn)]
  [result-clause error-clause])

👍 1
Noah Bogart16:01:39

that's not the same. my-fn returns some type that can be checked with error?. kind of like if-let, but for error?

borkdude16:01:29

that only matters if clj-kondo is clever enough to find out about that

Noah Bogart16:01:04

oh, you were suggesting a macro hook? my apologies

borkdude16:01:22

what do you think this was about?

Noah Bogart16:01:44

lol i thought you were suggesting an alternative way to write the match macro. a total misread on my part

😅 1
J17:01:33

Thanks @U04V15CAJ I have this:

(defn merr-match
  [{:keys [node]}]
  (let [[check-fn result-vec result-token error-vec error-token] (rest (:children node))
        [result-sym] (:children result-vec)
        [error-sym] (:children error-vec)
        sym-destructured (api/vector-node [result-sym error-sym])
        bindings (api/vector-node [sym-destructured check-fn])
        body (api/vector-node [result-token error-token])
        new-node (api/list-node
                   (list*
                     (api/token-node 'let)
                     bindings
                     body))]
    {:node new-node}))
But seems binding is not correctly see in branch body (cf. screen). What I miss?

J17:01:43

PS: kondo says unused binding

borkdude17:01:00

Try printing the result in the hook and show the command line output

borkdude18:01:23

the unused binding may result from not actually using the result-sym or error-sym

borkdude18:01:38

sorry I didn't see the screenshot

borkdude18:01:03

still, printing the result of merr-match before you return it may be worthwhile

borkdude18:01:36

I think you may want to change the last list* to list

borkdude18:01:49

since body is a vector node, not a list of nodes

J20:01:29

Thanks! The change to list resolve the issue.

nonrecursive19:01:41

hey y'all, I have a .clj-kondo/config.edn that uses :config-paths, but it seems like this isn't respected in emacs. is there anything I need to do to get this working?

borkdude19:01:13

do you have a repro? config paths are relative to the config.edn

borkdude19:01:48

so if you have a path relative to the project root, you need to use "../config-dir" if you have .clj-kondo/config.edn

nonrecursive20:01:45

oh I see, I was assuming I could include a filename in paths. I have {:config-paths ["../../.clj-kondo/common.edn"]} look like i need to have directory structure like common/config.edn

nonrecursive20:01:27

that's got it working