Fork me on GitHub
#clj-kondo
<
2023-02-06
>
orestis12:02:59

I have a macro that is linted-as def but I don't want it to trigger :clojure-lsp/unused-public-var - and I would like to avoid putting the :clj-kondo/ignore directive on each callsite. Is it possible to skip a particular linter at the macro-level?

orestis12:02:37

The macro itself is doing some global mutable state stuff so by definition it is always available as a public var - but obviously clj-kondo doesn't know that...

borkdude12:02:28

the linter is implemented by clojure-lsp (the only one). @UKFSJSM38 might have an idea

orestis12:02:54

Would it be different if it was another linter?

ericdallo12:02:52

You have multiple options to ignore that, check the https://clojure-lsp.io/settings/#clojure-lspunused-public-varpage: .clj-kondo/config.edn

{:linters {:clojure-lsp/unused-public-var {:exclude #{my-ns/foo
                                                      my-ns/bar
                                                      other-ns
                                                      my-func}
                                           :exclude-regex #{"my-integration-tests.*"}
                                           :exclude-when-defined-by #{my-ns/defflow}
                                           :exclude-when-defined-by-regex #{"my.custom/macro-.*"}}}}

orestis12:02:21

So I guess

:clojure-lsp/unused-public-var {:exclude-when-defined-by
                                           #{nosco.views.design-system-utils/defsection}}

👍 2
orestis12:02:03

And then when I use

(nds/defsection CardsSection ,,,) 
I shouldn't get the warning? Because it seems it's not working.

borkdude12:02:44

@U7PBP4UVA I think it would be best if you made a github repro since my experience is that even if this takes a few minutes to put together, it saves time discussing these issues

orestis12:02:05

should it be in LSP or in Kondo?

borkdude12:02:17

just a github repo to demonstrate the problem

ericdallo12:02:51

:exclude-when-defined-by won't work since it only works when kondo includes defined-by analysis, and that only happens in hook level and is defined by user

ericdallo12:02:11

You will have to: • use :exclude as you are aalready using • create a custom clj-kondo hook and add the defined-by • Make clj-kondo return the defined-by as the macro name currently is returning :defined-by clojure.core/def WDYT @U04V15CAJ?

borkdude12:02:43

@UKFSJSM38 maybe you can make a PR showing that idea?

orestis12:02:52

I'm tinkering with the custom hook, hold on

borkdude12:02:44

if you are making a hook you could also emit a usage to suppress the unused public var usage

☝️ 2
orestis12:02:01

I was trying a macroexpand first

borkdude12:02:21

(do (def x ...) x)

orestis12:02:53

BTW @U04V15CAJ the docs for macroexpand use the wording "recommended" to use the same namespaces/files for the macros, but perhaps it should be changed to "required" instead?

orestis12:02:14

I wasn't able to make it work until I moved the kondo macro in the same namespace as the original.

borkdude13:02:50

It's not required but you should realize how syntax quote expands

orestis17:02:43

Ah yeah, that makes sense - but in my case even a simple def wasn't being picked up. I'll try a repro in that same repo.