clj-kondo

https://github.com/clj-kondo/clj-kondo
Karol Wójcik 2020-08-09T09:33:51.307100Z

@borkdude Ok almost got it. The only issue is that I'm receiving unsupported binding form input, another warning:

(ns hooks.rum.defc
  (:require
   [clj-kondo.hooks-api :as api]))

(defn defcs
  [{:keys [node]}]
  (let [args (rest (:children node))
        component-name (first args)
        args (next args)
        [fn-args body] (loop [args* args
                              mixins []]
                         (if (seq args*)
                           (let [arg (first args*)]
                             (if (vector? (api/sexpr arg))
                               (let [fn-args (api/sexpr arg)
                                     fn-args-without-state (vec (rest fn-args))]
                                 [(with-meta (api/vector-node fn-args-without-state) (meta arg))
                                  (concat mixins (rest args*))])
                               (recur (rest args*)
                                      (conj mixins arg))))
                           [nil nil]))
        new-node (when fn-args
                   (with-meta (api/list-node [(api/token-node 'defn)
                                              component-name
                                              fn-args
                                              (api/list-node
                                               (list*
                                                (api/token-node 'let)
                                                (api/vector-node
                                                 [(api/token-node 'state) (api/token-node {})])
                                                body))])
                     (meta node)))]
    {:node new-node}))
demo.cljs
(ns demo
  (:require
   [rum.core :as rum]))

(rum/defcs SomeComponent <
  {:did-mount (fn [state] state)}
  [state input another]
  (let [x "Hello"]
    nil))

(rum/defc SomeComponent1 <
  {:did-mount (fn [state] state)}
  [input]
  input)

(SomeComponent "hello")
Any ideas how to fix it?"

borkdude 2020-08-09T09:37:02.307500Z

Can you print the sexpr using (println (api/sexpr new-node)) and show what it makes?

Karol Wójcik 2020-08-09T09:39:19.307800Z

(defn SomeComponent [input another] (let [state {}] < {:did-mount (fn [state] state)} (let [x Hello] nil)))

borkdude 2020-08-09T09:40:48.308Z

Can you post the raw output without editing it?

borkdude 2020-08-09T09:41:36.308200Z

Maybe use prn instead of println to preserve the string

Karol Wójcik 2020-08-09T09:43:31.308400Z

(defn SomeComponent [input another] (let [state {}] < {:did-mount (fn [state] state)} (let [x "Hello"] nil)))

borkdude 2020-08-09T09:44:24.308600Z

Btw, I did get the correct linting with your code

borkdude 2020-08-09T09:44:54.308800Z

borkdude 2020-08-09T09:45:19.309300Z

borkdude 2020-08-09T09:45:56.309800Z

Are you use your config.edn is edited properly?

borkdude 2020-08-09T09:46:12.310Z

I now have this:

{:hooks {:analyze-call {rum.core/defc hooks.rum.defc/defc
                        rum.core/defcs hooks.rum.defc/defcs}}
 :lint-as {;; rum.core/defcs rum.core/defc
           rum.core/defcc rum.core/defc}}

borkdude 2020-08-09T09:46:48.310200Z

So, all looking OK here

Karol Wójcik 2020-08-09T09:48:23.310400Z

Config:

{:hooks {:analyze-call {rum.core/defc hooks.rum.defc/defc
                        rum.core/defcs hooks.rum.defc/defcs}}
 :lint-as {rum.core/defcc rum.core/defc
           view.core/FOR clojure.core/for
           view.core/KEEP clojure.core/keep}
 :linters {:missing-else-branch {:level :off}}}

borkdude 2020-08-09T09:48:28.310600Z

I'm afk for a few hours now.

Karol Wójcik 2020-08-09T09:49:13.310800Z

Maybe wrong version of clj-kondo: clj-kondo v2020.07.29

borkdude 2020-08-09T09:49:50.311Z

that's the latest

borkdude 2020-08-09T09:50:06.311200Z

afk

Karol Wójcik 2020-08-09T09:51:02.311400Z

Ok 🙂 I'm getting that errors only if I run clj-kondo --lint demo.cljs

borkdude 2020-08-09T09:51:28.311600Z

Then maybe in that case it doesn't pick up on your config

borkdude 2020-08-09T10:17:12.312700Z

You should run it from the directory that has .clj-kondo in it

Karol Wójcik 2020-08-09T10:18:21.312900Z

I'm doing it 🙂

borkdude 2020-08-09T14:53:04.313100Z

I'm back now. So, I'm getting good results with your code. What I've done is add it in libraries/rum/.clj-kondo/hooks/rum/defc.clj and added the entry in config.edn. Then I made the screenshots

Karol Wójcik 2020-08-09T16:02:39.313300Z

@borkdude can you please use command clj-kondo --lint demo.cljs and check whether no errors are reported?

borkdude 2020-08-09T16:11:36.313500Z

Now you're sounding like me, I say this all the time to people. Yes, I'll try it after dinner :)

1👍1❤️
Karol Wójcik 2020-08-09T16:44:17.313700Z

haha 😄 Thank you very much! I'm just wrapping my head around why in flycheck buffer those errors are not visible whereas in terminal I can spot them. I think those are internal errors of clj-kondo.

robert-stuttaford 2020-08-09T16:54:26.314200Z

all i have to add at this point is: 👏👏👏👏👏👏👏👏👏👏

borkdude 2020-08-09T17:14:02.314400Z

@karol.wojcik Works correctly here: src/example.cljs:25:9: warning: unused binding x src/example.cljs:28:1: error: demo/SomeComponent is called with 1 arg but expects 2

borkdude 2020-08-09T17:14:59.314600Z

When I put that code in src/example.cljs that is

borkdude 2020-08-09T17:15:09.314800Z

When linted separately, I do see different errors... hmm

borkdude 2020-08-09T17:15:40.315Z

Ah I see:

src/example.cljs::: error: unsupported binding form x
src/example.cljs::: error: unsupported binding form input
src/example.cljs::: error: unsupported binding form another

borkdude 2020-08-09T17:15:53.315200Z

I'll start debugging, brb

borkdude 2020-08-09T17:45:12.315400Z

OK fixed it. The mistake that was in your hook code is that you inserted sexprs back into the node instead of proper nodes. I took the liberty to just start from scratch and adapt the existing defc one, in this commit: https://github.com/borkdude/clj-kondo/commit/9f004622d0c69a6baf93bb63243306a7098f0941

1❤️
borkdude 2020-08-09T17:45:39.315600Z

If that code isn't sufficient yet, feel free to make another PR

borkdude 2020-08-09T17:47:40.315800Z

Also I renamed libraries to examples to fix the illusion that clj-kondo will pick up on those automatically, it won't, you have to copy the code yourself

borkdude 2020-08-09T17:51:53.316300Z

one other detail: when introducing a let, you can get warnings about a nested let. you can prevent this by using let*, this should probably go into the docs

borkdude 2020-08-09T17:53:08.316500Z

I'm pretty happy that this can be fixed in user-land code, so I don't have to publish a new release for each tweak :)

1❤️
Karol Wójcik 2020-08-09T17:58:25.316700Z

Thank you very much @borkdude !

borkdude 2020-08-09T18:16:14.317500Z

@martinklepsch @robert-stuttaford fwiw, there is a new commit on clj-kondo master which (hopefully) fixes the hook example for rum/defcs

martinklepsch 2020-08-09T18:16:39.318Z

Thanks for the ping, will try & report back :)

robert-stuttaford 2020-08-09T19:42:44.318600Z

stupendous!