-
-
Save verma/58a6c170189b5cf3dc9bf4103ce2e475 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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