Fork me on GitHub
#clj-kondo
<
2022-11-03
>
lassemaatta14:11:27

would it be technically possible for clj-kondo to detect if ^{:some-metadata "foo} was being applied to an unsuitable form?

borkdude14:11:19

technically yes :)

lassemaatta14:11:54

for example I was looking at some reagent code such as ^{:key id} (if foo? [:div] [:span]), which according to my very limited understanding of metadata is incorrect

borkdude15:11:47

it's not incorrect as in, the clojure reader will fail, but the metadata is discarded since if doesn't do anything with it. you should use with-meta here

lassemaatta15:11:36

indeed. But it seems like it's a relatively common mistake to make, I think I've seen it a couple of times because it's easy to assume it applies to the resulting value at runtime (like with-meta). Sure would be nice to notice problems like that without looking at the react warnings in the js/console 😉

lassemaatta15:11:25

but I have no idea is it possible to reliably detect when the metadata is being ignored

borkdude15:11:35

in general it's not really something you can detect that is "wrong" since the macro (or special form) might do something with this metadata

borkdude15:11:23

but e.g. ^{:foo} 1 is always wrong, since it's a reader error

Dumch18:11:35

Is there some helper function to get a value by key from the map-node?

borkdude18:11:49

@arturdumchev Currently there's not such a helper

Dumch18:11:21

I will check on weekends if there is a good solution to add one and maybe come up with the pr

borkdude18:11:38

Let's first discuss if this is a common enough problem to add something for

Dumch18:11:55

My case: macro gets a map as a param. I have to get nodes from this map to use with-meta further

borkdude18:11:02

can you describe your use case further? example call of the macro for example? why do you need to read from the map?

Dumch18:11:47

The codebase I am working now has a lot of macro like:

(macro-that-will-creat-several-functions
  {:get-function "name-of-the-get-fn"
   :set-funtion  "name-of-the-set-fn"
   :this-will-be-used-so-I-need-to-tell-clj-kondo-that-this-value-is-used pool-that-will-be-used-inside})

Dumch18:11:25

Maybe better way would be to avoid having a map here and just use keys and values

borkdude18:11:17

but since clj-kondo nodes don't have whitespace, it's relatively easy to transform them in a map:

(let [children (:children map)
      ks (take-nth 2 children)
      ks (map api/sexpr ks)
      vs (take-nth 2 (rest children))]
  (zipmap ks vs))
  

borkdude18:11:29

there you have the key s-expressions to nodes

Dumch18:11:09

thank you!