Fork me on GitHub
#clj-kondo
<
2023-10-09
>
borkdude11:10:58

Here is a proposal for an extension to the :consistent-alias linter which checks consistency of aliases between multiple files: https://github.com/clj-kondo/clj-kondo/issues/2195 I've previously not implemented this because it would not work in an editor, but for command line usage it's probably still useful, right?

👀 1
seancorfield15:10:30

I can see this being useful via clojure-lsp if it could figure out the most common alias used for a given ns and then flag all the other aliases as not matching that. I would not use this, however, because we have a convention of always using sut as the alias for the primary ns being tested in our test nses, so it would always flag those as inconsistent. We tend to use the alias linter almost solely for 3rd party nses.

phill15:10:43

sut ≟ stuff undergoing testing

borkdude16:10:31

well, we could build a configurable exclusion for sut etc of course

borkdude16:10:47

clojure-lsp already has the data it needs to implement this feature. clj-kondo could do it too but then you'd just get the feature when linting a directory, not via the editor (unless we keep more stuff in the on disk cache, which is kinda complicated)

ericdallo16:10:23

I do think this is quite useful, but IMO would be nice to have built-in clj-kondo similar to unresolved-var linter which has context about other ns

seancorfield16:10:41

If you have 100 uses of a ns and only one is inconsistent, the order you lint the code matters... If you see the inconsistent use first, the other 99 will be flagged; if you see it elsewhere, only that 1 use would be flagged:grin:

ericdallo16:10:32

AFAIK clj-kondo does that for unresolved-var lint which reports when you have a unknown function from an alias

borkdude16:10:32

sure, I'm willing to think about it but then we get at the "complicated caching" bit, since clj-kondo needs to know about all aliases used in the project for a certain namespace. the process would be like this, when in an editor:

(ns foo (:require [clojure.string :as str]))
store {clojure.string {str {foo}} or so (not sure which layout is most handy and then lint the next file in an editor:
(ns bar (:require [clojure.string :as string]))
Add to index
{clojure.string {str #{foo} string #{bar}})
Now we find that there are multiple aliases, so we can show that there is another namespace with a different alias. But if you remove (:require [clojure.string ]) from foo how do we update the index? There is not a single layout which is optimal for querying probably. When you have a server process like with clojure-lsp, it becomes a little easier

ericdallo16:10:59

Got it, how we update the cache for the unresolved-var case? indeed doing in clojure-lsp should be easier

borkdude16:10:36

unresolved-var and invalid-arity, etc etc are all just part of layout by namespace

borkdude16:10:10

.clj-kondo-cache/v1/foo/bar/baz.transit.edn
(there are some indirections in there per clojure dialect, but this is the main idea)

ericdallo16:10:58

I see, I think I or @U04CAPVN1HS can take a look how hard would be to implement as a new custom linter in clojure-lsp

borkdude16:10:28

I think the only difficult thing about the on disk cache is: how to efficiently detect removals, e.g. when you change or remove an alias, how would you efficiently update the on disk cache

borkdude16:10:42

but perhaps this problem is actually similar with clojure-lsp, but just in memory

ericdallo18:10:48

yep, we already do the same for unused-public-vars, so we have a ns dependency graph which helps both in performance and find that

1