Fork me on GitHub
#clj-kondo
<
2019-08-22
>
rickmoynihan10:08:43

I have a macro that introduces arbitrary new symbols within its scope. Is there a way to tell clj kondo to ignore any unused/bound symbols within its sexp?

rickmoynihan10:08:53

specifically I’d like to support the matcha query macros here for construct, ask, select etc… https://github.com/Swirrl/matcha#construct

rickmoynihan10:08:41

ahh is it :skip-args I need?

rickmoynihan10:08:53

hmmm looks like :skip-args works but it then causes lint failures on vars that are used inside the macro (but outside its sexp) e.g.

(let [x 10 ;; x raises lint warning here
       ]
  (matcha/construct ?s [[?s ?p x]])
Inspite the fact that it is used inside matcha/construct

rickmoynihan10:08:40

:skip-args means the macros special ?s ?p symbols aren’t flagged — but it then seems to cause the usage of x not to be detected.

yuhan11:08:23

I have the same issue with the meander lib which uses such special symbols all over the place - would it make sense to have a global user-customizable regex for symbols which clj-kondo will ignore?

borkdude11:08:24

you can ignore special symbols in a macro using a config:

rickmoynihan11:08:23

yeah that’s not good enough, there is no one special symbol… you can put unbound ?symbols with names of your choosing anywhere in the macro definition.

rickmoynihan11:08:39

the macro’s implement a query engine for matching patterns in graph data

rickmoynihan11:08:50

is it fair to say clj-kondo assumes that all macros behave syntactically like other common macros?

borkdude11:08:42

@rickmoynihan can you give an example. I'm pretty sure you can ignore those symbols using a config.

rickmoynihan11:08:11

(matcha/construct {:grafter.rdf/uri ?ds
                          ?p ?o}
                         [[catalog-uri dcat:dataset ?ds]
                          [?ds ?p ?o]])
Is from actual code… catalog-uri is bound outside the macro form in lexical scope, whilst all ? symbols are arbitrary

rickmoynihan11:08:36

or another (real usage):

rickmoynihan11:08:00

(matcha/construct {:grafter.rdf/uri ?nav-item ?nip ?nio}
                         [[nav-uri pmdui:hasNavItem ?nav-item]
                          [?nav-item ?nip ?nio]
                          (optional [[nav-uri pmdui:hasDisplayContext ?disp]])]
                         matcha-db)
here nav-uri and pmdui:hasDisplayContext and matcha-db come from lexical or ns level scope… all other symbols represent unbound query variables. The optional is nested macro syntax.

rickmoynihan11:08:44

(for which there is actually a defmacro definition, that just raises an error if it’s used outside of a query macro — but it provides a way to put a docstring on the optional)

borkdude11:08:48

borkdude@MBA2015 ~/Dropbox/dev/clojure/jet (master) $ cat /tmp/foo.clj
(ns foo
  (:require [matcha :as matcha]))

(matcha/construct {:grafter.rdf/uri ?ds
                   ?p ?o}
                  [[catalog-uri dcat:dataset ?ds]
                   [?ds ?p ?o]])
borkdude@MBA2015 ~/Dropbox/dev/clojure/jet (master) $ clj-kondo --lint /tmp/foo.clj
/tmp/foo.clj:4:37: info: unresolved symbol ?ds
/tmp/foo.clj:5:20: info: unresolved symbol ?p
/tmp/foo.clj:5:23: info: unresolved symbol ?o
/tmp/foo.clj:6:21: info: unresolved symbol catalog-uri
/tmp/foo.clj:6:33: info: unresolved symbol dcat:dataset
linting took 17ms, errors: 0, warnings: 0
borkdude@MBA2015 ~/Dropbox/dev/clojure/jet (master) $ clj-kondo --lint /tmp/foo.clj --config '{:linters {:unresolved-symbol {:exclude [(matcha/construct)]}}}'
linting took 12ms, errors: 0, warnings: 0
borkdude@MBA2015 ~/Dropbox/dev/clojure/jet (master) $

borkdude11:08:25

exactly how it's documented in the doc link I just posted

rickmoynihan11:08:06

ahh I misunderstood what that was doing thanks, I will give it a try… but first lunch! 🙂

rickmoynihan12:08:50

Many thanks borkdude that seems to work great!

lread13:08:17

Hi @borkdude! Thanks for the new release, you’ve really created something awesome here! BTW, noticed README is still advising use of —cache in one spot: > So for lein the entire command would be: > $ clj-kondo --lint "$(lein classpath)" --cache

borkdude13:08:27

oh, that can go away 🙂 feel free to do a minor PR if you feel inclined

borkdude13:08:59

totally forgot to mention you in the latest release, you helped me a bunch testing the import-vars stuff: https://github.com/borkdude/clj-kondo/releases/tag/v2019.08.21-alpha

lread14:08:15

will do PR. And am very happy to contribute with or without mentions. simple_smile

dharrigan18:08:21

arch updated