Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "Create clj-kondo hook" actions #1737

Open
mrkam2 opened this issue Dec 13, 2023 · 4 comments
Open

Add "Create clj-kondo hook" actions #1737

mrkam2 opened this issue Dec 13, 2023 · 4 comments
Labels
enhancement New feature or request good first issue Good for newcomers
Projects

Comments

@mrkam2
Copy link

mrkam2 commented Dec 13, 2023

Is your feature request related to a problem? Please describe.
I'd like to automate the following, currently manual, steps I need to take to create a new clj-kondo hook:

  1. Update deps.edn file to include "resources" folder into :paths if it isn't there yet.
  2. Create resources/clj-kondo.exports/<my-org>/<my-lib>/config.edn file if it doesn't exist yet.
  3. Update this file content to assoc-in [:hooks :analyze-call <fully-qualified-macro-name>] hooks.<fully-qualified-macro-name> for analyze-call hooks and the same with :macroexpand instead of :analyze-call for the hook of the macro expand type.
  4. Create resources/clj-kondo.exports/<my-org>/<my-lib>/hooks/<namespace path>/<namespace name>.clj file.
  5. Add a new definition to this file - for :analyze-call it should be
(defn <macro-name> [{:keys [node]}]
  (let [[<macro-args>] (rest (:children node))]
     ;; TODO Add your code here.
     {:node node}))

For :macroexpand it should be

(defmacro <macro-name> [<macro-args>]
   ;; TODO Add you code here.
   )

Describe the solution you'd like
I'd like the following workflow. Open a macro in editor, invoke either "create analyze-call hook" or "create macroexpand hook" and have all of these steps performed and my cursor jump to Add your code here line.

It would also be great to have a command to jump from a macro to its hook and back (and maybe to its line in clj-kondo config file).

Reference:
https://github.com/clj-kondo/clj-kondo/blob/master/doc/hooks.md#hooks

Example:
I'm working on my-company/cool-lib project where I have feature/helper.clj namespace that has a macro (defmacro def-rule [name & specs]. I want the following two options:

  1. analyze-call option:
  • resources/clj-kondo.exports/my-company/cool-lib/config.edn file with the following new content:
    {:hooks {:analyze-call {feature.helper/def-rule hooks.feature.helper/def-rule}}}
  • resources/clj-kondo.exports/my-company/cool-lib/hooks/feature/helper.clj file with the following new content:
    (ns hooks.feature.helper
      (:require [clj-kondo.hooks-api :as api]))
    
    (defn def-rule [:{keys [node]}]
      (let [[name & specs] (rest (:children node))]
         ;; TODO Add your code here.
         {:node node}))
  1. macroexpand option:
  • resources/clj-kondo.exports/my-company/cool-lib/config.edn file with the following new content:
    {:hooks {:macroexpand {feature.helper/def-rule hooks.feature.helper/def-rule}}}
  • resources/clj-kondo.exports/my-company/cool-lib/hooks/feature/helper.clj file with the following new content:
    (ns hooks.feature.helper)
    
    (defmacro def-rule [name & specs]
       ;; TODO Add your code here.
       )
@mrkam2 mrkam2 added the enhancement New feature or request label Dec 13, 2023
@ericdallo ericdallo added this to Low priority in clojure-lsp via automation Dec 14, 2023
@ericdallo ericdallo added the good first issue Good for newcomers label Dec 14, 2023
@ericdallo
Copy link
Member

You may be interested on this one @borkdude

@borkdude
Copy link
Contributor

When exporting configs, don't use hooks/whatever for your clj-kondo namespaces, but use my_company/whatever, to avoid conflicts with others.

@mrkam2
Copy link
Author

mrkam2 commented Dec 14, 2023

When exporting configs, don't use hooks/whatever for your clj-kondo namespaces, but use my_company/whatever, to avoid conflicts with others.

Makes sense. Sounds like we might want to repeat the same <my-org>/<my-lib> if those aren't already part of an original namespace, which makes the path a bit repetitive, especially considering the full path. Something like:
my-company/cool-lib/resources/clj-kondo.exports/my-company/cool-lib/my-company/cool-lib/feature/helper.clj where my-company/cool-lib is repeated 3 times.

I'm wondering if we were able to reduce this to something like this:
my-company/cool-lib/resources/clj-kondo.exports/hooks/feature/helper.clj.

Do you think this may be changed in the future?

@borkdude
Copy link
Contributor

borkdude commented Dec 14, 2023

Just check what ends up in .clj-kondo and it should be clear how clashes can happen, no changes are planned around this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
clojure-lsp
Low priority
Development

No branches or pull requests

3 participants