Fork me on GitHub
#clj-kondo
<
2022-07-31
>
pinkfrog03:07:26

For some reason (potemkin is not supported on bb), I am directly using potemkin.namespace, How can I instruct clj-kondo to treat potemkin.namspaces/import-vars as if using potemkin/import-vars ?

rolt13:07:32

with :lint-as probably

Matthew Davidson (kingmob)06:08:21

Heh, funnily enough, I was adding this to Potemkin this weekend

pinkfrog08:08:45

@U10EC98F5 Adding this? I am writing a lib, so I don’t wanna the user to tweak with :lint-as.

Matthew Davidson (kingmob)11:08:55

If you come up with some good hooks, please share? :lint-as worked for many, but I still need to straighten out how import-vars works when I get back to it.

pinkfrog15:08:40

@U04V15CAJ Do you have any thoughts on how to achieving lint-as with import-vars from a lib ?

borkdude15:08:04

@UGC0NEP4Y clj-kondo should just add support for the multi segment potemkin namespace. @U10EC98F5 what is the final name going to be?

pinkfrog05:08:43

@U04V15CAJ Looking at the docs, https://cljdoc.org/d/clj-kondo/clj-kondo/2022.06.22/doc/configuration#exporting-and-importing-configuration, it seems it is impossible to automatically enable the lint-as configuration . The user must activate that on its own.

borkdude08:08:33

imported configs are now automatically activated, if you stick to the suggested format

Matthew Davidson (kingmob)15:08:29

I wasn't planning to change the Potemkin namespaces unless necessary. Barring some Graal user complaining, that's unlikely

borkdude16:08:28

@U10EC98F5 Then I misunderstood.

Heh, funnily enough, I was adding this to Potemkin this weekend
I thought you were referring to the ns rename recently with some other lib. I'm mixing things up ;)

borkdude16:08:41

perhaps potemin isn't problematic with graalvm since it runs at compile time

Matthew Davidson (kingmob)04:08:01

> I was adding this to Potemkin this weekend I meant I was adding useful clj-kondo hints 😄

Matthew Davidson (kingmob)04:08:56

On that note, a question: If I use a macro hook to transform import-vars :

(import-vars [ns1 foo bar]
             [ns2 baz qwerty])
which then becomes for kondo something like:
(def foo ns1/foo)
(def bar ns1/bar)
(def baz ns2/baz)
(def qwerty ns2/qwerty)
is that correct for teaching kondo about the defs? It seemed to work, for teaching kondo about names, but not for getting macro arity right for some of the imported macros. In particular, when I stopped, I was having issues with kondo recognizing the 2-arity version of import-fn, even though import-fn had its own hook. Maybe my misunderstanding is in how hooks work; even without using import-vars, my hook still isn’t working for the import-fn macro. The relevant hook code is:
(defn import-macro*
  ([sym]
   `(def ~(-> sym name symbol) ~sym))
  ([sym name]
   `(def ~name ~sym)))

(defmacro import-fn
  ([sym]
   (import-macro* sym))
  ([sym name]
   (import-macro* sym name)))
Any help appreciated!

borkdude09:08:25

> is that correct for teaching kondo about the defs? Yes - clj-kondo could do a better job seeing the connection between two vars. There is an issue about this

👍 1
Matthew Davidson (kingmob)14:08:45

Good news! What’s the issue #?

borkdude14:08:00

Feel free to upvote with a thumbs up or if you're really keen, you could try to work on. I kinda know what needs to happen, so I could guide you

Matthew Davidson (kingmob)15:08:45

Are there any code/branches I should look at? I’m not sure if/when I’ll get to it, but Potemkin (and proto-Potemkin) fns are all over Zach’s aleph/manifold/etc code, and I’m tired of staring at a sea of linter warnings. 😅 I can’t really use it for all the false positives.

borkdude15:08:38

Hmm, I could maybe help you with that, if you could give me a starting point

borkdude15:08:03

@U10EC98F5 There is a "proxy var" construct in clj-kondo based on what it detects based on the potemkin macro

borkdude15:08:14

