Hey; I'm creating an linter (for personal use) for clj-kondo using the hooks.api. First off; props 🍻!
I use a replacement for clojure/defn
in some cases. Right now I add my macro to :lint-as
and add a hook for clojure.core/defn
. This works nicely as mything/defn
is now recognised and linted properly.
I want to detect when clojure/defn
is used instead of mything/defn
and got a POC working based on the hooks-api. My issue is that i have to match the tag of the node exactly; which can change with other requires/refers.
Is there a better way to do this? Thanks 🙂
@jeroen.dejong I'm not sure what you mean with "match the tag of the node exactly"
In the hook i receive a node, which is the exact symbol it is in the sourcecode.
Because mything/defn
is linted as clojure/defn
, i can't differentiate the two if they're both referred as defn
.
Why do you use both lint-as + a hook?
I want the clj-kondo built-in defn-linting to keep working
@jeroen.dejong What do you need the hook for?
What are you doing in the hook?
My macro uses metadata to do some assertions, (which don't do anything for clj/defn). I'd like a linter which reminds me that i tried to use certain metadata so i should probably use my/defn
so you want a hook on clojure.core/defn?
Yes
I think I understand the issue now. https://github.com/clj-kondo/clj-kondo/issues/1170
Awesome, thank you 🙂
I just pushed a commit to master. You can download a new binary from the builds in a few minutes for testing
Linux binary: https://13196-176829714-gh.circle-artifacts.com/0/release/clj-kondo-2021.01.21-SNAPSHOT-linux-amd64.zip
macOS binary: https://13198-176829714-gh.circle-artifacts.com/0/release/clj-kondo-2021.01.21-SNAPSHOT-macos-amd64.zip
That's amazing. I'll check it out in a min
Not quite sure how this should work yet, I tried the following: Given these two functions
(my/defn foo [^:tag x] x)
(defn bar [y] y)
(defn fizz [^:tag z] z)
bar
is a simple fn,`my/defn` is expected usage (and should be treated as normal defn
` , and fizz
should report as "did you mean my/defn" because of the ^:tag
.
I configured condo:
{:hooks {:analyze-call {clojure.core/defn hooks/defn-hook}}
hooks/defn-hook
reports fizz
correctly :thumbsup::skin-tone-2: .
In order to make clj-kondo recognise my/defn
as defn
, I made a hook which transforms the node as defn
. unfortunately that triggers defn-hook
too (similar to the lint-as
approach).I think you should do the following:
{:hooks {:analyze-call {clojure.core/defn your-hook}
:lint-as {your/defn clojure.core/defn}}
so your your/defn calls will be processed as defn (without going through your hook), but the vanilla clojure.core/defn goes through your hook
That works!
You are the same person as Jeroen de Jong right?
Just for my info ;)
Yes 🙂 Figured i'd share it on GH too, for prosperities sake
:thumbsup:
Is there any way to use a hook from other project or something like that?
I'd like to use the same hook from rewrite-clj
on clojure-lsp
: https://github.com/clj-commons/rewrite-clj/blob/v1/.clj-kondo/hooks/rewrite_clj/potemkin/import_vars_with_mods.clj
do I need to copy that file or is there a better way to handle that?
Also even copying that, it seems to not work for clojure-lsp project 😕
I have this on my .clj-kondo/.config
:
{:hooks {:analyze-call {rewrite-clj.potemkin.clojure/import-vars-with-mods
hooks.rewrite-clj.potemkin.import-vars-with-mods/import-vars-with-mods}}
@ericdallo Yes, you can copy/move the hook code to a common directory and then add that directory to :config-paths
oh, got it, I'll try that
So, not sure what I'm doing wrong, I tryed changing config-paths, or using with ^replace [] just like rewrite-clj does but it's not recognizing the macros
my question is
Should I have that rewrite-clj.potemkin.clojure/import-vars-with-mods
copied besides the hooks.rewrite-clj.potemkin.import-vars-with-mods/import-vars-with-mods
?
I thought clj-kondo would search on classpath and find it
no, clj-kondo doesn't search hook code on a classpath. it only searches it within your .clj-kondo
directory and by extension in :config-paths
note that hook code does not need the code that it is about to be available anywhere
I see, so it'd be necessary to copy the code from rewrite-clj.potemkin.clojure/import-vars-with-mods
too?
yeah, I could add it too on .clj-kondo/hooks
right?
> rewrite-clj.potemkin.clojure/import-vars-with-mods this is not hook code right, just JVM code?
humm, you are right
is there a way for me to return more than one form through my hook function, or I could just do a (do (defn ...) (defn ..))
right?
yeah that worked :thumbsup:
@dharrigan Thanks for the kind sponsorship on OpenCollective!