Fork me on GitHub
#clj-kondo
<
2021-12-23
>
orestis08:12:08

Good morning. I'm wondering what's the best way to make clj-kondo work with macro-generated defs that come from static resources. Concrete example: we have a directory full of SVG icons, and each icon file gets turned into a var via a macro. Obviously when connected via the REPL, this works as expected and you also get completions. But clj-kondo rightfully complains as those vars don't appear in the source.

orestis08:12:06

The custom macroexpansion is an obvious place to try, but then it's mostly duplicating the code plus you'd need access to the classpath to locate the directory which contains the files.

borkdude08:12:28

In this case I would generate a stub file using clj-easy stub or manually

borkdude08:12:59

Or just add this namespace to the excludes in the unresolved var linter

orestis08:12:15

Oh that wouldn't work for cljs, hm.

borkdude08:12:55

Yeah, but you can apply a similar trick. Emit the list of files to a clj file in your kondo dir and then use that in the hook

orestis08:12:06

Hm, I'd like to avoid the manual sync step. Because in the end I could just evaluate the macro and paste the results back in the original file.

borkdude08:12:19

That will be the most robust solution and will work for both kondo and cursive

orestis08:12:20

BTW, The stub doesn't work, because the two namespaces are named the same (e.g. foo/icons.clj , foo/icons.cljs). The vars now live inside the foo/icons.clj while they're used from foo/icons.cljs. It seems like clj-kondo correctly looks at the CLJS file but other parts of the tooling is not.

orestis09:12:05

Hm, putting the literal defs (via eval/replace) into the clj file actually works, but I think it shouldn't?

borkdude09:12:31

why shouldn't it?

orestis09:12:27

The CLJS compiler isn't happy about that. If you have two namespaces with the same name, only macros make it automatically across, from CLJ->CLJS. So if from say nosco/login.cljs you require nosco.icons and you use nosco.icons/check , the check var has to be placed in nosco/icons.cljs, not nosco/icons.clj

orestis09:12:18

Whereas clj-kondo doesn't complain about undeclared vars no matter where the var is.

borkdude09:12:24

then why don't you emit this to a .cljs file?

orestis09:12:11

Oh I did now, just flagging this as something that surprised me on behalf of clj-kondo's behaviour. Interestingly, if I do the exact same via a stub it doesn't seem to work.

orestis09:12:27

I will probably need to start with a clean project and troubleshoot.

borkdude09:12:38

cool yeah, clj-kondo might be a little bit erring on the side of false negatives here

wilkerlucio11:12:00

one very simple solution is to use declare, in case the icons don't keep varying much it's easy to automate, and makes it work with every tool, example: https://github.com/fulcrologic/fulcro/blob/develop/src/main/com/fulcrologic/fulcro/dom.cljs#L17-L30

practicalli-johnny17:12:01

MegaLinter works great for my deps.edn file, although it uses an older version of clj-kondo and throwing a few errors about very new configs. I'm trying to encourage the https://github.com/megalinter/megalinter/pull/1119 I've also created a https://github.com/megalinter/megalinter/pull/1126