mirror of https://github.com/status-im/reagent.git
Merge pull request #301 from jeaye/assert-messages
Improve assert messages all around
This commit is contained in:
commit
dda0cfa0ad
|
@ -11,6 +11,7 @@
|
||||||
methods to provide React, you need to exclude this dependency and provide it yourself.
|
methods to provide React, you need to exclude this dependency and provide it yourself.
|
||||||
- Self-host compatibility ([#283](https://github.com/reagent-project/reagent/pull/283))
|
- Self-host compatibility ([#283](https://github.com/reagent-project/reagent/pull/283))
|
||||||
- Removed deprecated `reagent.interop/.'` and `reagent.interop/.!` macros
|
- Removed deprecated `reagent.interop/.'` and `reagent.interop/.!` macros
|
||||||
|
- Improve assert messages all around
|
||||||
|
|
||||||
## 0.6.2 (19.5.2017)
|
## 0.6.2 (19.5.2017)
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,10 @@
|
||||||
[reagent.impl.util :as util]
|
[reagent.impl.util :as util]
|
||||||
[reagent.impl.batching :as batch]
|
[reagent.impl.batching :as batch]
|
||||||
[reagent.ratom :as ratom]
|
[reagent.ratom :as ratom]
|
||||||
[reagent.debug :as deb :refer-macros [dbg prn]]
|
[reagent.debug :as deb :refer-macros [dbg prn
|
||||||
|
assert-some assert-component
|
||||||
|
assert-js-object assert-new-state
|
||||||
|
assert-callable]]
|
||||||
[reagent.interop :refer-macros [$ $!]]
|
[reagent.interop :refer-macros [$ $!]]
|
||||||
[reagent.dom :as dom]))
|
[reagent.dom :as dom]))
|
||||||
|
|
||||||
|
@ -30,13 +33,13 @@
|
||||||
([type]
|
([type]
|
||||||
(create-element type nil))
|
(create-element type nil))
|
||||||
([type props]
|
([type props]
|
||||||
(assert (not (map? props)))
|
(assert-js-object props)
|
||||||
($ react createElement type props))
|
($ react createElement type props))
|
||||||
([type props child]
|
([type props child]
|
||||||
(assert (not (map? props)))
|
(assert-js-object props)
|
||||||
($ react createElement type props child))
|
($ react createElement type props child))
|
||||||
([type props child & children]
|
([type props child & children]
|
||||||
(assert (not (map? props)))
|
(assert-js-object props)
|
||||||
(apply ($ react :createElement) type props child children)))
|
(apply ($ react :createElement) type props child children)))
|
||||||
|
|
||||||
(defn as-element
|
(defn as-element
|
||||||
|
@ -49,7 +52,7 @@
|
||||||
"Returns an adapter for a native React class, that may be used
|
"Returns an adapter for a native React class, that may be used
|
||||||
just like a Reagent component function or class in Hiccup forms."
|
just like a Reagent component function or class in Hiccup forms."
|
||||||
[c]
|
[c]
|
||||||
(assert c)
|
(assert-some c "Component")
|
||||||
(tmpl/adapt-react-class c))
|
(tmpl/adapt-react-class c))
|
||||||
|
|
||||||
(defn reactify-component
|
(defn reactify-component
|
||||||
|
@ -57,7 +60,7 @@
|
||||||
React, for example in JSX. A single argument, props, is passed to
|
React, for example in JSX. A single argument, props, is passed to
|
||||||
the component, converted to a map."
|
the component, converted to a map."
|
||||||
[c]
|
[c]
|
||||||
(assert c)
|
(assert-some c "Component")
|
||||||
(comp/reactify-component c))
|
(comp/reactify-component c))
|
||||||
|
|
||||||
(defn render
|
(defn render
|
||||||
|
@ -125,30 +128,30 @@
|
||||||
(defn state-atom
|
(defn state-atom
|
||||||
"Returns an atom containing a components state."
|
"Returns an atom containing a components state."
|
||||||
[this]
|
[this]
|
||||||
(assert (comp/reagent-component? this))
|
(assert-component this)
|
||||||
(comp/state-atom this))
|
(comp/state-atom this))
|
||||||
|
|
||||||
(defn state
|
(defn state
|
||||||
"Returns the state of a component, as set with replace-state or set-state.
|
"Returns the state of a component, as set with replace-state or set-state.
|
||||||
Equivalent to (deref (r/state-atom this))"
|
Equivalent to (deref (r/state-atom this))"
|
||||||
[this]
|
[this]
|
||||||
(assert (comp/reagent-component? this))
|
(assert-component this)
|
||||||
(deref (state-atom this)))
|
(deref (state-atom this)))
|
||||||
|
|
||||||
(defn replace-state
|
(defn replace-state
|
||||||
"Set state of a component.
|
"Set state of a component.
|
||||||
Equivalent to (reset! (state-atom this) new-state)"
|
Equivalent to (reset! (state-atom this) new-state)"
|
||||||
[this new-state]
|
[this new-state]
|
||||||
(assert (comp/reagent-component? this))
|
(assert-component this)
|
||||||
(assert (or (nil? new-state) (map? new-state)))
|
(assert-new-state new-state)
|
||||||
(reset! (state-atom this) new-state))
|
(reset! (state-atom this) new-state))
|
||||||
|
|
||||||
(defn set-state
|
(defn set-state
|
||||||
"Merge component state with new-state.
|
"Merge component state with new-state.
|
||||||
Equivalent to (swap! (state-atom this) merge new-state)"
|
Equivalent to (swap! (state-atom this) merge new-state)"
|
||||||
[this new-state]
|
[this new-state]
|
||||||
(assert (comp/reagent-component? this))
|
(assert-component this)
|
||||||
(assert (or (nil? new-state) (map? new-state)))
|
(assert-new-state new-state)
|
||||||
(swap! (state-atom this) merge new-state))
|
(swap! (state-atom this) merge new-state))
|
||||||
|
|
||||||
(defn force-update
|
(defn force-update
|
||||||
|
@ -166,19 +169,19 @@
|
||||||
(defn props
|
(defn props
|
||||||
"Returns the props passed to a component."
|
"Returns the props passed to a component."
|
||||||
[this]
|
[this]
|
||||||
(assert (comp/reagent-component? this))
|
(assert-component this)
|
||||||
(comp/get-props this))
|
(comp/get-props this))
|
||||||
|
|
||||||
(defn children
|
(defn children
|
||||||
"Returns the children passed to a component."
|
"Returns the children passed to a component."
|
||||||
[this]
|
[this]
|
||||||
(assert (comp/reagent-component? this))
|
(assert-component this)
|
||||||
(comp/get-children this))
|
(comp/get-children this))
|
||||||
|
|
||||||
(defn argv
|
(defn argv
|
||||||
"Returns the entire Hiccup form passed to the component."
|
"Returns the entire Hiccup form passed to the component."
|
||||||
[this]
|
[this]
|
||||||
(assert (comp/reagent-component? this))
|
(assert-component this)
|
||||||
(comp/get-argv this))
|
(comp/get-argv this))
|
||||||
|
|
||||||
(defn dom-node
|
(defn dom-node
|
||||||
|
@ -257,7 +260,7 @@
|
||||||
|
|
||||||
Probably useful only for passing to child components."
|
Probably useful only for passing to child components."
|
||||||
[value reset-fn & args]
|
[value reset-fn & args]
|
||||||
(assert (ifn? reset-fn))
|
(assert-callable reset-fn)
|
||||||
(ratom/make-wrapper value reset-fn args))
|
(ratom/make-wrapper value reset-fn args))
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -71,3 +71,26 @@ as well as package name and line number. Returns x."
|
||||||
~@forms)]
|
~@forms)]
|
||||||
(js/console.timeEnd label#)
|
(js/console.timeEnd label#)
|
||||||
res#)))
|
res#)))
|
||||||
|
|
||||||
|
(defmacro assert-some [value tag]
|
||||||
|
`(assert ~value (str ~tag " must not be nil")))
|
||||||
|
|
||||||
|
(defmacro assert-component [value]
|
||||||
|
`(assert (comp/reagent-component? ~value)
|
||||||
|
(str "Expected a reagent component, not "
|
||||||
|
(pr-str ~value))))
|
||||||
|
|
||||||
|
(defmacro assert-js-object [value]
|
||||||
|
`(assert (not (map? ~value))
|
||||||
|
(str "Expected a JS object, not "
|
||||||
|
(pr-str ~value))))
|
||||||
|
|
||||||
|
(defmacro assert-new-state [value]
|
||||||
|
`(assert (or (nil? ~value) (map? ~value))
|
||||||
|
(str "Expected a valid new state, not "
|
||||||
|
(pr-str ~value))))
|
||||||
|
|
||||||
|
(defmacro assert-callable [value]
|
||||||
|
`(assert (ifn? ~value)
|
||||||
|
(str "Expected something callable, not "
|
||||||
|
(pr-str ~value))))
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
(ns reagent.impl.batching
|
(ns reagent.impl.batching
|
||||||
(:refer-clojure :exclude [flush])
|
(:refer-clojure :exclude [flush])
|
||||||
(:require [reagent.debug :refer-macros [dbg]]
|
(:require [reagent.debug :refer-macros [dbg assert-some]]
|
||||||
[reagent.interop :refer-macros [$ $!]]
|
[reagent.interop :refer-macros [$ $!]]
|
||||||
[reagent.impl.util :refer [is-client]]
|
[reagent.impl.util :refer [is-client]]
|
||||||
[clojure.string :as string]))
|
[clojure.string :as string]))
|
||||||
|
@ -45,7 +45,7 @@
|
||||||
(deftype RenderQueue [^:mutable ^boolean scheduled?]
|
(deftype RenderQueue [^:mutable ^boolean scheduled?]
|
||||||
Object
|
Object
|
||||||
(enqueue [this k f]
|
(enqueue [this k f]
|
||||||
(assert (some? f))
|
(assert-some f "Enqueued function")
|
||||||
(when (nil? (aget this k))
|
(when (nil? (aget this k))
|
||||||
(aset this k (array)))
|
(aset this k (array)))
|
||||||
(.push (aget this k) f)
|
(.push (aget this k) f)
|
||||||
|
|
|
@ -3,7 +3,8 @@
|
||||||
[reagent.impl.batching :as batch]
|
[reagent.impl.batching :as batch]
|
||||||
[reagent.ratom :as ratom]
|
[reagent.ratom :as ratom]
|
||||||
[reagent.interop :refer-macros [$ $!]]
|
[reagent.interop :refer-macros [$ $!]]
|
||||||
[reagent.debug :refer-macros [dbg prn dev? warn error warn-unless]]))
|
[reagent.debug :refer-macros [dbg prn dev? warn error warn-unless
|
||||||
|
assert-callable]]))
|
||||||
|
|
||||||
(declare ^:dynamic *current-component*)
|
(declare ^:dynamic *current-component*)
|
||||||
|
|
||||||
|
@ -85,7 +86,7 @@
|
||||||
|
|
||||||
(defn wrap-render [c]
|
(defn wrap-render [c]
|
||||||
(let [f ($ c :reagentRender)
|
(let [f ($ c :reagentRender)
|
||||||
_ (assert (ifn? f))
|
_ (assert-callable f)
|
||||||
res (if (true? ($ c :cljsLegacyRender))
|
res (if (true? ($ c :cljsLegacyRender))
|
||||||
(.call f c c)
|
(.call f c c)
|
||||||
(let [v (get-argv c)
|
(let [v (get-argv c)
|
||||||
|
@ -201,8 +202,7 @@
|
||||||
(defn get-wrapper [key f name]
|
(defn get-wrapper [key f name]
|
||||||
(let [wrap (custom-wrapper key f)]
|
(let [wrap (custom-wrapper key f)]
|
||||||
(when (and wrap f)
|
(when (and wrap f)
|
||||||
(assert (ifn? f)
|
(assert-callable f))
|
||||||
(str "Expected function in " name key " but got " f)))
|
|
||||||
(or wrap f)))
|
(or wrap f)))
|
||||||
|
|
||||||
(def obligatory {:shouldComponentUpdate nil
|
(def obligatory {:shouldComponentUpdate nil
|
||||||
|
@ -225,8 +225,7 @@
|
||||||
render-fun (-> renders vals first)]
|
render-fun (-> renders vals first)]
|
||||||
(assert (pos? (count renders)) "Missing reagent-render")
|
(assert (pos? (count renders)) "Missing reagent-render")
|
||||||
(assert (== 1 (count renders)) "Too many render functions supplied")
|
(assert (== 1 (count renders)) "Too many render functions supplied")
|
||||||
(assert (ifn? render-fun) (str "Render must be a function, not "
|
(assert-callable render-fun)))
|
||||||
(pr-str render-fun)))))
|
|
||||||
(let [render-fun (or (:reagentRender fmap)
|
(let [render-fun (or (:reagentRender fmap)
|
||||||
(:componentFunction fmap))
|
(:componentFunction fmap))
|
||||||
legacy-render (nil? render-fun)
|
legacy-render (nil? render-fun)
|
||||||
|
@ -291,7 +290,7 @@
|
||||||
""))
|
""))
|
||||||
|
|
||||||
(defn fn-to-class [f]
|
(defn fn-to-class [f]
|
||||||
(assert (ifn? f) (str "Expected a function, not " (pr-str f)))
|
(assert-callable f)
|
||||||
(warn-unless (not (and (react-class? f)
|
(warn-unless (not (and (react-class? f)
|
||||||
(not (reagent-class? f))))
|
(not (reagent-class? f))))
|
||||||
"Using native React classes directly in Hiccup forms "
|
"Using native React classes directly in Hiccup forms "
|
||||||
|
|
|
@ -96,7 +96,8 @@
|
||||||
(if (nil? p1)
|
(if (nil? p1)
|
||||||
p2
|
p2
|
||||||
(do
|
(do
|
||||||
(assert (map? p1))
|
(assert (map? p1)
|
||||||
|
(str "Property must be a map, not " (pr-str p1)))
|
||||||
(merge-style p1 (merge-class p1 (merge p1 p2))))))
|
(merge-style p1 (merge-class p1 (merge p1 p2))))))
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -9,7 +9,7 @@
|
||||||
(defn- dot-args [object member]
|
(defn- dot-args [object member]
|
||||||
(assert (or (symbol? member)
|
(assert (or (symbol? member)
|
||||||
(keyword? member))
|
(keyword? member))
|
||||||
(str "Symbol or keyword expected, not " member))
|
(str "Symbol or keyword expected, not " (pr-str member)))
|
||||||
(assert (or (not (symbol? object))
|
(assert (or (not (symbol? object))
|
||||||
(not (re-find #"\." (name object))))
|
(not (re-find #"\." (name object))))
|
||||||
(str "Dot not allowed in " object))
|
(str "Dot not allowed in " object))
|
||||||
|
|
|
@ -15,7 +15,9 @@
|
||||||
co#))
|
co#))
|
||||||
|
|
||||||
(defmacro with-let [bindings & body]
|
(defmacro with-let [bindings & body]
|
||||||
(assert (vector? bindings))
|
(assert (vector? bindings)
|
||||||
|
(str "with-let bindings must be a vector, not "
|
||||||
|
(pr-str bindings)))
|
||||||
(let [v (gensym "with-let")
|
(let [v (gensym "with-let")
|
||||||
k (keyword v)
|
k (keyword v)
|
||||||
init (gensym "init")
|
init (gensym "init")
|
||||||
|
|
|
@ -354,7 +354,7 @@
|
||||||
|
|
||||||
IReset
|
IReset
|
||||||
(-reset! [a newval]
|
(-reset! [a newval]
|
||||||
(assert (fn? (.-on-set a)) "Reaction is read only.")
|
(assert (fn? (.-on-set a)) "Reaction is read only; on-set is not allowed")
|
||||||
(let [oldval state]
|
(let [oldval state]
|
||||||
(set! state newval)
|
(set! state newval)
|
||||||
(.on-set a oldval newval)
|
(.on-set a oldval newval)
|
||||||
|
|
Loading…
Reference in New Issue