Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a more useful default error reporter for cljs instrumentation #678

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions src/malli/dev/cljs.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#?(:cljs (:require-macros [malli.dev.cljs]))
#?(:cljs (:require [malli.instrument.cljs]))
#?(:clj (:require [malli.clj-kondo :as clj-kondo]
[malli.dev.pretty :as pretty]
[malli.instrument.cljs :as mi])))

#?(:clj (defmacro stop!
Expand All @@ -24,11 +23,11 @@
#?(:clj (defmacro start!
"Collects defn schemas from all loaded namespaces and starts instrumentation for
a filtered set of function Vars (e.g. `defn`s). See [[malli.core/-instrument]] for possible options.
Differences from Clojure malli.dev/start:
Differences from Clojure `malli.dev/start!`:

- Does not emit clj-kondo type annotations.
- Does not emit clj-kondo type annotations. See `malli.clj-kondo/print-cljs!` to print clj-kondo config.
- Does not re-instrument functions if the function schemas change - use hot reloading to get a similar effect."
([] (start!* &env {:report `(pretty/reporter)}))
([] (start!* &env {}))
([options] (start!* &env options))))

#?(:clj (defmacro collect-all! [] (mi/-collect-all-ns)))
21 changes: 15 additions & 6 deletions src/malli/instrument/cljs.clj
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,26 @@
;; instrument
;;

(defn -emit-instrument-fn [env {:keys [gen filters] :as instrument-opts} {:keys [schema] :as schema-map} ns-sym fn-sym]
(defn -emit-instrument-fn [env {:keys [gen filters report] :as instrument-opts} {:keys [schema] :as schema-map} ns-sym fn-sym]
;; gen is a function
(let [schema-map (-> schema-map
(select-keys [:gen :scope :report])
;; The schema passed in may contain cljs vars that have to be resolved at runtime in cljs.
(assoc :schema `(m/function-schema ~schema)))
schema-map-with-gen
(as-> (merge (select-keys instrument-opts [:scope :report :gen]) schema-map) $
;; use the passed in gen fn to generate a value
(cond (and gen (true? (:gen schema-map))) (assoc $ :gen gen)
:else (dissoc $ :gen)))
schema-map-with-gen (as-> (merge (select-keys instrument-opts [:scope :report :gen]) schema-map) $
;; use the passed in gen fn to generate a value
(if (and gen (true? (:gen schema-map)))
(assoc $ :gen gen)
(dissoc $ :gen)))
;; adds a more useful report function if none is provided.
schema-map-with-gen (if report
schema-map-with-gen
(assoc schema-map-with-gen :report
`(cljs.core/fn [type# data#]
(throw (cljs.core/ex-info
(cljs.core/str type# " error for instrumented function " '~fn-sym)
(assoc (cljs.core/select-keys data# [:value :args])
:type type# :fn '~fn-sym))))))
replace-var-code (when (ana-api/resolve env fn-sym)
`(do
(swap! instrumented-vars #(assoc % '~fn-sym ~fn-sym))
Expand Down