Fork me on GitHub
#clj-kondo
<
2023-12-21
>
Alexander Kouznetsov18:12:41

I’d like to propose this inline way of declaring hooks for macros:

(defmacro my-macro
  {:clj-kondo/hook 'my-hook-namespace/my-hook-name}
  [...]
  ...)
This way it is immediately visible how the macro is linted and moreover, cmd-click on the hook name (at least in calva) brings you immediately to the hook definition. Thoughts?

Alexander Kouznetsov18:12:56

We can detect whether it is analyze-call or macroexpand based on whether it is defined as a macro or as a function, but we could also add it to the inline tag to be explicit. :clj-kondo/macroexpand-hook 'my-hook-name as one option.

borkdude19:12:04

I don't know if this helps since you need to define those hooks in external files anyway. I don't like to introduce any ambiguity between analyze-call or macroexpand, macros can be handled with analyze-call as well

Alexander Kouznetsov19:12:48

What do you mean by external files? They’re in resources which are also part of the source code.

borkdude20:12:48

the idea of inline configs originated from someone who didn't like to write external files like .clj-kondo/config.edn but when you write hooks you need to add files in .clj-kondo/..../foo.clj_kondo anyway

Alexander Kouznetsov22:12:43

Is assert expected to work in clj-kondo hooks? I have some and they crash with error: No matching ctor found for class java.lang.AssertionError My code is (assert (vector? node)) in the analyze-call hook.

Noah Bogart00:12:20

I like to use namespaced keywords for custom hook errors: :splint/mismatched-diagnosis, etc

Noah Bogart00:12:12

We have roughly 15 custom linter keywords in my job’s codebase

Alexander Kouznetsov00:12:10

Nice, this worked!

🎉 2
Alexander Kouznetsov20:01:46

> maybe it’s better to use api/reg-finding! than assert though is there a good way to obtain row and col when it is used in a macroexpand hook? I tried it outside of a syntax quote like this:

(defmacro my-hook [source & body]
  (when-not (string? source) (api/reg-finding! (merge
                                                 (select-keys (meta source) [:row :col :end-row :end-col])
                                                 {:message "source must be string"
                                                  :type :type-mismatch
                                                  :level :error})
  `(let [xyz# ~source] ~@body))
but the error I’m seeing has no line number my_file.clj::: error: source must be string

borkdude20:01:45

> is there a good way to obtain row and col when it is used in a macroexpand hook? the row/col metadata should be available as metadata, at least on objects that can carry metadata

👍 1
borkdude20:01:12

if you e.g. pass a number, it's not there anymore

1
borkdude20:01:36

you could pass the metadata of the &form argument, which is the whole surrounding form

👍 1
Alexander Kouznetsov20:01:56

So there is no way to recover location of a number?

borkdude20:01:14

nope, (with-meta 1 {:row 1}) doesn't work

😢 1
borkdude20:01:35

you might be better off with an :analyze-call hook here

👍 1