This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-01-28
Channels
- # announcements (4)
- # aws (2)
- # babashka (56)
- # beginners (43)
- # calva (70)
- # clj-kondo (25)
- # cljs-dev (29)
- # clojure (103)
- # clojure-dev (9)
- # clojure-europe (55)
- # clojure-gamedev (8)
- # clojure-nl (5)
- # clojure-norway (5)
- # clojure-uk (4)
- # clojured (1)
- # clojurescript (56)
- # copenhagen-clojurians (1)
- # core-async (2)
- # cursive (16)
- # datomic (5)
- # deps-new (18)
- # emacs (9)
- # events (3)
- # fulcro (45)
- # graphql (2)
- # gratitude (2)
- # kaocha (6)
- # lambdaisland (8)
- # lsp (72)
- # meander (41)
- # missionary (5)
- # nextjournal (52)
- # off-topic (2)
- # pathom (12)
- # pedestal (2)
- # practicalli (1)
- # re-frame (6)
- # reitit (5)
- # releases (1)
- # reveal (1)
- # specter (3)
- # sql (4)
- # tools-deps (22)
- # vim (8)
- # wasm (1)
- # xtdb (22)
I know there are many existing topic related to custom macros, but I’m having a hard time finding a solution to this specific problem. As we know, the defrecord
macro generates some constructor functions like ->
and map->
. Suppose I have a custom macro like defrecord+
which generates custom constructor functions. What are my options for configuring clj-kondo to handle this? Ideally I’d like to avoid having to add a configuration entry for every custom record definition created through my defrecord+
.
(defrecord+ MyRecord [id data])
; unresolved var
(make->MyRecord {:id 1 :data "something"})
In this example, is your.ns
the namespace that defines the defrecord+
macro or the namespace that is using it?
I currently have this in my configuration under :lint-as
as suggested, but that doesn’t seem to work.
(ns my-ns
(:require [my-custom-record :refer [defrecord+]]))
; No lint issues here
(defrecord+ MyRecord [id data])
(ns my-other-ns
(:require [my-ns :as mns]))
; unresolved var
(mns/make->MyRecord {:id id :data data})
Yeah just to be clear, I’m not getting lint issues on the usage of defrecord+
its from the custom function it generates.
ah ok. your option here is to write a custom config macro or hook. See https://github.com/clj-kondo/clj-kondo/blob/master/doc/hooks.md
Something which just expands into
(do (declare make->MyRecord) (defrecord MyRecord [] ...)
or so will doAh ok, so I can set the :analyze-call
for the defrecord+
macro, and I won’t need to configure :analyze-call
for each of the make->X
functions?
there is :analyze-call
but also :macroexpand
which will probably be easier to start with, you can just provide a normal macro there, but it gives less precise warnings / locations
This question might be specific to clojure lsp or vscode, but can you recommend a good workflow for developing and testing these hooks?
Currently, use clj-kondo on the command line and insert printlns in the code for debugging, and then run it on examples
Yeah I had to smack myself when I realized I was working the really really hard way 🙂 using the command line was the way to go here! Hooks are working quite well for me now, thank you!
Though I ran into this issue that puzzled me. I thought it would be a no brainer to use :lint-as
but that was not working, I had to use a custom hook which seems like it would do the same thing. Any idea here?
(ns shared-lib.io.utils)
(defmacro with-tmp-file* [[sym f] & body] ...)
;; Usage like:
(io-utils/with-tmp-file* [tmp-file "asset"] ...)
Tried adding :lint-as
{:lint-as {shared-lib.io.utils/with-tmp-file* cojure.core/let}}
Did not work.
The hook works though:
(defn with-tmp-file* [{:keys [node]}]
(let [new-node (api/list-node
(list* (api/token-node 'let)
(rest (:children node))))]
{:node new-node}))
@UQE9SS4DP a typo: cojure.core/let