Fork me on GitHub
#clj-kondo
<
2020-12-17
>
practicalli-johnny12:12:47

Is clj-kondo able to pick up files with - in the name rather than _ ? So if I had test/practicalli/lintme-test.clj instead of test/practicalli/lintme_test.clj I would get a warning.

borkdude12:12:29

clj-kondo will lint any file you throw at it

practicalli-johnny12:12:03

but it does not tell me I should have used an underscore. I assume this is out of scope

borkdude12:12:45

Still waiting for @pez's PR ;)

borkdude12:12:19

Feel free to "upvote" the issue with a thumbs up

👍 3
borkdude12:12:28

(this is Github's silly way to prioritize issues)

pez13:12:00

Haha, never heard the “Yes, fits this project” signal. 😃

pez13:12:43

Not that you didn’t send it. It was just lost somewhere in transit. Haha.

orestis13:12:10

So I’ve put better-cond in my codebase… and clj-kondo can’t read it (obviously). I started implementing a hook for it but it’s tricky. @borkdude I remember you used it as an example but I’m quite lost since the discussions were using a different syntax.

borkdude13:12:28

which I used to test-drive the hook functionality. But I'm not sure how complete it is

orestis14:12:25

It’s not complete, but it’s a good starting point. I’ll bother you with some things if I don’t manage to finish it 🙂

👍 3
orestis15:12:30

Do you have a way to easily debug the various expressions? I’m trying to call api/sexpr but it seems to only work on the top level and I’d like to avoid writing the recursive version 😛

borkdude15:12:16

@orestis Just call prn or println on the node

orestis15:12:00

I get [<token: do> <list: (whena(dob))>] (no spacing)

orestis15:12:30

which I think means:

(do (when a (do b))) 
but I’m not sure

borkdude15:12:31

this is usually a sign that you garbled something

orestis15:12:40

ah good to know 🙂

borkdude15:12:00

it seems you have a vector of nodes and not a node object itself

orestis15:12:42

Isn’t that common though? When you are taking the children of a node and want to do stuff?

borkdude15:12:42

this happens to me too, just keep printing and iterating ;)

borkdude15:12:15

yes, but you should put the children back into some node: (api/vector-node children) and not [children] in order for it to be a valid node

orestis15:12:17

Ah right, I was trying to figure out if it should be a vector-node or a list-node or what?

orestis16:12:38

I’m getting the hang of this:

(defn process-pairs [pairs]
  (loop [[[lhs rhs :as pair] & pairs] pairs
         new-body [(api/token-node 'do)]]
    (if pair
      (let [lhs-sexpr (api/sexpr lhs)]
        (clojure.core/cond
          ;; single trailing element, flush the body
          (= 1 (count pair))
          (api/list-node (conj new-body lhs))

          ;; when node, recurse
          (#{:when 'when} lhs-sexpr)
          (api/list-node 
            (conj new-body (api/list-node
                             [(api/token-node 'when)
                              rhs
                              (process-pairs pairs)])))

          ;; let-like nodes
          (#{:let :when-let :when-some
             'let 'when-let 'when-some} lhs-sexpr)
          (api/list-node (conj new-body 
                               (api/list-node 
                                 [(api/token-node 'let)
                                  rhs 
                                  (process-pairs pairs)])))

          ;; normal conditions, just add them to the body
          :else
          (recur pairs
                 (conj new-body lhs rhs))))
      ;; no trailing element, flush the body
      (api/list-node new-body))))

(def cond
  (fn [{:keys [node]}]
    (let [expr (let [args (rest (:children node))
                     pairs (partition-all 2 args)]
                 (process-pairs pairs))]
      ;(println "COND" {:node (api/sexpr expr)})
      {:node (with-meta expr
               (meta node))})))

orestis16:12:30

This deals with most cases — but not the “early bailing” ones (e.g. when-let) because the type-checking continues…

borkdude16:12:09

I bet better-cond would come in handy here ;)

orestis16:12:10

(b/cond
  :when-let [b nil]
  (+ 1 b))

orestis16:12:41

In reality, (+ 1 b) is never evaluated because b is nil, but clj-kondo flags: expected number, got nil

borkdude16:12:42

ah, that gives a false positive?

borkdude16:12:19

who uses nil in when-let though?

borkdude16:12:28

that's not a realistic use case

orestis16:12:32

It’s not too bad in practice since you never use explicit nils but doing the full thing would probably mean just lifting the implementation 1-1

orestis16:12:48

Gotta run 🙂 I’ll continnue this tomorrow.

Oliver George23:12:09

Idle thought: do you imagine clj-kondo might grow to include a --autofix flag to make the obvious corrections (e.g. remove redundant lets, remove unused bindings, convert if to when...)