Fork me on GitHub
#clj-kondo
<
2022-09-14
>
flowthing08:09:36

Is there a way to tell clj-kondo to think of a .cljc file as a .bb and .clj file instead of a .clj and .cljs file? For example, given test.cljc:

(Character/isWhitespace "")
clj-kondo says that Character is an unresolved namespace even though both Babashka and Clojure support it.

borkdude08:09:08

@flowthing You can do :cljc {:features [:clj]} to only lint .cljc as .clj

borkdude08:09:18

which I use typically for bb-compatible stuff

flowthing08:09:39

Cool, that works, thanks. :thumbsup:

sheluchin14:09:34

I notice that var-definitions sometimes contain nil values for :row and :col. I'm wondering if this might be a bug. Details in ๐Ÿงต

sheluchin14:09:20

For example, this definition https://github.com/fulcrologic/fulcro-rad-semantic-ui/blob/c22ab504d6030e1174a3166e1f1e6f5f5f597f11/src/main/com/fulcrologic/rad/rendering/semantic_ui/autocomplete.cljc#L31-L41 produces the following analysis data:

{:fixed-arities #{1}
       :end-row nil
       :name-end-col 31
       :name-end-row 31
       :name-row 31
       :ns com.fulcrologic.rad.rendering.semantic-ui.autocomplete
       :name normalize-options
       :defined-by clojure.core/defn
       :lang :clj
       :filename "... /resources/repos/fulcrologic/fulcro-rad-semantic-ui/src/main/com/fulcrologic/rad/rendering/semantic_ui/autocomplete.cljc"
       :col nil
       :name-col 14
       :end-col nil
       :row nil}

borkdude14:09:39

might be a bug

sheluchin14:09:58

@U04V15CAJ I thought so, but wanted to check with you first just in case. Doesn't look like a result you would expect for such a definition?

sheluchin14:09:44

Notice that other location fields do contain a value.

borkdude14:09:28

As I said, might be a bug

borkdude14:09:01

Feel free to post an issue, not sure why it happens. I or you or someone else needs to take a look

sheluchin14:09:05

Alrighty, thank you. I'll have a shot at a fix.

๐Ÿ™ 1
sheluchin16:09:48

@U04V15CAJ I have a fix but I don't know how to write a test for it and I don't fully understand why the code is written the way it is, probably because I haven't studied how rewrite-clj works. The problem is https://github.com/clj-kondo/clj-kondo/blob/787a8f75df65d82794c1cc8407d88a7b93a4c298/src/clj_kondo/impl/namespace.clj#L100-L104. reg-var! expects to find the missing location fields to be in the (meta expr), but it's in the passed in metadata for this definition and others like it which fail. Replacing the let binding with:

expr-row (or (:row m) (:row metadata))
fixes it. Any suggestions?

borkdude16:09:26

I see. I'm not sure why we don't always use "metadata", this is a bit messy

borkdude16:09:17

I'm fine with adding or there, if that works

sheluchin17:09:04

Seems to. I'll do a bit more manual testing, but not sure how to unit test such a case because the var definition I'm testing against is a custom macro.

borkdude17:09:37

with a hook or so?

sheluchin19:09:15

Sorry, I don't yet understand how to do that. There is already a hook for defmutation that Fulcro https://github.com/fulcrologic/fulcro/blob/6b53c03787344db00cd7f4b92f779e861cc2fcb3/src/clj-kondo/clj-kondo.exports/com.fulcrologic/fulcro/com/fulcrologic/fulcro/clj_kondo_hooks.clj#L4. Is there some way I can leverage that within the tests? I don't mean to bother you with this. If you would prefer I can just open a ticket or PR with the findings and solution so far.

borkdude19:09:03

I was just asking, how the custom macro was handled in clj-kondo

borkdude19:09:31

you can take a look at the hook tests how to do this

๐Ÿ‘€ 1
borkdude19:09:51

It could also be a sign that the hook doesn't properly attach the location metadata

sheluchin19:09:52

Good point. Reading through the hooks workshop made me wonder why the meta doesn't include the location information anyhow.

sheluchin21:09:11

That does look to be the case here. The location meta is on the incoming node but not the return value. Maybe all the (or (:name-col metadata) (:col metadata)) -like code in reg-var! is there just in case of situations like this?

borkdude21:09:39

Perhaps ... it's a while ago since I touched that

borkdude21:09:47

But I think a location is always better than none :)

sheluchin22:09:31

A wrong location could be worse than none :man-shrugging: As for the tests, I'm looking at clj-kondo.hooks-test, but it's not so clear to me. I'm to define a simplified hook similar to Fulcro's defmutation and put it either in a string right in the test, or in corpus/hooks, and then use it in a call to lint! to assert against?

