You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have read the Clojure etiquette and will respect it when communicating on this platform.
problem
clojure-lsp/unused-public-var linter is quite useful but sometimes we want to suppress it. For example, in a situation when a variable is defined by a custom macro and that's the correct behavior. For such cases, this linter has a useful :exclude-when-defined-byoption. Unfortunately, when a hook is used, this option doesn't help much because the custom macro name is not propagated to the linter when a hook is used. In particular, definition parameter received by clojure-lsp.queries/exclude-public-definition?function contains the following two fields (which it then exercises):
(nsmyns)
(defmacromydef [db n & body]
`(do (defn ~(str n "-foo") [] (with-open [_# ~db] ~@body))
(defn ~(str n "-bar") [] (with-open [_# ~db] ~@body))))
I then define a :macroexpand hook with roughly the same content. There are situations when the client code only uses one of these variables and they're being flagged by unused-public-var linter. The simplest solution would be to provide the following config:
But this wouldn't work as when this linter receives the information about these n-foo and n-bar vars their attributes show them as defined by defn and not mydef.
problem
clojure-lsp/unused-public-var
linter is quite useful but sometimes we want to suppress it. For example, in a situation when a variable is defined by a custom macro and that's the correct behavior. For such cases, this linter has a useful:exclude-when-defined-by
option. Unfortunately, when a hook is used, this option doesn't help much because the custom macro name is not propagated to the linter when a hook is used. In particular,definition
parameter received byclojure-lsp.queries/exclude-public-definition?
function contains the following two fields (which it then exercises):As Eric suggested in slack comment, when
:analyze-call
hook is used, it can provide additional:defined-by
field on the returned map:This causes the
definition
parameter to contain these values instead:Unfortunately, this is not done by default and there seems to be no way to have it work for
:macroexpand
hooks.I looked at the code and I figured that with a small modifications, this information can be always supplied for all the hooks.
Here is the proposed diff: master...mrkam2:clj-kondo:patch-2
Example
I have a macro that defines two variables:
I then define a
:macroexpand
hook with roughly the same content. There are situations when the client code only uses one of these variables and they're being flagged byunused-public-var
linter. The simplest solution would be to provide the following config:But this wouldn't work as when this linter receives the information about these
n-foo
andn-bar
vars their attributes show them as defined bydefn
and notmydef
.Slack discussion: https://clojurians.slack.com/archives/CPABC1H61/p1706756199608299?thread_ts=1688068718.797849&cid=CPABC1H61.
The text was updated successfully, but these errors were encountered: