Fork me on GitHub
#clj-kondo
<
2024-04-24
>
Lilitha Potye11:04:51

Hi Team, we want to discourage other Clojure Engineers in our company from using js/fetch. We intend on implementing a js-fetch function which will add some extra instrumentation to requests before they are sent. I am trying to use clj-kondo for this, however, I am having difficulties with getting the discouraged-var linter to work with js/fetch. Custom hooks also suffer from the same fate. For example, this linter works as expected for web.http/js-fetch but not for js/fetch

:discouraged-var {js/fetch {:message "Use web.http/js-fetch instead"}
                    web.http/js-fetch {:message "Use web.http/js-fetch instead"}}

borkdude11:04:19

This isn’t yet supported with clj-kondo but issue welcome

1
Lilitha Potye11:04:47

Cool, I'll open an issue 👍:skin-tone-5:

borkdude11:04:25

What you could do as a workaround is redefine js/fetch in your runtime to make it throw when you use it

Lilitha Potye15:04:33

Okay, that's an idea. I'll try that out and see if it works for us. Thank you!

borkdude15:04:52

or simply: (js-delete js/globalThis "fetch")

Lilitha Potye15:04:38

So our hope was to wrap ‘js/fetch’ in a new function. Would we still be able to access it if we deleted it?

borkdude15:04:16

yes,

(def ^:private fetch js/fetch)
(js-delete js/globalThis "fetch")

(defn foobar [x]
  (fetch x))  

Lilitha Potye15:04:31

Cool, this is a great idea. I’ll use it thanks you 👍:skin-tone-5:

borkdude15:04:04

note that this could mess with libraries that are using fetch

borkdude15:04:14

you could also redefine fetch in a similar way, to instrument it

Lilitha Potye15:04:49

Nice catch. Would I have to define a fetch function in a js namespace? :thinking_face:

borkdude15:04:10

js/... just means the global context. it would be the same as writing: (set! js/globalThis.fetch ...)

borkdude15:04:35

but (set! js/fetch ...) also works

Lilitha Potye16:04:53

Nice but that might be confusing for people who dint know we’ve redefined it. They might expect to have to translate EDN into JSON for example.

Lilitha Potye16:04:59

My instinct for instance when I see js is to use some interop.

borkdude18:04:43

I'm fine with supporting your use case in clj-kondo, just needs an issue

Lilitha Potye05:04:01

Awesome, thank you so much for the help! I've created an https://github.com/clj-kondo/clj-kondo/issues/2318 (EDIT: I deleted this one and created a new one using my work account)

Lilitha Potye06:04:16

Sorry, I made a bit of a blunder. I created the issue using my personal GitHub account. Here's thehttps://github.com/clj-kondo/clj-kondo/issues/2319