Print function names instead of source in warnings/errors

This commit is contained in:
Dan Holmsand 2015-10-07 19:50:29 +02:00
parent b86e6278eb
commit ec769d0c3c
4 changed files with 57 additions and 24 deletions

View File

@ -232,16 +232,6 @@
:reagentRender render-f
:render (:render static-fns)))
(defn fun-name [f]
(or (and (fn? f)
(or (.' f :displayName)
(.' f :name)))
(and (implements? INamed f)
(name f))
(let [m (meta f)]
(if (map? m)
(:name m)))))
(defn wrap-funs [fmap]
(when (dev?)
(let [renders (select-keys fmap [:render :reagentRender :componentFunction])
@ -256,10 +246,10 @@
render-fun (or render-fun
(:render fmap))
name (str (or (:displayName fmap)
(fun-name render-fun)))
name (if (empty? name)
(str (gensym "reagent"))
(clojure.string/replace name "$" "."))
(util/fun-name render-fun)))
name (case name
"" (str (gensym "reagent"))
name)
fmap (assoc fmap
:displayName name
:cljsLegacyRender legacy-render
@ -306,7 +296,7 @@
(if (dev?)
(let [c *current-component*
n (or (component-path c)
(some-> c .-constructor fun-name))]
(some-> c .-constructor util/fun-name))]
(if-not (empty? n)
(str " (in " n ")")
""))
@ -319,7 +309,7 @@
(some? (some-> f .-prototype (.' :render)))))
"Using native React classes directly in Hiccup forms "
"is not supported. Use create-element or "
"adapt-react-class instead: " (let [n (fun-name f)]
"adapt-react-class instead: " (let [n (util/fun-name f)]
(if (empty? n) f n))
(comp-name))
(let [spec (meta f)

View File

@ -1,5 +1,6 @@
(ns reagent.impl.template
(:require [clojure.string :as string]
[clojure.walk :refer [prewalk]]
[reagent.impl.util :as util :refer [is-client]]
[reagent.impl.component :as comp]
[reagent.impl.batching :as batch]
@ -249,13 +250,22 @@
jsprops)]
(make-element argv comp p first-child))))))
(defn str-coll [coll]
(if (dev?)
(str (prewalk (fn [x]
(if (fn? x)
(let [n (util/fun-name x)]
(case n "" x (symbol n)))
x)) coll))
(str coll)))
(defn hiccup-err [v & msg]
(str (apply str msg) (pr-str v) (comp/comp-name)))
(str (apply str msg) ": " (str-coll v) "\n" (comp/comp-name)))
(defn vec-to-elem [v]
(assert (pos? (count v)) (hiccup-err v "Hiccup form should not be empty: "))
(assert (pos? (count v)) (hiccup-err v "Hiccup form should not be empty"))
(let [tag (nth v 0)]
(assert (valid-tag? tag) (hiccup-err v "Invalid Hiccup form: "))
(assert (valid-tag? tag) (hiccup-err v "Invalid Hiccup form"))
(cond
(hiccup-tag? tag)
(let [n (name tag)
@ -264,9 +274,9 @@
-1 (native-element (cached-parse n) v 1)
0 (let [comp (nth v 1)]
;; Support [:> comp ...]
(assert (= ">" n) (hiccup-err v "Invalid Hiccup tag: "))
(assert (= ">" n) (hiccup-err v "Invalid Hiccup tag"))
(assert (or (string? comp) (fn? comp))
(hiccup-err v "Expected React component in: "))
(hiccup-err v "Expected React component in"))
(native-element #js{:name comp} v 2))
;; Support extended hiccup syntax, i.e :div.bar>a.foo
(recur [(subs n 0 pos)
@ -309,9 +319,9 @@
[res derefed] (ratom/check-derefs #(expand-seq-dev x ctx))]
(when derefed
(warn (hiccup-err x "Reactive deref not supported in lazy seq, "
"it should be wrapped in doall: ")))
"it should be wrapped in doall")))
(when (.' ctx :no-key)
(warn (hiccup-err x "Every element in a seq should have a unique :key: ")))
(warn (hiccup-err x "Every element in a seq should have a unique :key")))
res))
(defn make-element [argv comp jsprops first-child]

View File

@ -36,6 +36,18 @@
name-str
(apply str start (map capitalize parts))))))
(defn fun-name [f]
(let [n (or (and (fn? f)
(or (.' f :displayName)
(.' f :name)))
(and (implements? INamed f)
(name f))
(let [m (meta f)]
(if (map? m)
(:name m))))]
(-> n
str
(clojure.string/replace "$" "."))))
(deftype partial-ifn [f args ^:mutable p]
IFn

View File

@ -1,7 +1,7 @@
(ns reagenttest.testreagent
(:require [cljs.test :as t :refer-macros [is deftest testing]]
[reagent.ratom :as rv :refer-macros [reaction]]
[reagent.debug :refer-macros [dbg println log]]
[reagent.debug :refer-macros [dbg println log dev?]]
[reagent.interop :refer-macros [.' .!]]
[reagent.core :as r]))
@ -738,3 +738,24 @@
(with-mounted-component [c2] check)
(is (= (:will-unmount @res)
{:at 9 :args [@t]})))))
(defn foo []
[:div])
(deftest test-err-messages
(when (dev?)
(is (thrown-with-msg?
:default #"Hiccup form should not be empty: \[]"
(rstr [])))
(is (thrown-with-msg?
:default #"Invalid Hiccup tag: \[:>div \[reagenttest.testreagent.foo]]"
(rstr [:>div [foo]])))
(is (thrown-with-msg?
:default #"Invalid Hiccup form: \[23]"
(rstr [23])))
(is (thrown-with-msg?
:default #"Expected React component in: \[:> \[:div]]"
(rstr [:> [:div]])))
(is (thrown-with-msg?
:default #"Invalid tag: 'p.'"
(rstr [:p.])))))