Is it possible to disable the arity-check for keywords? We have an embedded language that uses keywords in function position, with an arbitrary number of arguments.
@hugod yes, let me get back to you after a meeting
meanwhile you might want to look at https://github.com/borkdude/clj-kondo/blob/master/doc/config.md
I think this is what you're after: https://github.com/borkdude/clj-kondo/blob/master/doc/config.md#exclude-arity-linting-inside-a-specific-macro-call
alternatively you could write a hook for your macro, but that's more work than just suppressing warnings
No urgency. Thanks for the config pointers. I see how to disable the check for a specific function/macro, but not for keywords in general. The set of keywords we use like this is open, although there is a limited set that probably covers most usage. Maybe that is good enough.
Writing a hook is probably the best solution, but requires some work
I assume these keywords are only used within a macro right?
Within various macros, right. I’ll have a look at hooks. Thanks
Um, looks like it is not possible to define a hook on an unqualified symbol pulled in via a :refer :all
it depends if clj-kondo knows that namespace (e.g. it's been linted before)
btw, I think I have a hook here:
I assume the macro you use is required?
This is the code:
(ns hooks.kw-macro
(:require [clj-kondo.hooks-api :as api]))
(defn rewrite [node]
(let [sexpr (api/sexpr node)]
(if (and (seq? sexpr)
(keyword? (first sexpr)))
(let [children (rest (:children node))
children (mapv rewrite children)]
(with-meta (api/vector-node children)
(meta node)))
(with-meta
(update node :children #(mapv rewrite %))
(meta node)))))
(defn kw-macro [{:keys [:node]}]
(let [new-node (api/vector-node (mapv rewrite (rest (:children node))))]
;; for debugging:
;;(prn :new-node new-node)
{:node new-node}))
I basically rewrite (my/kw-macro (:foo 1 2 3) (:bar 1 2 3))
to [[:foo 1 2 3] [:bar 1 2 3]]
That should work with some modification to use walk to pick up nested forms. 🙂
walk unfortunately doesn't preserve metadata
I think I have a walk that does - thanks for the reminder
the macro is required with a :refer :all
and is actually defined in a different namespace via an import-vars
like mechanism.
I can’t get the hook to trigger at all
you're out of luck there, clj-kondo has no way of knowing what var you are calling
it does have support for import-vars (the widely used one). so if you use the same syntax as that one and use lint-as AND lint all of your namespaces, it might work
Is there any existing macro to inspect how clj-kondo is currently parsing an expression? eg, something I can insert around a form that prints the rewrite map
yes, look at the example I provided you
;;(prn :new-node new-node)
duh, upgrading clj-kondo helps a lot
:)
How do you develop hooks? clj-kondo seems to just silently ignore the hook if it fails to load.
Clj-kondo will print a warning if the hook isn’t found but is in your config
e.g. with this config:
{:hooks {:analyze-call {my/kw-macro hooks.kw-macro/kw-macro2}}}
borkdude@MBP2019 /tmp/proj $ clj-kondo --lint example.clj
WARNING: error while trying to read hook for my/kw-macro: Could not resolve symbol: hooks.kw-macro/kw-macro2 [at line 2, column 1]
example.clj:4:14: error: keyword :foo is called with 3 args but expects 1 or 2
example.clj:5:14: error: clojure.core/inc is called with 3 args but expects 1
example.clj:6:15: error: keyword :foo is called with 3 args but expects 1 or 2
example.clj:6:28: error: clojure.core/inc is called with 3 args but expects 1
linting took 59ms, errors: 4, warnings: 0
^ @hugodand correcting it:
$ clj-kondo --lint example.clj
example.clj:5:14: error: clojure.core/inc is called with 3 args but expects 1
example.clj:6:28: error: clojure.core/inc is called with 3 args but expects 1
linting took 33ms, errors: 2, warnings: 0
if there is a compile error in the hook itself, it seems to be silently ignored.
let me make a deliberate mistake
I'm putting a random x in my hook code:
$ clj-kondo --lint example.clj
WARNING: error while trying to read hook for my/kw-macro: Could not resolve symbol: x [at /private/tmp/proj/.clj-kondo/hooks/kw_macro.clj, line 5, column 3]
um, I wonder what’s different here
yeah, you might have hit a different case. repro welcome, maybe something could be improved
hooks are very flexible :)
powered by the same interpreter as babashka
@hugod could also be a laziness issue so the error is never reached?
Good thought. I’ll try and investigate more later.