sheluchin22:09:14

BTW, there are quite a few libraries out there that produce such analysis results. Maybe there should be a warning or something somewhere?

borkdude22:09:50

there might also be tests in analysis_test regarding hooks, I'm not sure

borkdude22:09:51

ah good, well, similar to that one then ;)

borkdude22:09:08

you could also put the code in a directory in corpus if you prefer that

sheluchin11:09:30

https://github.com/clj-kondo/clj-kondo/issues/1812 There's an issue for it. I think I have a fix PR as well. Thank you for the guidance, @U04V15CAJ ๐Ÿ™

borkdude11:09:44

Merged, and thank you!

clj-kondo 1
Jacob Emcken15:09:58

I have a ClojureScript project (in Calva) where I am getting a Unused public var caused by the "entry point" to the project. How do I tell the linter to accept that nothing (inside the project) is referencing this function? I've tried adding the following to .clj-kondo/config.edn to no avail:

{:linters {:clojure-lsp/unused-public-var {:level :warning
                                           :exclude #{my-ns/handler}}}}
While adding the following in front does indeed make "the problem" go away... but I would like these things outside the code:
(ns my-ns)

#_{:clj-kondo/ignore [:clojure-lsp/unused-public-var]}
(defn handler
...

borkdude15:09:57

Unused public var is not a clj-kondo feature but a #lsp feature. Can you post your question there? I'm not sure how that config works. Or maybe @UKFSJSM38 can reply here ๐Ÿงต

Jacob Emcken15:09:12

Oh, I was just reading this example here (which references .clj-kondo/config.edn), so I just assumed ๐Ÿ˜ฌ https://clojure-lsp.io/settings/#clojure-lspunused-public-var

borkdude15:09:14

yeah, it's a custom linting rule

borkdude15:09:27

it's fine to ask the question here as well, but you might get better replies in #lsp

๐Ÿ‘ 1
ericdallo15:09:31

Out of curiosity @USW6URK47, what or where calls that handler funcion?

Jacob Emcken15:09:43

an AWS lambda (Node.js)

Jacob Emcken15:09:33

it is built with Shadow-cljs:

...
:builds {:lambda {:target  :node-library
                  :exports {:handler my-ns/handler}
...

ericdallo15:09:04

I see, so it comes from shadow, I'm wondering if we could get analytis from shadow edn as well @U04V15CAJ in the future along with bb.edn

borkdude16:09:37

Wasn't the original question why the config didn't work?

ericdallo16:09:29

I mean, if we have analysis on shadow-cljs.edn , then we would know that function is being called there, so users wouldn't need to specify which function they want to exclude from linter

borkdude16:09:00

sure, but excluding should work no matter what

ericdallo16:09:30

ah yes, excluding via code and config should work indeed, @USW6URK47 just add the linter config on .clj-kondo/config.edn as mentioned https://clojure-lsp.io/settings/#clojure-lspunused-public-var

borkdude16:09:03

@UKFSJSM38 He already did that. That was the original issue: that this didn't work for him. See original post.

ericdallo17:09:26

Got it, sorry, I misread the question. @USW6URK47 Hum, it does seems to work for me with:

(my-ns)

(defn foo [])
.clj-kondo/config.edn
{:linters {:clojure-lsp/unused-public-var {:exclude #{my-ns/foo}}}} 
Or
{:linters {:clojure-lsp/unused-public-var {:exclude #{my-ns}}}} 

โค๏ธ 1
ericdallo17:09:56

Make sure to re-edit de buffer after changing a config to re-lint the file

ericdallo17:09:50

(BTW, that sounds like a good nice to have, clojure-lsp could re-lint the project when user changes a config.edn: https://github.com/clojure-lsp/clojure-lsp/issues/1247)

๐Ÿ‘ 2
Jacob Emcken04:09:38

I found my issue... though I don't understand why it would have any effect at all ๐Ÿ˜ฎ I had something commented out like so:

{:linters {:clojure-lsp/unused-public-var {:exclude #{my-ns/foo}
                                           #_#_:exclude-regex #{"my-ns\..*-test"}}}}

Jacob Emcken05:09:10

When I remove #_#_:exclude-regex #{"my-ns\..*-test"} it works. Thanks for your patience, both

Jacob Emcken05:09:44

:thinking_face: I figured it out... I was tricked by the (single) back-slash

๐Ÿ‘ 2
ericdallo00:09:44

About https://github.com/clojure-lsp/clojure-lsp/issues/1247, this was done on master :) Every time user save a clj-kondo or lsp config, we re-lint all opened files so the UX is smoother

๐Ÿ‘ 1