Fork me on GitHub
#clj-kondo
<
2022-08-10
>
hanDerPeder09:08:52

How to I lint a macro like:

(defmacro with-resource [symbol & body])
so it’s like with-open but with a symbol instead of a binding vector. symbol should be used in body

borkdude09:08:45

@peder.refsnes This macro is similar to cljs.test/async so you could use :lint-as for this

hanDerPeder09:08:21

thanks. anyway I could have figured this out myself? My strategy is usually to only look in clojure.core for a similar macro. is this the definition? https://github.com/clj-kondo/clj-kondo/blob/master/src/clj_kondo/impl/analyzer/test.clj#L26

borkdude09:08:51

The definition is in clojurescript

hanDerPeder09:08:11

of the lint rule?

borkdude09:08:24

syntax looks like this:

(async done ... .... (done))

hanDerPeder09:08:50

meant the definition of the lint rule, not the macro. my bad

borkdude09:08:58

Just try :lint-as {your/macro cljs.test/async}

borkdude09:08:26

clj-kondo knows about the syntax of cljs.test/async

borkdude09:08:34

this is why :lint-as works

hanDerPeder09:08:22

I did, it works. but does not assert the usage of symbol in body. wanted to try creating a rule in clj-kondo that would do that for fun

borkdude09:08:18

I think you're better off creating your own hook then: https://github.com/clj-kondo/clj-kondo/blob/master/doc/hooks.md

hanDerPeder09:08:56

aha, I see. thanks! ❤️

borkdude09:08:43

If the docs are too terse, you can consider doing these exercises: https://github.com/clj-kondo/hooks-workshop-clojured-2022

hanDerPeder09:08:53

nice, thanks :thumbsup:

hanDerPeder11:08:32

given this macro

(defmacro with-resource [symbol & body]
  `(let [~symbol (constantly 1)]
     ~@body))

(with-resource x
  (+ 1 (x)))
I want a hook for linting it. Came up with this
(defn with-resource [{:keys [:node]}]
  (let [[sym & body] (rest (:children node))
        fn-node (api/list-node
                 (list (api/token-node 'constantly)
                       (api/token-node 'nil)))

        new-node (api/list-node
                  (list* (api/token-node 'let)
                         (api/vector-node [sym fn-node])
                         body))]
    {:node new-node}))
This works exactly as I want it to. Just wondering if there is a more idiomatic way of writing it.