And this same mechanism could be used for (def x y)

Matthew Davidson (kingmob)15:08:18

Huh, I hadn’t realized potemkin had special code already

borkdude15:08:35

yes, there is special code in clj-kondo to support the usage of potemkin

borkdude15:08:53

but this is based on potemkin/import-vars usage

Matthew Davidson (kingmob)15:08:43

Well, import-vars was my starting point

borkdude15:08:26

it already has support for that

borkdude15:08:03

you need to lint your project sources first though

borkdude15:08:08

before it can make the connections

Matthew Davidson (kingmob)15:08:46

But it doesn’t yet support the other issue, right? Understanding the arity of the “imported” vars

borkdude15:08:05

I think it does

borkdude15:08:24

there's test cases you could look at

Matthew Davidson (kingmob)15:08:07

Sure, I’m happy to take a look. I just checked, and it definitely doesn’t understand the import-fn macro’s arity, though

borkdude15:08:18

I thought you were referring to the arity of the imported var, not the import-vars macro itself. Perhaps you can just submit a failing test or so, I think we're talking past each other ;)

Matthew Davidson (kingmob)15:08:29

I added (import-fn unify-gensyms uniqlo) at the bottom of potemkin.clj, and I thought it would know that import-fn has a 2-arity version where the second one is a new symbol, and I thought I got the :macroexpand hook for that correct

borkdude15:08:04

clj-kondo only has support for import-vars - I've never heard of import-fn

Matthew Davidson (kingmob)15:08:24

If you add that snippet, does it complain about uniqlo?

borkdude15:08:48

but it could be potentially have that if it's a variation, I just don't know that construct

Matthew Davidson (kingmob)15:08:06

I think import-vars is really the important one. It uses import-fn under the hood

Matthew Davidson (kingmob)15:08:33

And hell, some of the potemkin stuff should also be discouraged…

👍 1
Matthew Davidson (kingmob)15:08:06

import-fn is not important per se, I was just trying to figure out what, or if, I was doing wrong when adding my own hook for it

borkdude15:08:37

maybe you could have a hook which expands that into import-vars 😆

borkdude15:08:00

and then it should work

Matthew Davidson (kingmob)15:08:45

and that’s how he froze his computer tonight

Matthew Davidson (kingmob)15:08:31

WOuld it be an infinite loop?

borkdude15:08:01

no, because clj-kondo handles that code directly, it doesn't expand to import-fn

Matthew Davidson (kingmob)15:08:28

Cool, well, I shouldn’t let the perfect be the enemy of the good. Even linting many of these should clear up a lot of linter warnings, I hope

sheluchin20:07:31

I'm wondering if there is any way to get the arguments list from functions that are defined like this: (def foo (fn [x] ...)). Any suggestions about how the analysis data could be expanded in this way?

borkdude20:07:11

In JVM Clojure, you don't get any arglists metadata for that. clj-kondo could do this, but why?

sheluchin20:07:54

I'm presuming the reason that LSP is able to show function arguments for defn functions and not fn functions is because clj-kondo provides more details about the defn. My rationale is that they're both accomplishing the same thing and it would be more consistent for clj-kondo to be able to provide as much of the same data as possible on both forms. But if you don't think it's a reasonable thing I think I can figure out how to do it by using something like clj-kondo.impl.analyzer/analyze-fn-arity. Looks like some of the tests use a fairly small ctx map in places where it's needed.

borkdude20:07:02

I think we could add this info. But why not just use defn instead?

sheluchin20:07:01

Good question 🙂 I don't know why people opt for the (def (fn approach, but they do. As for myself, I'm hacking away on a Clojure code exploration tool that leverages clj-kondo, so I'm at the mercy of the community.

borkdude21:07:36

OK, issue welcome. clj-kondo itself tracks arity info for this:

$ clj-kondo --lint - <<< '(def f (fn [])) (f 1 2)'
<stdin>:1:17: error: user/f is called with 2 args but expects 0
so I think it would be a small step to improve this

sheluchin21:07:09

Okay, thanks very much. I'll create an issue to track this.