noprompt / meander

Tools for transparent data transformation
MIT License
922 stars 55 forks source link

clj-kondo #247

Open awb99 opened 11 months ago

awb99 commented 11 months ago

This is not a bug of meander. I use meander and clj-kondo. The meander-templates trigger lots of :unresolved-symbol warnings in clj-kondo. I am looking for a way to ignore this warnigns by way of clj-kondo config. Any ideas?

bnert commented 10 months ago

Adding a #_:clj-kondo/ignore around a form causes clj-kondo to ignore the inner form.

Usages given readme example:

(require '[meander.epsilon :as m])

(defn favorite-food-info [foods-by-name user]
  #_:clj-kondo/ignore
  (m/match {:user user
            :foods-by-name foods-by-name}
    {:user
     {:name ?name
      :favorite-food {:name ?food}}
     :foods-by-name {?food {:popularity ?popularity
                            :calories ?calories}}}
    {:name ?name
     :favorite {:food ?food
                :popularity ?popularity
                :calories ?calories}}))

And if you want to get really granular:

(require '[meander.epsilon :as m])

(defn favorite-food-info [foods-by-name user]
  (m/match {:user user
            :foods-by-name foods-by-name}
    #_:clj-kondo/ignore
    {:user
     {:name ?name
      :favorite-food {:name ?food}}
     :foods-by-name {?food {:popularity ?popularity
                            :calories ?calories}}}
    #_:clj-kondo/ignore
    {:name ?name
     :favorite {:food ?food
                :popularity ?popularity
                :calories ?calories}}))

There is also the option of adding an extension, though I am not really familiar w/ doing that and haven't invested enough time to understand that process, so will leave it as merely a mention.

bsless commented 10 months ago

Rough starting point, but courtesy of @borkdude

;; .clj-kondo/meander/epsilon.clj:
(ns meander.epsilon
  (:require [clojure.string :as str]
            [clojure.walk :as walk]))

(defn collect-vars [expr]
  (let [vars (volatile! [])]
    (walk/postwalk (fn [x]
                     (when (and (symbol? x)
                                (str/starts-with? (str x) "?"))
                       (vswap! vars conj x))
                     x)
                   expr)
    @vars))

(defmacro rewrite [expr & exprs]
  (let [ret `(do ~expr
                 ~@(map (fn [[lhs rhs]]
                          `(fn ~(collect-vars lhs)
                             ~rhs))
                        (partition 2 exprs)))]
    ;; (prn ret)
    ret))