Skip to content

Instantly share code, notes, and snippets.

@verma
Created February 11, 2021 21:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save verma/58a6c170189b5cf3dc9bf4103ce2e475 to your computer and use it in GitHub Desktop.
Save verma/58a6c170189b5cf3dc9bf4103ce2e475 to your computer and use it in GitHub Desktop.
(ns hooks.omtools
(:require [clj-kondo.hooks-api :as api]))
(defn valid-form? [node]
(reduce (fn [err n]
(let [sexpr (api/sexpr n)]
(if (and (vector? sexpr)
(not= (first sexpr) :data)
(not= (first sexpr) :state))
(let [{:keys [row col]} (meta n)]
(api/reg-finding! {:message (str "Unrecognized binding: " (first sexpr) ". Only :data and :state binding forms are valid")
:type :omtools/invalid-binding
:row row
:col col})
false)
err)))
true
(:children node)))
(defn tap [v]
(prn (api/sexpr v))
v)
(defn t [v]
(prn v)
v)
(defn wrap-body
"The closest structure we have to our body is a body of letfn, so lets try wrapping it in"
[body-node]
(api/list-node
(list
(api/token-node 'letfn)
(api/vector-node
(map (fn [node]
(let [[_ & r] (:children node)]
(api/list-node
(list* (api/token-node '_)
r))))
body-node)))))
(defn defn-node-with-flat-mapping [component-name bindings-node body-node]
(let [as-vec (api/sexpr bindings-node)
flattened (mapcat (fn [n]
(let [v (api/sexpr n)]
(if (vector? v)
(rest v)
v)))
as-vec)]
(api/list-node
(list
(api/token-node 'defn)
component-name
(api/vector-node flattened)
(wrap-body body-node)))))
(defn defcomponentk [{:keys [node]}]
(let [[_ component-name bindings & body] (:children node)]
(when (valid-form? bindings)
{:node (tap (defn-node-with-flat-mapping component-name bindings body))})))
(ns hooks.omtools
(:require [clj-kondo.hooks-api :as api]))
(defn valid-form? [node]
(reduce (fn [err n]
(let [sexpr (api/sexpr n)]
(if (and (vector? sexpr)
(not= (first sexpr) :data)
(not= (first sexpr) :state))
(let [{:keys [row col]} (meta n)]
(api/reg-finding! {:message (str "Unrecognized binding: " (first sexpr) ". Only :data and :state binding forms are valid")
:type :omtools/invalid-binding
:row row
:col col})
false)
err)))
true
(:children node)))
(defn tap [v]
(prn (api/sexpr v))
v)
(defn t [v]
(prn v)
v)
(defn wrap-body
"The closest structure we have to our body is a body of letfn, so lets try wrapping it in"
[body-node]
(api/list-node
(list
(api/token-node 'letfn)
(api/vector-node
(map (fn [node]
(let [[_ & r] (:children node)]
(api/list-node
(list* (api/token-node '_)
r))))
body-node)))))
(defn defn-node-with-flat-mapping [component-name bindings-node body-node]
(let [as-vec (api/sexpr bindings-node)
flattened (mapcat (fn [n]
(let [v (api/sexpr n)]
(if (vector? v)
(rest v)
v)))
as-vec)]
(api/list-node
(list
(api/token-node 'defn)
component-name
(api/vector-node flattened)
(wrap-body body-node)))))
(defn defcomponentk [{:keys [node]}]
(let [[_ component-name bindings & body] (:children node)]
(when (valid-form? bindings)
{:node (tap (defn-node-with-flat-mapping component-name bindings body))})))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment