Fork me on GitHub
#clj-kondo
<
2021-01-21
>
rickmoynihan08:01:36

What do people think of a clj-kondo linter that warns on aliased keyword usage? e.g.

(ns blah.blah (:require [foo.bar :as bar]))

::bar/baz ;; <- WARNING: aliased keyword 

:foo.bar/baz ;; <-- ok
i.e. to encourage find/replaceable keywords

didibus21:01:59

I think there's a linter that warns for inconssistent aliasing

didibus21:01:11

Not sure if it cover keywords

didibus21:01:29

But if it did, you could use that to still make it easy to find/replace, and still let people use aliases

didibus21:01:00

Basically, it forces the alias to be consistent across the board. So you know that you just need to find/replace that one alias.

borkdude08:01:35

@rickmoynihan @snoe and @ericdallo and currently working on adding keyword analysis output (for LSP) but this would also allow you to get all aliased keywords (and write a tool that spits out warnings). But it can also included in clj-kondo pretty easily and by default turned off.

rickmoynihan08:01:35

cool… so clj-kondo currently doesn’t preserve that sort of data or have keyword analysis, that would make this linter easy?

borkdude08:01:28

yes, the information is there, there is just not such a linter right now

rickmoynihan08:01:02

Similarly I was thinking of another for integrant keys, that ensures the keys are compatible with ig/load-namespaces e.g.

(ns foo.bar (:require [integrant.core :as ig]))

(defmethod ig/init-key :wibble/wobble  ;; <-- WARNING: integrant key does not match the namespace defining  ig/init-key not discoverable by ig/load-namespaces
   [_ opts] ,,,) 

(defmethod ig/init-key :foo.bar/baz  ;; ok
   [_ opts] ,,,) 

(defmethod ig/init-key :foo/bar  ;; ok
   [_ opts] ,,,) 

borkdude08:01:48

what would make wibble/wobble not compatible?

rickmoynihan08:01:50

the defmethod would need to be put in a namespace wibble.wobble or wibble (single segment namespaces aside)

rickmoynihan08:01:15

essentially ig/load-namespaces uses the key names specified in the integrant config to load the set of configured keys / namespaces. So it infers the namespace from the keywords name

borkdude09:01:47

I wonder if you could do this with a hook. I think you could but the namespace is currently not passed to the hook function. Can be added though.

borkdude09:01:23

How is ig/init-key :foo/bar ok here though?

borkdude09:01:00

shouldn't this be :foo.bar/something?

rickmoynihan09:01:16

no its ok because there are two rules for matching… I forget exactly but something like: 1. if you have the key :foo/bar try first to load the namespace foo.bar 2. if that fails try the namespace foo (and expect to find the ig/init-key :foo/bar in it)

rickmoynihan09:01:03

i.e. sometimes a namespaced keyword might fully identify a namespace (and a single component). Other times a namespace might contain many components; either is fine.

rickmoynihan09:01:22

my example is perhaps a bit confusing because I combined all the examples in a single ns. See here for the official docs: https://github.com/weavejester/integrant#loading-namespaces

borkdude09:01:11

Feel free to post issues about both your ideas

rickmoynihan09:01:44

Thanks, will do :thumbsup: just wanted to check they seemed feasible